bash while循环使用ssh只显示最后一个输出

时间:2015-03-06 15:41:24

标签: bash ssh

我在文件中有服务器列表,在while循环中我会读取并使用ssh来查看命令。它仅显示上次服务器正常运行时间输出。其他服务器没有显示。

例如:

cat a
host1
host2

cat a | while read s; do ssh -q $s -x "uptime"; done

它仅显示host2正常运行时间输出。

我们改为像这样运行它显示两个服务器输出

cat a | while read s; do echo "ssh -q $s -x uptime"|bash; done

我喜欢理解,为什么我们需要读取循环只显示最后一个输出。

我们是否可以使用while循环执行ssh remote命令?

由于 SR

1 个答案:

答案 0 :(得分:0)

你应该这样做:

while read -r s
 do ssh -q $s -x "uptime" </dev/null
done < a

这种情况正在发生,因为ssh正试图从stdin中读取