Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

in my bash loop over a list of some servers, if the ssh connects the bash script exits

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...