using ssh to run commands remotely

Deployment systems like Chef are great, but sometimes it’s useful to have an ad-hoc method for running commands on remote systems. ssh is great for this, in part because it’s already present on most systems.

To follow these examples it’s best to have passwordless authentication setup, otherwise you’ll end up having to enter your password lots of times (you are read about this here).

Running a command on a single remote system is trivial with ssh. You can just do the following:

ssh myusername@remotecomputer mycommand

For example, if you were trying to download an install a tar ball:

ssh [email protected] wget http://exegensis.com/package.tar.gz
ssh [email protected] tar xzvf package.tar.gz
ssh [email protected] tar xzvf ./package/install.sh

Which would run three commands on the remote host to download the package, untar it and run an install script.

However with a bit of bash magic you can use ssh to run the same command on multiple systems (here valis tyrellcorp polsvr1). For example:

for i in valis tyrellcorp polsvr1; do
  ssh pkdick@$i wget http://exegensis.com/package.tar.gz;
  ssh pkdick@$i tar xzvf package.tar.gz
  ssh pkdick@$i tar xzvf ./package/install.sh
done

Which would run the same install process as above on servers called valis tyrellcorp and polsvr1.

It’s a hacky solution compared to using a real deployment system, but it can be useful at times. Particularly for quick fixes.