3

如何在多个主机上使用 ssh 和 xargs 运行 bash 脚本而不将脚本复制到每个主机?

这行得通,
ssh host1.com bash -s < ~/httpd-conf/weblogrotate.sh

这行不通,
cat webserver.hosts | xargs -I % ssh % bash -s < ~/httpd-conf/weblogrotate.sh
tcsh:不明确的输入重定向。

猫 webserver.hosts
host1.com
host2.com
host3.com

4

2 回答 2

2

在 bash 中:

while read host; do
   ssh $host bash -s < ~/httpd-conf/weblogrotate.sh &
done < webserver.hosts

这也并行运行它们。

我相信你也可以在 tcsh 中做到这一点,但我不熟悉那个 shell 的语法。

或者,您也可以使用 GNU 并行而不是 xargs,它通过 ssh 远程执行到远程主机。

于 2012-05-02T17:52:23.430 回答
1

You're looking for DSSH. It's a distributed terminal. Execute command once, it will be executed across all of the hosts.

A hosts file is used

$ cat hosts
mojito
-l noland kodiak
mojito
kodiak
-C mojito
-i /home/noland/.ssh/id_rsa kodiak

Running a command

$ ./dssh.sh "uptime" < hosts
mojito:O:0:19:16:45 up 3 days, 14 min,  5 users,  load average: 0.22, 0.22, 0.20
kodiak:O:0:13:24:00 up 20:00,  1 user,  load average: 0.42, 0.16, 0.05
mojito:O:0:19:16:45 up 3 days, 14 min,  5 users,  load average: 0.22, 0.22, 0.20
kodiak:O:0:13:24:00 up 20:00,  1 user,  load average: 0.42, 0.16, 0.05
mojito:O:0:19:16:45 up 3 days, 14 min,  5 users,  load average: 0.22, 0.22, 0.20
kodiak:O:0:13:24:00 up 20:00,  1 user,  load average: 0.42, 0.16, 0.0
于 2012-05-02T21:00:57.537 回答