while read line 中使用ssh只能读取一行

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

#!/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`

#!/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/Peter2014/p/8342495.html

posted @ 2020-07-03 16:24  杰克马001  阅读(294)  评论(0编辑  收藏  举报