SSH Automation with DSH

SSH Automation with DSH

dsh (aka Dancers Shell or Distributed Shell) is a very handy tool to improve your ssh automation if you have to manage more than a couple of hosts.

Homepage: http://www.netfort.gr.jp/~dancer/software/dsh.html

You should know that dsh will only be fun if you are using ssh with pulic-key authentication. Otherwise you’ll find yourself typing passwords all day.

If you don’t know what that is, I suggest find out about it and come back once you are familiar with it. Some keywords for successful googling: ssh_keygen, id_rsa, authorized_keys, ssh-agent, puttygen.exe

Install dsh

That’s easy: apt-get install dsh

Configure dsh

First you should organize your machines in groups or at least list them in the global machines.list. Besides that you can edit the dsh.conf to fit your needs.

I prefer having the dsh configuration in my home directory like so:

A quick note regarding remoteshellopt:
That’s useful if you want to hand over some options to your remoteshell (which should be ssh), e.g. these ones:

man 5 sshd_config will reveal the possible options for ssh

How to use dsh

As soon as your configuration is set up we are ready to play around with dsh. This brings the basic ssh automation into action.
All the command line parameters and the configuration file are well documented in the man pages:

view the loadavg of the previous configured group ‘olsrv’:

Output:

You can execute more than one command if you seperate them with semicolons:

Here I explicitly name two hosts independent of any group they might be in.

Run your command on every single host (based on what is in machines.list)

ssh automation via batch or script handling with dsh

I had to play around a little bit – there is not so much information about that on the internet. What if you want to execute e.g. 20 consecutive commands? For that I would really prefer a file instead of an endless command line.

I’d name this the “dsh batch mode” or if you prefer “dsh script mode” 😉

So if you want to execute a bunch of commands (aka batch or script) written down in a file the following will work. First create your batch/script file.
It is required to end each command with a ; (semicolon) !

In this example I want to get rid of some unnecessary munin plugins on my hosts:

Then feed dsh with the batch/script:

Having two nodes in the group the output would look like:

Advanced usage: dsh with scp

But what if you want to copy files? Normally you would do that with scp. And because dsh doesn’t support scp, you’ll actually have to stick to scp – what a pity.

Though you can take advantage of your already existing dsh-groups. Let me give you an example.

Imagine you want to copy your new default .bash_login file to all machines in your olsrv dsh group. All you have to do is:

Leave a Reply

Your email address will not be published. Required fields are marked *