while read $HOSTNAME
do
ssh root@$HOSTNAME "poweroff"
done < host.log
host.log 中的内容:
192.168.122.1
192.168.122.2
192.168.122.3
机器配置过ssh互信
执行那个后发现每次只能关闭一台机器脚本就退出了。需要再次执行才能关闭后面的机器,请问是为什么?谢谢
淡淡烟草味2017-04-25 09:03:06
Try writing this line like this: ssh root@$HOSTNAME "poweroff" < /dev/null
漂亮男人2017-04-25 09:03:06
or write ssh -n
. The reason this happens is that ssh reads the rest of your stdin.
So I try not to use while + redirection to read data. If I want to read, I also use another file descriptor to read. stdin can easily be read by other commands.
黄舟2017-04-25 09:03:06
perl -ne 'system q/ssh/, q/root@/.$_,q/poweroff/' host.log #This should be fine
怪我咯2017-04-25 09:03:06
Remove the $ in while read $
HOSTNAMEwhile reading HOSTNAME
do
ssh root@$HOSTNAME "poweroff"
done < host.log