Last Updated: February 25, 2016
·
578
· Amar Sanakal

Working on multiple servers

If you find yourself having to work on multiple servers, I've found the following for loop quite useful.

for s in server1 server2 server3
do
  export s
  bash
  unset s
done

This puts you in a new shell with the $s variable (you can call it whatever you like) set to the first server in the list. You can do your bits using $s to reference the server. After you finish your work on that server, hit CTRL-D to exit that session and you will then be in a new shell with $s set to the next server in the for loop.

In case you lose track of which shell you are in, just echo $s. If it shows a servername, you are still within that for loop. If you've quite all the shel sessions, viz. completed the loop, you will find that $s is empty. I had to put that in for my own safety & sanity :)