我最近经常使用 netcat 来测试服务器,使用向上箭头重复以前的命令会非常有帮助。然而现在,它只进入^[[A
。有什么办法可以改变这种行为吗?
有2种可能性。第一种是使用rlwrap将readline历史库包装在您的netcat
程序周围。另一种方法是使用内置 readline 作为选项的socat。
例如,如果您使用 netcat 执行 telnet,您可能会说
rlwrap nc -t remotehost 23
Run Code Online (Sandbox Code Playgroud)
然后您输入的每一行都保存在文件中~/.nc_history
,可以使用常用的 readline 键进行导航。重新运行相同的命令会保留现有的历史记录。
使用socat
,没有 telnet 选项,但对于其他类型的连接,您可以执行例如
socat readline,history=$HOME/.socat.hist TCP4:remotehost:port
Run Code Online (Sandbox Code Playgroud)
如果你没有,rlwrap
你可以使用 socat 来运行你的 netcat:
socat readline,history=$HOME/.socat.hist exec:'nc -t remotehost 23'
Run Code Online (Sandbox Code Playgroud)
如果您没有这些程序,但有一个bash
带有内置 readline的shell,则第三种可能性是让 bash 从终端读取命令并将它们发送到 netcat 的 stdin。以下是执行此操作的脚本的一个相当简单的示例,使用相同的nc
命令,并在 file 中保存和恢复历史记录/tmp/myhistory
。
#!/bin/bash
# emulate rlwrap nc -t localhost 23
HISTFILE=/tmp/myhistory
history -r # read old history
while IFS= read -p 'netcat> ' -e # sets REPLY, -e enables readline
do history -s "$REPLY" # add to history
history -w # save to file
echo "$REPLY" # write to netcat
done |
nc -t remotehost 23
Run Code Online (Sandbox Code Playgroud)