Hacker News new | ask | show | jobs
by ktopaz 2426 days 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"
2 comments

this 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_adh...

<list_of_hosts xargs -I {} -P 0 -n 1 ssh {} hostname
Here's a more polished version of just this idea: https://github.com/michaelkarlcoleman/ssssh
This 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.