我用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/