There is a list of IP addresses in a .txt file, ex.:
1.1.1.1
2.2.2.2
3.3.3.3
Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is in the known_hosts
list (on my PC, Ubuntu 10.04 LTS/bash).
How can I run commands on these servers, and collect the output?
Ideally, I'd like to run the commands in parallel on all the servers.
I'll be using public key authentication on all the servers.
Here are some potential pitfalls:
known_hosts
file.The servers are AIX/ksh (but I think that doesn't really matter.
Assuming that you are not able to get pssh or others installed, you could do something similar to:
tmpdir=${TMPDIR:-/tmp}/pssh.$$
mkdir -p $tmpdir
count=0
while IFS= read -r userhost; do
ssh -n -o BatchMode=yes ${userhost} 'uname -a' > ${tmpdir}/${userhost} 2>&1 &
count=`expr $count + 1`
done < userhost.lst
while [ $count -gt 0 ]; do
wait $pids
count=`expr $count - 1`
done
echo "Output for hosts are in $tmpdir"
-o StrictHostKeyChecking=no
to avoid this. However it is better to scan all the servers from a command-line first so the host keys can be added. — Aug 19, 2011 at 19:40 wait
command reclaims those resources, including the process' return code (how the processs exited). Then if later in the program, you put another process, lets say a compression, in the background and needs to wait for it, then without this loop, the wait will reclaim one of the completed ssh programs, not the compression - which may still be running. You would get an return status on the wrong child process. It is good to clean up after yourself. — Aug 28, 2011 at 18:23