shell - 当任何连接超时时,xargs sh -c 'ssh …'停止执行

标签 shell ssh xargs

我用bash编写了一个小类轮以在服务器上吐出opensl版本(checking if I'm protected against this most recent openssl vulnerability),但是如果SSH无法连接并超时,它将停止执行脚本的其余部分。.我知道有足够的bash可以解决,但我不太确定要强制它继续执行什么操作,也许是困了SIGTERM并从我离开的地方继续?我敢肯定有一种更简单的方法。

这是命令:

cat servers.txt | \
  xargs -I {} sh -c "echo {} && ssh -o ConnectTimeout=3 myusername@{} openssl version"

其中servers.txt只是一大堆IP地址,每行一个

最佳答案

xargs手册页:

The xargs utility exits immediately (without processing any further input) if a command line cannot be assembled, utility cannot be invoked, an invocation of utility is terminated by a signal, or an invocation of utility exits with a value of 255.



大概您的故障将以255状态退出。

就个人而言,我不会为此而烦恼xargs;您无需执行任何操作(例如并行化或在命令行上放置多个主机名),这特别有用。
while read -r name <&3; do
  echo "$name"; ssh -o ConnectTimeout=3 username@"$name" "openssl version" ||:
done 3<servers.txt

这使用文件描述符3读取servers.txt文件,因此stdin保留其默认值。

顺便说一句,如果我正在编写使用xargs的代码,我会这样做:
xargs sh -c 'for host; do ssh -o ConnectTimeout=3 myusername@"$host" "openssl version" </dev/null' _ <servers.txt

这样,您就可以让xargs将主机列表传递给每个shell调用,而不是每个主机传递一个shell。

关于shell - 当任何连接超时时,xargs sh -c 'ssh …'停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31345677/

相关文章:

docker - 如何取消在 SSH session 中运行的 docker 容器

linux - 如何通过单个扩展名复制所有文件

bash - 一次循环多个项目

linux - 如果已经存在,如何避免设置 cron

shell - 使用 awk 或 sed 将十六进制转换为十进制

bash - 捕获远程 ssh 命令失败的输出

linux - grep 和 ls : 'l' flag not supported for ls when used with xargs?

linux - "-sh: executable_path:not found"是什么意思

C - 等待,获取返回值时失败

ssh - 无法通过集市 ssh 到启动板