shell在while循环中使用ssh命令提前退出的问题

我想编写一个xcall.sh脚本,用于快速向集群中的所有节点执行相同的命令。集群的节点信息放在hosts文件中:

[root@localhost ~]# cat hosts 
10.4.7.81 root
10.4.7.82 root
10.4.7.83 root

编辑的xcall.sh内容如下:

[root@localhost ~]# cat xcall.sh 
#!/bin/bash

params=$@

#到各个节点
while read line
do
    user=$(echo $line | cut -d " " -f 2)
    ip=$(echo $line | cut -d " " -f 1)
    
    echo "*****$ip*****"
    ssh $user@$ip "source /etc/profile; $params"

done < /root/hosts

但是执行xcall.sh之后,发现只执行了一个节点,就退出了:

[root@localhost ~]# ./xcall.sh jps
*****10.4.7.81*****
15107 Jps
[root@localhost ~]#

百度得知,ssh命令在每次执行时,会读取所有的stdin标准输入中的内容。所以第二次read line时什么都读不到了。

解决方法:

  1. ssh -n 使用-n参数(此参数只能在ssh命令后台运行时使用)
  2. ssh xxxx < /dev/null重定向shell命令的输入

修改后的xcall.sh脚本如下:

[root@localhost ~]# cat xcall.sh 
#!/bin/bash

params=$@

#到各个节点
while read line
do
    user=$(echo $line | cut -d " " -f 2)
    ip=$(echo $line | cut -d " " -f 1)
    
    echo "*****$ip*****"
    ssh -n $user@$ip "source /etc/profile; $params"

done < /root/hosts

再执行,结果正常:

[root@localhost ~]# mv xcall.sh /usr/bin/
[root@localhost ~]# type xcall.sh 
xcall.sh is /usr/bin/xcall.sh
[root@localhost ~]# xcall.sh jps
*****10.4.7.81*****
15804 Jps
*****10.4.7.82*****
13511 Jps
*****10.4.7.83*****
12765 Jps
[root@localhost ~]# 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值