Crash course in dsh

Thanks to a recommendation from Michael and Florian, I've been using dsh with a lot of success for quite some time. In short, dsh is a small application which will allow you to run commands across many servers via ssh very quickly.

You may be wondering: "Why not just use ssh in a for loop?" Sure, you could do something like this in bash:

for i in`cat ~/myhosts.txt`; do ssh $i 'uptime'; done

But dsh allows you to do this:

dsh -g myhosts 'uptime'

In addition, dsh allows you to run the commands concurrently (-c) or one after the other (-w). You can tell it to prepend each line with the machine's name (-M) or it can omit the machine name from the output (-H). If you need to pass extra options, such as which ssh key to use, or an alternative port, you can do that as well (-o). All of these command line options can be tossed into a configuration file if you have a default set of options you prefer.

Another thing that makes dsh more powerful is the groups feature. Let's say you have three groups of servers - some are in California, others in Texas, and still others in New York. You could make three files for the groups:

  • ~/.dsh/group/california
  • ~/.dsh/group/texas
  • ~/.dsh/group/newyork

Inside each file, you just need to list the hosts one after the other. Here's the ~/.dsh/group/texas group file:

db1.tx.mydomain.com
db2.tx.mydomain.com
web1.tx.mydomain.com
web2.tx.mydomain.com
#web3.tx.mydomain.com

As you can see, dsh handles comments in the hosts file. In the above example, the web3 server will be skipped since it's prepended with a comment. Let's say you want to check the uptime on all of the Texas servers as fast as possible:

dsh -c -g texas 'uptime'

That will run the uptime command on all of the servers in the Texas group concurrently. If you need to run it on two groups at once, just pass another group (eg. -g texas -g california) as an argument. You can also run the commands against all of your groups (-a).

The dsh command can really help you if you need to gather information or run simple commands on many remote servers. If you find yourself using it often for systems management, you may want to consider something like puppet.

Printed from: http://rackerhacker.com/2010/01/20/crash-course-in-dsh/ .
© Major Hayden 2012.

11 Comments   »

  • z0mbix says:

    Thanks RH, very kind! I've been using shmux just because it's what I've always used. I keep meaning to give dsh a try, so this is a great excuse to, as I don't need to read up on it!

    Cheers z0mbix

  • Major Hayden says:

    z0mbix - Glad the post was helpful for you!

  • pvo says:

    Thanks, Major. Don't forget to check out pssh (http://freshmeat.net/projects/pssh/). I've used it in the past. It has similar features and is also very fast.

  • Major Hayden says:

    pvo - Thanks. I'll have to give that pssh a looksee. :-)

  • realinfosec says:

    Thanks Maj for the great post.

  • Major Hayden says:

    realinfosec - De nada. ;-)

  • Jimmy Tang says:

    You should take a look at pdsh and genders (written by some guys at LLNL) it's much better than dsh (I used to use dsh too), pdsh just scales better and the genders way of creating groups of hosts is much more nicer to work with.

  • Major Hayden says:

    Jimmy - Thanks for the suggestion. I've had that suggestion come in from a couple of other people as well, so pssh is something I'm going to have to check out very soon.

  • Jeff H says:

    I've used DCMD for years. (http://sourceforge.net/projects/dcmd/) Parallel SSH execution combined with ssh-agent is a great time saver.

  • errr says:

    This was really helpful thanks Major.
    Id like to add the tips you gave me in IRC. To connect to the host as another user or to connect on another port use: -o "-lsomeuser -p 1234" <-- use the 'someuser' user to hit port 1234. This info can be given on the cli (as shown) or placed in the ~/.dsh/dsh.conf

    Awesome stuff =)

  • Major Hayden says:

    Thanks for adding that in, errr!

RSS feed for comments on this post

Leave a Reply

 

  • Welcome! I started this blog as a way to give back to all of the other system administrators who have taught me something in the past. Writing these posts brings me a lot of enjoyment and I hope you find the information useful. If you spot something that's incorrect or confusing, please write a comment and let me know. Drop me a line if there's something you want to know more about and I'll do my best to write a post on the topic.
    -- Major Hayden

    Flattr this