3 ms·
Years ago I wrote this tiny script: <run-on-all.sh> #!/bin/bash clusterfile=$1 cmd="source /etc/profile; $2" ssh='ssh -n -A -o BatchMode=yes -o Conne
by ktopaz 7y ago
Years ago I wrote this tiny script:
<run-on-all.sh>
#!/bin/bash
clusterfile=$1
cmd="source /etc/profile; $2"
ssh='ssh -n -A -o BatchMode=yes -o ConnectTimeout=10 -o LogLevel=quiet'
#add "-o ConnectTimeout=x" for timing out the ssh connection after x seconds
#redircting stdin for ssh command to /dev/null (using switch "-n" for ssh) since otherwise ssh command breaks bash's "while" loop
#reference: http://www.unix.com/shell-programming-scripting/38060-ssh-break-while-loop.html
echo
while read line;
do
echo "@==============@ running on $line @==============@"
echo
echo
$ssh $line "$cmd"
echo
echo
done < $clusterfile
echo
echo
echo "==============> FINISHED RUNNING for $clusterfile <=============="
All one needs to do is call it like so:
./run-on-all.sh /path/to/cluster/file/list-of-servers-here.txt "sleep 60; reboot"
- scarby2 7y agothis is good for a few servers - but its sequential nature will become onerous if for example you needed to run something that took 10 minutes on 100 hosts. ansible ad-hoc commands do almost exactly what you do but in a more scalable fashion https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html https://docs.ansible.com/ansible/latest/user_guide/intro_adh...
- reacweb 7y ago<list_of_hosts xargs -I {} -P 0 -n 1 ssh {} hostname
- downerending 7y agoHere's a more polished version of just this idea: https://github.com/michaelkarlcoleman/ssssh https://github.com/michaelkarlcoleman/ssssh
- latchkey 7y agoThis is not transactional. What happens when a host reboots randomly in the middle of execution? Or the network drops? You're stuck looking through the logs to see the failure along the way. No thanks. I want something that forces me to be idempotent and retries automatically on failures.