Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

Shell script - run command using variable from a list

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
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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