Linux For Loop

How can I write a script to shutdown some remote Linux servers.  I believe I need to create a list of IP addresses for the servers to have the script loop to it, then ssh into as root and shut it down with the shutdown command.  Thanks
yogioAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

woolmilkporcCommented:
Yes, you should create a list of IP addresses or hostnames, one per line.

You can then run either a "while" or a "for" loop against that file, where I prefer the "while" loop:

while read server
  do
    ssh -n root@$server "shutdown now"
  done < /path/to/hostname_list

Here is the "for" loop:

for server in $(</path/to/hostname_list)
  do
    ssh root@$server "shutdown now"
  done

Add options like "-H" or "-P" to the shutdown command, if desired.

wmp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
With ssh, after issuing the shutdown, you have to also issue an exit, or the shutdown, might lock your session.
Usually root login might not be allowed, the use of a Setuid script or sudo shutdown now ;exit might be a way to go.
simon3270Commented:
There shouldn't be a hang - the ssh call will run the command and exit as soon as it finishes.

It can be a problem if you log onto the remote server and issue the "shutdown now" command manually.  In that case, you could issue the command
    exec shutdown now
so that the session ends as soon as the command completes (and before the machine shuts down).
omarfaridCommented:
If you are looking for automating the script, then you need to set trust relationship between your system and remote systems by exchanging keys (so you don't have to provide passwords).

please see link below:

http://www.linuxproblem.org/art_9.html

You can avoid hanging shutdown commands by running woolmilkporc's modified script below:

for server in $(</path/to/hostname_list)
  do
    ssh root@$server "nohup shutdown now &"
  done
yogioAuthor Commented:
Thanks for all the help.
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.