Script to check connection (and restart network if down)
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
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.
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.
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.
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.
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.
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.
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.
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:
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.
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.