bash - 如何终止 netcat 以便我的脚本可以再次循环?

标签 bash ubuntu-14.04 terminate netcat

我正在运行一个 bash 脚本,它遍历我的远程服务器 IP 列表,通过每一行的 netcat (telnet) 连接,并运行一些命令。

问题是我似乎无法弄清楚如何终止 netcat 以便脚本可以循环到列表中的下一个 IP。

这里是相关的部分:

#!/bin/bash
while ISF= read -r line;do
(
sleep 3
printf 'command1'
sleep 3
printf 'command2'
sleep 3
) | nc $line
done < ~/servers.txt

远程服务器不发送 EOF,那么我可以在 netcat 上使用 echoprintf 来终止 netcat,以便脚本可以再次循环吗?我真的不想为超时设置 -w 标志,因为我有很多服务器需要执行此操作,而超时会使它花费更长的时间。

最佳答案

指定一个超时时间,如果 nc 没有收到来自远程端或通过标准输入的进一步输入,则该超时时间之后将退出。

... | nc "$line" -w 10   # Choose a value for -w as appropriate

关于bash - 如何终止 netcat 以便我的脚本可以再次循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41061678/

相关文章:

linux - 添加引号的字段分隔

python - Ubuntu 更新后 Python 中的段错误

c++ - 在 64 位 Ubuntu 14.04 上为 32 位应用程序构建 QT (QJSValue : No such file or directory)

c++ - 我如何追溯错误

linux - bash 。读取符号之间的输入 "words"字符串

linux - linux 命令的输出未从控制台中被抑制

c# - 为什么 Main 还没有回来?

Android模拟器不会关闭

regex - 尝试匹配字符串并将其添加到替换中

ubuntu-14.04 - iopl() 间歇性失败