Solved

Shell script - run command using variable from a list

Posted on 2016-09-26
2
58 Views
Last Modified: 2016-09-26
Hi,

I need to issue a command to 10 hosts remotely and I was wondering what would be the best way to script it so I wouldnt have run it over and over for each one of them...

I was thinking on having a host list variable in a script and use:

ssh $HOST <command>

For each command I run I need to create a output file in the local machine where Im issuing the command from...

So it would be: ssh $HOST1 <command> > HOST1, ssh $HOST2 <command> > HOST2.. and so on....

What would be the best way to do it in a shell script?

Tks
0
Comment
Question by:joaotelles
  • 2
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 41816267
You should create a file (e. g. "hostlist") containing your host names, one name per line.

Then use a "while" loop:

while read HOST
  do
     ssh -n  $HOST <command> > ${HOST}.result
   done < hostlist

Please note the "-n" flag of "ssh". It's very important here to prevent "ssh" from emptying stdin.
Note, too, that this doesn't work when "ssh" has to ask for a passphrase.
If you can't avoid this please let me know - we'll use a different approach then.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41816276
If you don't have passwordless access to your hosts use "for" instead of "while" ("for" does not read from stdin):

for HOST in $(<hostlist)
  do
     ssh $HOST <command> > ${HOST}.result
   done
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now