当while read line 遇到 ssh(转)

https://www.cnblogs.com/Peter2014/p/8342495.html

 

问题:while read line 中使用ssh只能读取一行?

1
2
3
4
5
6
#!/bin/sh
while read line
do
echo $line
ssh root@$line "echo 123456 | passwd --stdin peter" /dev/null
done < hosts.txt

结果hosts.txt中只有第一行ip地址生效,问题在于ssh默认也会从标准输入中读取数据,导致循环失败。

解决:使用for line in `cat hosts.txt`

1
2
3
4
5
6
#!/bin/sh
for line in `cat hosts.txt`
do
echo $line
ssh root@$line "echo 123456 | passwd --stdin peter" /dev/null
done

此外,还可以使用ssh -n 来避免ssh读取标准输入。

原文地址:https://www.cnblogs.com/zhidian2020/p/15213480.html