LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2004, 06:42 PM   #1
mac_phil
Member
 
Registered: Sep 2003
Distribution: Mandrake 10.0
Posts: 200

Rep: Reputation: 30
Script to check connection (and restart network if down)


I leave my desktop online 24/7, and often SSH into it when I'm away.

Every once in awhile the system loses its network connection. (This is with DHCP.)

If I'm home, I can just type 'service network restart'. If I'm not home, I'm screwed.

How would I write a simple script to do the following every 15 minutes:

1)Check if the network is up
1a) If up, exit.
2b) If down, issue command 'service network restart' and goto 1.


I understand the basic ideas, that 1 will probably involve pinging some host, but I don't know how to write such a script.

Thanks!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-03-2004, 08:17 PM   #2
scowles
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Fedora
Posts: 620

Rep: Reputation: 31
1) copy/paste the following lines into a new file named /usr/local/bin/check_network. Change the ROUTER_IP variable to the IP address that can verify that your network is up.
Code:
#!/bin/bash

ROUTER_IP=192.168.9.6
( ! ping -c1 $ROUTER_IP >/dev/null 2>&1 ) && service network restart >/dev/null 2>&1
2) Add the following line to /etc/crontab. Change the */2 to whatever increment you want to have this script run in minutes. It's currently set to run /usr/local/bin/check_netowork every two minutes.
Code:
*/2 * * * * root /usr/local/bin/check_network
3) Set the execute permissions on the script. As root, type:
chmod +x /usr/local/bin/check_network.

thats it!
 
Old 12-05-2004, 01:24 AM   #3
mac_phil
Member
 
Registered: Sep 2003
Distribution: Mandrake 10.0
Posts: 200

Original Poster
Rep: Reputation: 30
Thanks! That is really helpful.
 
Old 12-09-2004, 01:44 PM   #4
VibeOfOurTribe
LQ Newbie
 
Registered: Jul 2004
Posts: 21

Rep: Reputation: 16
Ok, I guess I don't really unerstand the 2>&1 part of the script, but your script does not appear to be correct. Here is an example (I removed the >/dev/null so I could see what is going on):

My network is alive and I type:
Code:
(ping -c1 google.com 2>&1) && echo "true"
Here is what that does:
"PING google.com (216.239.37.99) 56(84) bytes of data.

--- google.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms"

notice it doesn't echo "true", which if I'm understanging your script correctly, it should.

So if I do:
Code:
(! ping -c1 google.com 2>&1) && echo "true"
again, while my network is up it says.
"PING google.com (216.239.37.99) 56(84) bytes of data.

--- google.com ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

true"


According to this, your script
Code:
#!/bin/bash

ROUTER_IP=192.168.9.6
( ! ping -c1 $ROUTER_IP >/dev/null 2>&1 ) && service network restart >/dev/null 2>&1
the network will get restarted even if it's up. Removing the "!" seems to have the desired effect, but it just looks incorrect. What is going on here? Possibly our versions of the "ping" program have different implementations?

Last edited by VibeOfOurTribe; 12-09-2004 at 01:57 PM.
 
Old 12-09-2004, 02:08 PM   #5
mac_phil
Member
 
Registered: Sep 2003
Distribution: Mandrake 10.0
Posts: 200

Original Poster
Rep: Reputation: 30
Yes, it seems there is something slightly wrong with the script.

My network connection usually dies about once every three weeks. I've been running this script nonstop for about five days, and it has restarted my network at least 15 times. So it is giving some false positives for the network being down.
 
Old 12-09-2004, 02:13 PM   #6
VibeOfOurTribe
LQ Newbie
 
Registered: Jul 2004
Posts: 21

Rep: Reputation: 16
Ok here is a script that works beautifully for me:
Code:
#!/bin/bash
x=`ping -c1 google.com 2>&1 | grep unknown`
if [ ! "$x" = "" ]; then
        echo "It's down!! Attempting to restart."
        service network restart
fi
This script works because if the network is down, ping will return "ping: unknown host google.com". If it is up then the word "unknown" shouldn't lie anywhere in the output.

Hope that works for you too!
 
2 members found this post helpful.
Old 12-09-2004, 02:21 PM   #7
VibeOfOurTribe
LQ Newbie
 
Registered: Jul 2004
Posts: 21

Rep: Reputation: 16
also, to be on the safe side in my crontab i put a second line which restarts the network every hour regardless. So my crontab for root looks like this:

Code:
*/2 * * * * /usr/local/bin/check_network
0 * * * *    service network restart > /dev/null
 
1 members found this post helpful.
Old 12-09-2004, 02:21 PM   #8
mac_phil
Member
 
Registered: Sep 2003
Distribution: Mandrake 10.0
Posts: 200

Original Poster
Rep: Reputation: 30
I'll give that one a try, thanks!
 
Old 10-14-2009, 09:48 AM   #9
hande1
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
A similar script

Did you have any luck with this Phil?

I'm looking to do something similar but I'm not overly familiar with bash scripting so it's driving me mad.

In an ideal world my script would ping 3 different IPs in turn, and if packet loss on either link exceeded say, 10%, a set of actions would be taken to restart the link:

a) Restart networking
b) Restart networkmanager
c) Ask networkmanager to dial up my link again (using cnetworkmanager as a command line utility )

I have the latter commands, but am struggling to structure and build a script to perform the above logic.

Any gurus fancy offering their kind assistance?
 
Old 01-05-2010, 10:10 PM   #10
abibibo
LQ Newbie
 
Registered: Feb 2007
Location: Ontario, CA
Distribution: Ubuntu 8.04, FC9
Posts: 25

Rep: Reputation: 15
Hande: If it's not too late, here's something I just wrote for my own use. It should be simple to modify such that it tests packet loss for multiple IP's and acts based on the average. Run using the crontab examples posted previously.


Code:
#!/usr/bin/env bash
#/usr/local/bin/wap_check

# Script to monitor and restart wireless access point when needed

maxPloss=10 #Maximum percent packet loss before a restart

restart_networking() {
        # Add any commands need to get network back up and running
        /etc/init.d/networking restart

        #only needed if your running a wireless ap
        /etc/init.d/dhcp3-server restart 
}

# First make sure we can resolve google, otherwise 'ping -w' would hang
if ! $(host -W5 www.google.com > /dev/null 2>&1); then
        #Make a note in syslog
        logger "wap_check: Network connection is down, restarting network ..."
        restart_networking
        exit
fi

# Initialize to a value that would force a restart
# (just in case ping gives an error and ploss doesn't get set)
ploss=101
# now ping google for 10 seconds and count packet loss
ploss=$(ping -q -w10 www.google.com | grep -o "[0-9]*%" | tr -d %) > /dev/null 2>&1

if [ "$ploss" -gt "$maxPloss" ]; then
        logger "Packet loss ($ploss%) exceeded $maxPloss, restarting network ..."
        restart_networking
fi

Last edited by abibibo; 01-05-2010 at 10:18 PM.
 
Old 01-06-2010, 06:19 AM   #11
hande1
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Thanks abibibo!
 
Old 04-07-2016, 04:43 AM   #12
jnovos
LQ Newbie
 
Registered: Apr 2016
Location: A Coruņa
Distribution: lubuntu, raspbian, orangepi-debian
Posts: 1

Rep: Reputation: Disabled
Wink

The command ping not work into sh script because it needs to change the permission.

In ubuntu execute this command sudo chmod u+s `which ping`



thanks for script
 
Old 04-11-2016, 05:09 AM   #13
pingu_penguin
Member
 
Registered: Aug 2004
Location: pune
Distribution: Slackware
Posts: 365

Rep: Reputation: 61
Try dig , doesnt require root priviledges.

A script could go this way

#!/bin/bash

dig google.com
REPLY=`echo $0`
if [ $REPLY ! = 0 ]
then service network restart
else
sleep 60s


You can put this script in a infinite loop.
I believe this is a lot simpler. You can redirect output to /dev/null wherever needed and tune the sleep time too as required.

Also you can set the HOSTIP variable as needed with the following :
HOSTIP=`ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`

to check if your ethernet card has been given a ip.
 
Old 04-12-2016, 01:00 PM   #14
rob.rice
Senior Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 1,076

Rep: Reputation: 205Reputation: 205Reputation: 205
the simplest way to do what you want would be to install
wicd and set it to aoutmaticly reconnect BUT you need to go back to the out of the box net work settings for the network
interface you use to connect to the inter net
it will work for eather net connections
I use it for wifi it's so fast at reconneccting that videos
on youtube don't even buffer befor the connection is back up
 
Old 04-12-2016, 05:03 PM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,987
Blog Entries: 4

Rep: Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038Reputation: 4038
There's a really good, generalized, solution to this sort of system-management task: Nagios.

There are both commercial and open-source versions of this battle-tested product. I can personally attest that it works beautifully, for this and many other things.
 
  


Reply

Tags
bash, connection, network, ping, restart, script, ssh


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
wireless connection requires network restart bbcar Linux - Wireless Networking 1 11-21-2005 03:02 AM
Restart network connection? GT_Onizuka Linux - Networking 6 09-09-2004 07:30 PM
Cron job to check/restart network connection? mac_phil Linux - Networking 6 09-01-2004 02:51 PM
How to restart network connection satimis Debian 2 01-12-2004 03:30 AM
bash script for network restart munkeh Programming 8 11-10-2003 06:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:58 AM.

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