Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active October 29, 2023 03:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mbbx6spp/c16b5438270be609619f83c462b148d5 to your computer and use it in GitHub Desktop.
Save mbbx6spp/c16b5438270be609619f83c462b148d5 to your computer and use it in GitHub Desktop.
Tutorial on pdsh. I am revising this for pdsh from my previous tutorial on dsh from 2013 found here: https://gist.github.com/mbbx6spp/6181003

Parallel Distributed SHell (pdsh)

Similar to ansible command but allows you to use any command that will work in your shell. Not tied to specific configuration management tooling, just SSH and your default shell on remote systems. Just works. I <3 it :)

What does it do?

Runs commands across potentially many machines. Allows you to organize your servers/VMs/instances into groups very easily.

Getting Started

Most OSes just have a package for it in their package management system. Doesn't rely on Python or Ruby or whatever. e.g.

# On NixOS/Nix
nix-env -i pdsh

# On Ubuntu
sudo apt-get install dsh

# On CentOS/Fedora/RHEL/YUM-based repos
sudo yum install dsh

# Using Homebrew on OS X
brew install dsh

Ok, now what?

So there is usually a directory where you can define your host groups in plain text files. It's that simple. On most *NIX systems it is /etc/dsh. *NIX conventions FTW. Nothing fancy again.

Most user accounts on a *NIX system will add host group files to their $HOME/.dsh/group directory.

$ tree /etc/dsh/ | head -15     
/etc/dsh/                       
├── dsh.conf                    
├── group
│   ├── all -> ../machines.list 
│   ├── prod.admin
│   ├── prod.api
│   ├── prod.cache
│   ├── prod.proxy
│   ├── prod.webapp
│   ├── prod.webcache
...
│   ├── profiling.admin
│   ├── profiling.api
│   ├── profiling.cache
│   ├── profiling.proxy
│   ├── profiling.webapp
│   ├── profiling.webcache
...
│   ├── staging.admin
│   ├── staging.api
│   ├── staging.cache
│   ├── staging.proxy
│   ├── staging.webapp
│   ├── staging.webcache
...
│   └── api.all
│   └── cache.all
│   └── proxy.all
│   └── webcache.all
│   └── webapp.all
└── machines.list  

What does a group file look like?

$ cat /etc/dsh/group/prod.webapp  
webapp001.appname.com
webapp002.appname.com
webapp003.appname.com
webapp004.appname.com
webapp005.appname.com
webapp006.appname.com
webapp007.appname.com
...
webapp123.appname.com

So now what?

Let's do something.

pdsh -R ssh -M -g webcache.all 'sudo service varnish restart'
pdsh -R ssh -cM -F 4 -g webapp.prod 'pgrep -f my_awesome_app'
pdsh -R ssh -m webapp001.appname.com -m webcache003.appname.com -m proxy005.appname.com 'sudo reboot'
pdsh -R ssh -g staging.webapp -g staging.admin 'sudo service nginx reload'
pdsh -R ssh -cF 5 -g all 'ss | awk '{ print $4 }' | sort | uniq'

Configuration Options

This is an example of the global dsh.conf file.

$ cat /etc/dsh/dsh.conf 
verbose = 0             
remoteshell = ssh       
showmachinenames = 0    
waitshell = 1           
remoteshellopt=-q       

Pretty simple. Check the man pages for more information.

@q2dg
Copy link

q2dg commented Aug 6, 2018

Ansible can be used "ad-hoc" with "shell" module, so configuration is minimal...so I still don't get the point to use pdsh

@shamoya
Copy link

shamoya commented Jan 15, 2020

@q2dg - I think performance.
I tried using ansible and it's impossibly slow, even with a very small set of nodes.
pdsh is very fast, even with 1K+ nodes/

@satishdotpatel
Copy link

I have installed apt-get install pdsh dsh on ubuntu 20.04 but look like it doesn't support dshgroup. look like they removed support of dsh in newer release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment