Advertisement
Guest User

ssh_all.sh

a guest
Feb 25th, 2014
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # this script will ssh into all hostnames/IPs
  4. # passed to it and run whichever command specified
  5.  
  6. # print some usage text if no arguments are given
  7. [[ -n "$1" ]] || { echo "Usage: ssh_all \"command\" [server_list.txt] OR [server1 server2 server3] ..."; exit 0 ; }
  8.  
  9. # pull the first arg out since that is going to be our command sent to ssh
  10. COMMAND=$1
  11. shift
  12.  
  13. # if next arg is a file, then use that as the list of servers
  14. # otherwise use the remaining args as your server list
  15. if [ -f $1 ]; then
  16.      SERVER_LIST=$(cat $1)
  17. else
  18.      SERVER_LIST=$@
  19. fi
  20.  
  21. # specify some options
  22. # use option -oStrictHostKeyChecking=no to get rid of that annoying RSA warning.
  23. SSH_OPTIONS="-oStrictHostKeyChecking=no"
  24.  
  25. # do it!
  26. for SERVER in $SERVER_LIST; do
  27.      echo ""
  28.      echo "Running '$COMMAND' on $SERVER"
  29.      echo $COMMAND | ssh $SSH_OPTIONS $SERVER /bin/bash
  30.      echo "----------------------"
  31. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement