Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-23-2012, 10:14 PM
|
#1
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Rep:
|
Script to restart the network of multiple Centos hosts
Hi members,
I am new to Linux and I require some help here.
Basically I am tasked to get a way to restart the network of about 30 Centos servers.
The command I use is "service network restart"
Now is there a way to execute "service network restart" all the 30 servers WITHOUT having to manually login to all 30 servers?
I have got a primary server that already has passwordless connection to these 30 servers. All I need is a simple script to accomplish this.
Can anyone please guide me on this?
Thank you
|
|
|
08-23-2012, 10:36 PM
|
#2
|
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 3,348
Rep:
|
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.
|
|
|
08-23-2012, 10:54 PM
|
#3
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Original Poster
Rep:
|
Thank you Ser Olmy
I will try this and update my results.
Appreciate your help
|
|
|
08-23-2012, 11:23 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
I think you'll need quote marks around the cmd sent
|
|
|
08-23-2012, 11:29 PM
|
#5
|
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 3,348
Rep:
|
Quote:
Originally Posted by chrism01
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.
|
|
|
08-23-2012, 11:32 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
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
|
|
|
08-24-2012, 12:34 AM
|
#7
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Original Poster
Rep:
|
Thanks Guys,
I still am having some problems.
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
|
|
|
08-24-2012, 01:21 AM
|
#9
|
LQ Newbie
Registered: Aug 2012
Posts: 7
Original Poster
Rep:
|
Thanks Chris and Ser Olmy
I got it to work with your help
|
|
|
All times are GMT -5. The time now is 03:29 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|