我有这个脚本:
#!/bin/sh
SERVERLIST=hosts
ICMD='cat /etc/redhat-release'
while read SERVERNAME
do
ssh $SERVERNAME $ICMD
done < "$SERVERLIST"
Run Code Online (Sandbox Code Playgroud)
随着hosts就像:
hostname
hostname
hostname
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,./run.sh它只会转到单个主机并停止。为什么不读取整个主机列表?
Pau*_*ant 17
ssh 预读 stdin 的前提是远程作业需要一些数据,这样可以加快启动速度。所以第一份工作吃掉了主机列表的其余部分。
您可以使用该ssh -n选项或通过像ssh < /dev/null. 作为一个腰带和大括号的人,我两者都做:ssh -n < /dev/null <other ssh options>。
如果您真的希望远程作业从本地系统读取某些内容,则需要在每次迭代时对其进行管道传输,或在此处进行文档处理。即使使用for <host>,ssh 也会为每个主机启动从 stdin 读取不确定数量的数据。
我总是会在 ssh 之前 ping 远程主机以获取状态。IIRC,ping -W有一个硬超时选项,虽然ssh可以挂在 DNS 或连接超过 90 秒,对于未知或死主机。
您也可以pdsh为此使用它,它允许在多个主机上并行运行命令。
pdsh -w ^hostlist.txt -R ssh "cat /etc/redhat-release"
Run Code Online (Sandbox Code Playgroud)
^ 定义一个包含主机名列表的文件,或者可以使用逗号分隔的列表:
pdsh -w host1,host2,host3,... -R ssh "cat /etc/redhat-release"
Run Code Online (Sandbox Code Playgroud)
结果如下所示:
pdsh, -w raspi,raspi.local,192.168.0.3,localhost,127.0.0.1 -R ssh "cat /etc/issue"
raspi: Raspbian GNU/Linux 10 \n \l
raspi:
raspi.local: Raspbian GNU/Linux 10 \n \l
raspi.local:
192.168.0.3: Raspbian GNU/Linux 10 \n \l
192.168.0.3:
localhost: Debian GNU/Linux 10 \n \l
localhost:
127.0.0.1: Debian GNU/Linux 10 \n \l
127.0.0.1:
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |