Shell script - remote execution on multiple servers

ittechlab
ittechlab used Ask the Experts™
on
I need to write a shell script to login to 5 linux servers using ssh and run a command remotely.

ssh keys are already.  key less authentication is setup already.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
for sys in systemname1 systemname2 systemname3 systemname4 systemname5 
do
     ssh ${sys} do_command
done

Open in new window


or with systemnames from a file:

for sys in $( cat systemnamefile )
do
     ssh ${sys} do_command
done

Open in new window

David FavorFractional CTO
Distinguished Expert 2018

Commented:
If your commands will take a long time to run, you can use one of the many parallel ssh wrappers or...

Just append an & to the end of do_command above.
nociSoftware Engineer
Distinguished Expert 2018

Commented:
If working from an X workplace, try clusterssh to run one command on many systems.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ittechlabLinux Support

Author

Commented:
is below statement will work?

for sys in systemname1 systemname2 systemname3 systemname4 systemname5
do
     ssh ${sys}  "rpm -qa | grep httpd"
done
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
The syntax looks fine and when I run something similar, it works as expected. Please don't award me any points. They should be given to noci as it is their answer.
nociSoftware Engineer
Distinguished Expert 2018

Commented:
for sys in systemname1 systemname2 systemname3 systemname4 systemname5 
do
     ssh ${sys}  bash -c  "rpm -qa | grep httpd"
done

Open in new window


definitely will work as expected. (the | will need to be executed on the remte system ==>  it needs to be expanded on a command line on the remote system.
bash -c will do just that.
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
Having tested your original, noci, using the quotes works for me on my distro as well, without explicitly using bash.
nociSoftware Engineer
Distinguished Expert 2018

Commented:
@martyn, openssh does that, but not all ssh = openssh.  I do know of one that doesn't do /bin/sh -c...  (openvms)

Also the default shell of the user is taken from the password file if that is a menu or /bin/false there will be problems anyway.
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
@noci, thanks for the clarification. I imagined that there was a good reason for mentioning it specifically.
dssh handles it quite nicely

@noci, whatever menu or shell configured on the remote system is bypasssed when you pass a command to run. which is why setting /bin/false as the shell for user that should not be able to login does not work.

passing arguments white spaces or complex commands to ssh tend to be a total pain in pretty much all cases

if those are needed, you may resort to the easier

ssh destination sh -s <<EOF
set "$@"
# shell commands here
EOF

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial