Scripting remote execution with SSH

Scripting remote execution with SSH

Today, while doing some maintenance on my MythTV master backend, I rebooted it, but forgot to restart the backend services on my slave backends. Of course this meant that I missed some recordings. This started me thinking about how I could automate the process of restarting the remote backend processes whenever I started the master backend process. The solution was to configure ssh to authenticate between the machines using a public/private keypair, rather than using password authentication. I could then write a script that would ssh to the slave machines and restart their mythbackend services.

Setting up the ssh authentication is simple. LetÂ’s say I want to run a script from a machine named mythmbe and have it execute a command using the username mythtv on the remote machine named mythbe1

First, generate a private/public keypair using ssh-keygen, which will be stored in ~/.ssh

$ ssh-keygen -t dsa

Now copy the public key to every remote machine you want to ssh in to without supplying a password…

$ scp ~/.ssh/ mythtv@mythbe1:~mythtv/.ssh/authorized_keys

You can now log in to the remote machine with ssh without needing to supply a password. The public key you copied to the remote machine will be tested against your private key to verify your identity whenever you try to connect. To test this, try something like…

$ ssh mythbe1 /sbin/service mythbackend restart

Leave a Reply