Linux shell script to reboot a few linux machines

Dear experts,

I am task with something I have never done before.

I am sure is easy for some of you :)

I need to execute a linux shell script from our network that will connect ssh to this devices and reboot them with 15 minutes difference.

I imagine it will look something like this:

 #!/bin/bash

USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done
Need to connect the reboot command with the ssh connection

for p in server1 server2 server3
do
  ssh ${p} sudo reboot
  sleep 900

 I need it to connect and reboot after 15 minutes from the first machine rebooted.

Thanks
marceloNYCMiddle-Tier AdministratorAsked:
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.

savoneCommented:
You are going to need SSH keys for password less (no password prompt) login.

You will also need sudo setup so the user can reboot the server without being asked for their password.

#!/bin/bash
username="yourusername"
HOSTS=( host1 host2 host3 )
echo "Array is ${HOSTS[@]}"
for HOSTNAME in "${HOSTS[@]}"; do
        echo "Rebooting $HOSTNAME..."
        ssh $username@$HOSTNAME "sudo reboot"
        sleep 900
done

Open in new window

1

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
marceloNYCMiddle-Tier AdministratorAuthor Commented:
Thank you!
0
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
Shell Scripting

From novice to tech pro — start learning today.