Register
It is currently Fri Jul 25, 2014 12:47 am

Check network connectivity and restart if needed


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Tue Apr 22, 2008 12:46 pm   

Joined: Tue Apr 22, 2008 12:39 pm
Posts: 3
I probably am making this harder than it actually is but I have a server for whatever reason tends to drop networking. I cannot determine when it happens but it has happened twice now and I can get everything back online by running service network restart. This is a Novell SLES 10 server and I am looking for a script to place in a cron job that will check and start networking if it has gone astray. Any advice or assistance? I just ran into the site which is bookmarked and I plan to learn quite a bit from everyone. Thanks!


Top
 Profile  
 PostPosted: Tue Apr 22, 2008 12:57 pm   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
for testing connectivity, you can try to ping a stable site. eg:

Code:
if ! ping -q -c 10 google.com | grep -q -F '0% packet loss'; then
  restart
fi


Top
 Profile  
 PostPosted: Tue Apr 22, 2008 1:07 pm   

Joined: Tue Apr 22, 2008 12:39 pm
Posts: 3
Very nice. I am new to the bash scripting world and would never have thought I could do what I need in just there lines, very minimal. I have a lot of work to do in order to become more proficient with bash scripting. Thanks again. I am back off to browse these boards for very useful and meaningful information.

Any recommendations of books or training?

Thanks again.


Top
 Profile  
 PostPosted: Wed Apr 23, 2008 1:07 pm   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I always recommend these to people new to bash:

http://linuxcommand.org/learning_the_shell.php
http://tldp.org/LDP/abs/html/


There is some overlap, but both are great resources.


Top
 Profile  
 PostPosted: Wed Sep 03, 2008 8:25 am   

Joined: Tue Feb 06, 2007 2:25 pm
Posts: 13
Location: Liverpewl
Something like:
Code:
#!/bin/bash
TEST_HOST="192.168.1.254"
NET_SCRIPT="/etc/init.d/net.eth0"
ping -c 1 "$TEST_HOST"
if [[ "$?" != "0"  ]]
then
while [[ 1 ]]
do
$NET_SCRIPT restart && break
done
fi
?


Top
 Profile  
 PostPosted: Wed Sep 03, 2008 10:52 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Not bad. You can use "while true" though to make it a bit nicer-looking


Top
 Profile  
 PostPosted: Wed Sep 03, 2008 4:42 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Just for a little variety (we need more posters!!!) what about fping? it's available in the Debian and OpenSuse repository, but I'm not sure about SuSE Enterprise...:

Code:
if ! fping -q google.com; then service network restart; fi


If the ping is successful, it returns zero, but if not, it returns 1 and restarts networking.


Top
 Profile YIM  
 PostPosted: Thu Sep 04, 2008 10:50 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
What advantage in this case does i have over normal ping? That also returns based on the result iirc.


Top
 Profile  
 PostPosted: Thu Sep 04, 2008 12:43 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
True, ping does also give you a return code! But even with '-q' regular ping still prints the end results to the screen. If you wanted it to be truly silent, you'd have to redirect the output to /dev/null. With fping, the '-q' option means no output at all, just a return code which is all our 'if' statement needs. So to do the same thing with ping, you'd do something like this:

Code:
# if ! ping -q -c1 google.com > /dev/null; then service network restart; fi


That way you just get the return code. I just think fping is a little cleaner. on my screen, the fping command is like, a whole inch shorter! ;) I use it a lot because I miss the old Unix ping that would just say "google.com is alive" instead of giving you the full ping results :)


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP