LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-23-2012, 10:14 PM   #1
Bl@ckJ@ck
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Rep: Reputation: Disabled
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
 
Old 08-23-2012, 10:36 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,348

Rep: Reputation: Disabled
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.
 
Old 08-23-2012, 10:54 PM   #3
Bl@ckJ@ck
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you Ser Olmy

I will try this and update my results.
Appreciate your help
 
Old 08-23-2012, 11:23 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
I think you'll need quote marks around the cmd sent
 
Old 08-23-2012, 11:29 PM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,348

Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 08-23-2012, 11:32 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
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
 
Old 08-24-2012, 12:34 AM   #7
Bl@ckJ@ck
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-24-2012, 12:44 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
Code:
bash: /root/scripts/network_restart.sh: Permission denied
means what it says; either the file is not owned by caller or you haven't set executable perms on


Assuming this is a root only script (& it should be), you want it similar to this ; see chown http://linux.die.net/man/1/chown chmod http://linux.die.net/man/1/chmod
Code:
-rwx------. 1 root root   242 Aug 17 21:19 network_restart.sh

given you are new to Linux, I HIGHLY recommend you read
http://rute.2038bug.com/index.html.gz,
see also
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

and www.linuxtopia.org for LOTS of Manuals/books (free to read)

PS var name 'user' != var name 'username' in your script; pick one

PPS add
Code:
set -xv
as 2nd line of your script when debugging

Last edited by chrism01; 08-24-2012 at 12:45 AM.
 
Old 08-24-2012, 01:21 AM   #9
Bl@ckJ@ck
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks Chris and Ser Olmy

I got it to work with your help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i need a script to check my startup for start restart stop linux centos tayseerj Programming 1 06-16-2010 06:11 AM
Network cannot restart successfully in CentOS 5 nagman Linux - Newbie 3 06-07-2010 07:49 AM
Network cannot restart successfully in CentOS 5 !! shipon_97 Linux - Newbie 1 03-17-2010 12:04 PM
Deleting remote directories on multiple hosts with a bash script zirtik Programming 6 04-16-2009 09:52 AM
FTP script to multiple hosts telamonides Linux - General 6 02-23-2007 05:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration