Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell script - run command using variable from a list

Posted on 2016-09-26
2
Medium Priority
?
113 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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

916 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