bash脚本并行ssh远程命令 [英] bash script parallel ssh remote command
问题描述
我有一个脚本,该脚本通过ssh连接在多台不同的计算机上触发远程命令.脚本如下所示:
i have a script that fires remote commands on several different machines through ssh connection. Script goes something like:
for server in list; do
echo "output from $server"
ssh to server execute some command
done
问题显然是时间,因为它需要建立ssh连接,启动命令,等待答案,打印出来.我想要的是一个脚本,该脚本将尝试一次建立所有连接,并在得到脚本后立即返回echo"$ server的输出"和命令的输出,因此按列表顺序是不必要的.
The problem with this is evidently the time, as it needs to establish ssh connection, fire command, wait for answer, print it. What i would like is to have script that would try to establish connections all at once and return echo "output from $server" and output of command as soon as it gets it, so not necessary in the list order.
我已经搜索了一段时间,但没有找到答案.我无法在命令作为一个线程建议运行后取消ssh会话,因为我需要输出,并且不能在其他线程中使用建议的并行gnu.另外,我不能使用任何其他工具,也不能在此计算机上安装任何东西,只有可用的工具是GNU bash,版本4.1.2(1)-发行.
I've been googling this for a while but didn't find an answer. I cannot cancel ssh session after command run as one thread suggested, because i need an output and i cannot use parallel gnu suggested in other threads. Also i cannot use any other tool, i cannot bring/install anything on this machine, only useable tool is GNU bash, version 4.1.2(1)-release.
另一个问题是,像这样的ssh会话如何受到限制?如果我只是粘贴5条左右的"ssh connect,执行某些命令"行,则实际上什么也没做,或者仅从列表中的第一条执行. (如果我粘贴3-4行,则可以使用).谢谢
Another question is how are ssh sessions like this limited? If i simply paste 5+ or so lines of "ssh connect, do some command" it actually doesn't do anything, or execute only on first from list. (it works if i paste 3-4 lines). Thank you
推荐答案
您尝试过吗?
for server in list; do
ssh user@server "command" &
done
wait
echo finished
更新:启动子shell:
Update: Start subshells:
for server in list; do
(echo "output from $server"; ssh user@server "command"; echo End $server) &
done
wait
echo All subshells finished
这篇关于bash脚本并行ssh远程命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!