3

I wrote a script that SSH to remote servers in loop and run a command, it gets hung after running on last server.Here is my loop script -

cat server | while read serName
        do
                cat cmmnds| while read line
                do
                 result=`ssh -f $serName  $line`
                 echo -n ",$result" >> test.csv
        done
echo " " >> test.csv
done

Here is how it errors out in the end and have to control+C for exiting out

Cannot fork into background without a command to execute.
ssh: illegal option -- d

usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-e escape_char] [-F configfile]
           [-I pkcs11] [-i identity_file]
           [-L [bind_address:]port:host:hostport]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-R [bind_address:]port:host:hostport] [-S ctl_path]
           [-W host:port] [-w local_tun[:remote_tun]]
           [user@]hostname [command]
Cannot fork into background without a command to execute.
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
Cannot fork into background without a command to execute.

Any clue ??

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Dee
  • 31
  • 2

1 Answers1

1

Rather than try and do this in this manner using ssh you might want to use a tool such as pssh. This U&L Q&A titled: Execute a command on multiple hosts, but only print command if it is successful?.

In your case you could take the server & command files directly into pssh like so:

$ pssh -h server -i -I < commands.txt

Example

Here I've constructed the following 2 files:

$ cat servers
skinner
mulder

$ cat commands.txt
date
echo 'hi'

Now when I run it:

$ pssh -O ForwardX11=no -h servers -i -I < commands.txt
[1] 03:10:03 [SUCCESS] skinner
Fri Jul 17 03:10:03 EDT 2015
hi
[2] 03:10:04 [SUCCESS] mulder
Fri Jul 17 03:06:41 EDT 2015
hi

You can either catch the above output like this:

$ .... | tee some_log_file.txt

Or you can take the -i switch out and let pssh create separate log files for each server using the -o <dir> switch.

$ pssh -O ForwardX11=no -h servers -o ~/somedir -I < commands.txt
[1] 03:13:04 [SUCCESS] skinner
[2] 03:13:04 [SUCCESS] mulder

Now check the results:

$ ls somedir/
mulder  skinner

$ cat somedir/*
Fri Jul 17 03:09:41 EDT 2015
hi
Fri Jul 17 03:13:03 EDT 2015

References

slm
  • 363,520
  • 117
  • 767
  • 871