在我的bash循环中,如果ssh连接到bash脚本退出,则对一些服务器的列表进行循环

时间:2021-03-23 14:22:16

I have a quick script to run a command on each server using ssh (i am sure there are lots of better ways to do this, but it was intended to just work quick!!). For the test1 etc, there is no server so the script continues, the script also continues if the pubkey auth fails. However if the script connects, the date is printed but the ssh loop terminates...

我有一个使用ssh在每个服务器上运行命令的快速脚本(我确信有很多更好的方法可以做到这一点,但是它的目的是快速运行!!)对于test1等,没有服务器,所以脚本继续,如果pubkey auth失败,脚本也继续。但是,如果脚本连接,日期将被打印,但是ssh循环终止…

#!/bin/bash -x

cat <<EOF |
##file servers
test1
test2
server1
server2
EOF
while read line
do
if [ "${line:0:1}" != "#"  ]; then

ssh -q -oPasswordAuthentication=no -i id_dsa user1@${line} date

fi
done

echo read line must have exited

output is like so;

输出是一样;

+ cat
+ read line
+ '[' t '!=' '#' ']'
+ ssh -q -oPasswordAuthentication=no -i id_dsa user1@test1 date
+ read line
+ '[' t '!=' '#' ']'
+ ssh -q -oPasswordAuthentication=no -i id_dsa user1@test2 date
+ read line1
+ '[' s '!=' '#' ']'
+ ssh -q -oPasswordAuthentication=no -i id_dsa user1@server1 date
Fri Jul  9 09:04:16 PDT 2010
+ read line
+ echo read line must have exited
read line must have exited`enter code here`

something to do with the successful return of the ssh command is messing with the condition for the loop or the var... any suggestions on why?

成功返回ssh命令的原因是循环或var……任何建议,为什么?

2 个解决方案

#1


9  

You should pass the -n flag to ssh, to prevent it messing with stdin:

您应该将-n标志传递给ssh,以防止它干扰stdin:

ssh -n -q -oPasswordAuthentication=no -i id_dsa user1@${line} date

I tested this with my own server and reproduced the problem, adding -n solves it. As the ssh man page says:

我用我自己的服务器测试了这个问题,并复制了这个问题,添加-n可以解决这个问题。正如ssh man页面所言:

Redirects stdin from /dev/null (actually, prevents reading from stdin)

从/dev/null重定向stdin(实际上,防止从stdin读取数据)

In your example, ssh must have read from stdin, which messes up your read in the loop.

在您的示例中,ssh必须读取来自stdin的内容,这会在循环中打乱您的读取。

#2


0  

I think the reason is that as ssh is being forked and exec'd in your bash script the script's standard input is being reopened so your read simultaneously terminates. Try re-crafting as follows:

我认为原因在于,当ssh在您的bash脚本中被分叉并执行时,将重新打开脚本的标准输入,以便您的read同时终止。尝试re-crafting如下:

for line in test1 test2 server1 server2
do
    if [ "${line:0:1}" != "#"  ]; then
        ssh -q -oPasswordAuthentication=no -i id_dsa user1@${line} date
    fi
done

or maybe run the ssh in a sub-shell like this:

或者在这样的子shell中运行ssh:

( ssh -q -oPasswordAuthentication=no -i id_dsa user1@${line} date )

#1


9  

You should pass the -n flag to ssh, to prevent it messing with stdin:

您应该将-n标志传递给ssh,以防止它干扰stdin:

ssh -n -q -oPasswordAuthentication=no -i id_dsa user1@${line} date

I tested this with my own server and reproduced the problem, adding -n solves it. As the ssh man page says:

我用我自己的服务器测试了这个问题,并复制了这个问题,添加-n可以解决这个问题。正如ssh man页面所言:

Redirects stdin from /dev/null (actually, prevents reading from stdin)

从/dev/null重定向stdin(实际上,防止从stdin读取数据)

In your example, ssh must have read from stdin, which messes up your read in the loop.

在您的示例中,ssh必须读取来自stdin的内容,这会在循环中打乱您的读取。

#2


0  

I think the reason is that as ssh is being forked and exec'd in your bash script the script's standard input is being reopened so your read simultaneously terminates. Try re-crafting as follows:

我认为原因在于,当ssh在您的bash脚本中被分叉并执行时,将重新打开脚本的标准输入,以便您的read同时终止。尝试re-crafting如下:

for line in test1 test2 server1 server2
do
    if [ "${line:0:1}" != "#"  ]; then
        ssh -q -oPasswordAuthentication=no -i id_dsa user1@${line} date
    fi
done

or maybe run the ssh in a sub-shell like this:

或者在这样的子shell中运行ssh:

( ssh -q -oPasswordAuthentication=no -i id_dsa user1@${line} date )