Shell script - remote execution on multiple servers

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.
ittechlabLinux SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 EngineerCommented:
If working from an X workplace, try clusterssh to run one command on many systems.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

ittechlabLinux SupportAuthor 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 DirectorCommented:
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 EngineerCommented:
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 DirectorCommented:
Having tested your original, noci, using the quotes works for me on my distro as well, without explicitly using bash.
nociSoftware EngineerCommented:
@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 DirectorCommented:
@noci, thanks for the clarification. I imagined that there was a good reason for mentioning it specifically.
skullnobrainsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.