pssh tricks to accept a group of hosts as a pattern and execute a local script remotely
pssh is a tool that allows to run commands in parallel, I love this tool since it is an alternative to Ansible for ad-hoc commands where…
pssh is a tool that allows to run commands in parallel, I love this tool since it is an alternative to Ansible for ad-hoc commands where Ansible is an overkill for such simple tasks, despite its overall usefulness pssh missed two features that i consider very useful
- Define a group of remote hosts based on a numeric pattern like host[0–10]
- execute a local script remotely avoiding copying the file to the remote host
As we said those options are not built in but there are ways to overcome them, let's see how.
Define a group of hosts based on a numeric pattern
Brace expansion is a bash shell technique where you can create text based on a sequence of numbers or text, the example we will show here will be a numerical example suitable to create hosts in this format host[0–10]. Open a terminal and type the following
echo host{1..10}This should create the following output
host1 host2 host3 host4 host5 host6 host7 host8 host9 host10what if our hosts start from 01 to 10? enter the following, and will do the trick!
echo host{01..10}
host01 host02 host03 host04 host05 host06 host07 host08 host09 host10To make this output useful we need to integrate it with the pssh tool, to do this write the following
pssh -i -H "$(echo host{1..10})" "date"This will connect in parallel to each host with the name host1 to host10 and execute the command date, this works because -H parameter expects a line of hosts separated by space.
Execute a local script remotely
using the ssh command you could execute a local script in a remote host like this
ssh host1 < my_script.shUnfortunately, this is not the case with pssh, but there is a trick!
pssh -i -H "$(echo host{1..10})" "$(cat my_script.sh)"What this effectively does is pass in the stdin the content of script my_script.sh and execute the commands.
Conclusion
pssh is a cool tool with some limitations, but as we can see we can overcome those limitations by using smart and simple tricks! i hope you enjoyed this short article!