在我的bash回路对一些服务器的列表,如果SSH连接bash脚本退出 [英] in my bash loop over a list of some servers, if the ssh connects the bash script exits

查看:109
本文介绍了在我的bash回路对一些服务器的列表,如果SSH连接bash脚本退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的脚本运行在每个服务器上使用SSH命令(我相信有很多更好的方法来做到这一点,但它的目的是刚工作快!)。对于TEST1等,不存在服务器,以便脚本将继续,如果PUBKEY AUTH失败剧本还在继续。但是,如果脚本连接,日期打印,但SSH循环终止...

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...

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

输出像这样;

+ 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`

是与ssh命令成功返回与为循环或VAR的条件搞乱...任何建议为什么?

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?

推荐答案

您应该通过-n标志为SSH,为prevent它与标准输入搞乱:

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

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

我用我自己的服务器测试这一点,并再现了问题,加上-n解决它。由于SSH手册页说:

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

从/ dev / null的标准输入重定向
  (实际上,prevents从读
  标准输入)

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

在你的榜样,SSH必须从标准输入,它弄乱了你的阅读在循环中读取。

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

这篇关于在我的bash回路对一些服务器的列表,如果SSH连接bash脚本退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆