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! |
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 Code:
*/2 * * * * root /usr/local/bin/check_network chmod +x /usr/local/bin/check_network. thats it! |
Thanks! That is really helpful.
|
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" "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" "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 |
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 Hope that works for you too! :) |
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 |
I'll give that one a try, thanks!
|
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? |
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 |
Thanks abibibo!
|
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 :) |
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. |
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. |
An updated script for checking an unstable wifi connection
Nagios is a great solution... if you are managing a whole environment. My use case exactly matches the OP's, and I also was interested in a shell scripting exercise. So I wrote a short script to address exactly this problem; it is updated to include: logging to systemd via systemd-cat, checking connectivity using http HEAD request instead of ping or HTTP GET, and extensive use of nmcli as the scripting interface for Network Manager. Moreover, this script does not use the brute force method of restarting all network services, but rather operates only on the wifi radio and wlan interface.
Warning, this was written late in the day, I was tired, and the script is FAR from optimized or even logical. Feel free to improve on it. Code:
#! /bin/bash |
All times are GMT -5. The time now is 04:12 AM. |