Saturday, September 29, 2012

ssh runs only once inside a loop

If you need to run ssh inside a loop, you need to pass in "-n" flag in ssh to tell ssh to take stdin from /dev/null. If not, the loop will stop after the first run

for h in host1 host2 host3 host4
do
    ssh -n user@$h "/run/something"
done

man page on my Ubuntu say:
     -n      Redirects stdin from /dev/null (actually, prevents reading from
             stdin).  This must be used when ssh is run in the background.  A
             common trick is to use this to run X11 programs on a remote
             machine.  For example, ssh -n shadows.cs.hut.fi emacs & will
             start an emacs on shadows.cs.hut.fi, and the X11 connection will
             be automatically forwarded over an encrypted channel.  The ssh
             program will be put in the background.  (This does not work if
             ssh needs to ask for a password or passphrase; see also the -f
             option.)

0 Comments:

Post a Comment

<< Home