Bash script parallel remote ssh command

I have a script that runs remote commands on several different machines via an ssh connection. The script looks something like this:

for server in list; do
echo "output from $server"
ssh to server execute some command
done

The problem with this, obviously, is time, since it needs to establish an ssh connection, a fire brigade command, wait for a response, print it. I would like the script to try to establish connections immediately and return echo "output from $ server" and output the command immediately after receiving it, therefore it is not necessary in the order of the list.

I searched for this for a while, but could not find an answer. I cannot cancel the ssh session after running the command as a single thread, because I need the output, and I cannot use the parallel gnu suggested in other threads. Also, I cannot use any other tool, I cannot bring / install anything on this computer, only a useful tool is GNU bash, version 4.1.2 (1) -release.

Another question: how limited are ssh sessions? If I just insert 5 or more lines of "ssh connect, do some command", it actually does nothing or will execute only the first one from the list. (it works if I insert 3-4 rows). Thanks you

+4
source share
4 answers

Have you tried this?

for server in list; do
  ssh user@server "command" &
done
wait
echo finished

: :

for server in list; do
  (echo "output from $server"; ssh user@server "command"; echo End $server) &
done
wait
echo All subshells finished
+6

SSH, :

, , Chef, Puppet, Ansible, Fabric .. (. ).

- , ​​ pconsole

GNU, script :

for server in $servers ; do
   ( { echo "output from $server" ; ssh user@$server "command" ; } | \
    sed -e "s/^/$server:/" ) & 
done
wait 

sort .

+3

, , - : https://github.com/bearstech/pussh

, - 250 20 ( , ssh-agent). .

man- ( "man./pussh.1" ): https://github.com/bearstech/pussh/blob/master/pussh.1

< >

rootfs :

pussh -f servers df -h / |grep /dev |sort -rn -k5

:

pussh -f servers grep ^processor /proc/cpuinfo |wc -l

, :

pussh -f servers sed -ne "s/^model name.*: //p" /proc/cpuinfo |sort |uniq -c

:

pussh -f servers -o packages-for-%h dpkg --get-selections

():

tar czf files.tar.gz ... && pussh -f servers -i files.tar.gz tar -xzC /to/dest

():

pussh -f servers -o '|(mkdir -p %h && tar -xzC %h)' tar -czC /src/path .

, pussh -u ( ) , , , , . , .

+3

parallel-ssh pssh:

pssh -h servers.txt -l user command

, . -P .

+1

All Articles