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

4
  • 3
    1) What is in servers? 2) What is in cmmnds? Please edit your post with more information. Commented Jul 17, 2015 at 3:07
  • Add set -x before that loop to get a trace of what's going on. Evidently one of your variables doesn't contain what you think it does. Properly double-quoting variable expansions may help. Or it could be as simple as having a blank line. Commented Jul 17, 2015 at 22:56
  • I'd go with there being a blank line in each of the two source files. Commented Jul 17, 2015 at 23:34
  • And please indent your code properly. Commented Jul 17, 2015 at 23:53

1 Answer 1

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

Not the answer you're looking for? Browse other questions tagged .