Script to restart the network of multiple Centos hosts
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ssh can take a "command" parameter: ssh user@hostname [command]
If you have passwordless ssh login, is should do the trick. It could then be scripted quite easily:
Code:
username=user
servers="server1 server2 server3"
for host in ${servers} ; do
ssh ${user}@${host} service network restart
done
Note: I don't know how the "service" command works, and if you're reconfiguring all network connections, there's a chance the ssh session will terminate before the command finishes, leaving the network connections in an unknown state. If so, you may have to use "service network restart &" (including double quotes) to make the process fork into the background.
I think you'll need quote marks around the cmd sent
Actually, no, although it certainly won't hurt. I tested this prior to posting:
Code:
ssh user@server ls -l
...and got a long listing. However, in order to send a character that would otherwise be interpreted by the shell, like &, quotes are obviously needed.
Yeah, I wasn't sure off the top of my head, but I'd definitely want to background the cmd in case it hangs or some such.
Basically, I think I always quote it just in case; defensive programming
Basically from my controlling server, I named and saved the scripts as network_restart.sh
Then from the controlling server, I ran the following command:
[root@Controller ~]# /root/scripts/network_restart.sh
Here is the result:
[root@Controller ~]# /root/scripts/network_restart.sh
-bash: /root/scripts/network_restart.sh: Permission denied
********************************************************************
This is the content of network_restart.sh
********************************************************************
#! /bin/bash
username=root
servers="10.1.88.88"
for host in ${servers} ; do
ssh ${user}@${host} "service network restart"
done
exit
I hope I am clear enough for you to help
Cheers
Last edited by Bl@ckJ@ck; 08-24-2012 at 12:36 AM.
Reason: typo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.