Script to monitor Internet connection stability

11,241

Solution 1

You can try something like that in a cron job

#!/bin/bash
dt=$(date +%Y%m%d_%H%M%S)
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
    echo "$dt:Online"
else
    echo "$dt:Offline"
    mail -s "Internet connection lost on $(hostname) at $(date)" 
fi

I personnaly have a script that stores reports of connection quality (depending on package speedtest you can get from apt-get install speedtest-cli

#!/bin/bash
#***************************************
# FOR CRONTAB
# */10 * * * * /location/of/my-internet-test.sh
#***************************************
LOG_FILE="/var/log/internet_test_$(hostname).csv"
mkdir -p $(dirname $LOG_FILE)
DT=$(date '+%Y-%m-%dT%H:%M:%S')
SPEED_TEST_RES=$(speedtest-cli --simple 2>/dev/null)
#set -o xtrace
DL=$(echo $SPEED_TEST_RES | awk '{print $5}')
UL=$(echo $SPEED_TEST_RES | awk '{print $8}')
PING=$(echo $SPEED_TEST_RES | awk '{print $2}')
#set -x
#echo "Ping: '$PING' DL:'${DL}' UL:'${UL}'"
[[ -z "$DL" ]] && { DL=0; UL=0; PING=0; }
echo "$DT,$PING,$DL,$UL" >> $LOG_FILE

Note that its a better practice to call a script regularly from a cron than doing a loop

Solution 2

I appreciate this is an old question, but I found it searching for a solution, so I ended up writing one. It's a shell script that checks for internet connectivity, and logs any outages, along with the re-connected link speed. Feel free to grab it:

https://github.com/TristanBrotherton/netcheck

Solution 3

Since the introduction of the ip command you can do something with the ip monitor command. Try doing something like this :

#!/bin/bash
shopt -s extglob
ip -ts monitor neigh | while read event; do
    case "$event" in
      *router*FAILED*) POST_NOTIFICATION_OF_ROUTER_DOWN ;;
esac
done

Theip monitor neigh will monitor the states of the neighboring hosts. The router is a neigbor as well as a router.

When the router goes down the monitor sends a message like this:

[2020-04-19T07:22:59.322335] fe80::1256:3456:ffff:1212 dev ethusb1  router FAILED

When the router is up the message is like this:

[2020-04-19T07:30:15.291261]fe80::1256:3456:ffff:1212  dev ethusb1 lladdr 10:56:11:7e:42:49 router REACHABLE

The shopt -s extglob enables advanced bash parsing.

The -ts is for short timestamp format. It isn't functionally necessary for this, but I find it useful when logging the output.

To monitor all ip objects, try ip -ts monitor all.

Share:
11,241

Related videos on Youtube

Eduardo
Author by

Eduardo

Updated on September 18, 2022

Comments

  • Eduardo
    Eduardo over 1 year

    I would like it to be reported every time that the my router loses its internet connection.

    I would like to have my Ubuntu system send me an email every time that the router's system log displays the message "ethernet link down".

    Is that possible? I believe that I would have to write a script, am I right?

    • muru
      muru over 9 years
      How do you access the router's log? Via a browser? More importantly, how will the mail be sent if your internet connection is lost? O.o
  • Philippe Gachoud
    Philippe Gachoud over 6 years
    @Eduardo I'll put your log file in /var/log/yourLogFile.log and will finish it with .log extension! csv is coma separated values, if you want to do a statistic you'd better in fact do a csv log file with date & time (see date command) and then you could do a graph with gnuplot
  • bac0n
    bac0n over 4 years
    cat: /sys/class/net/virbr0-nic/carrier: Invalid argument some carrier will return -EINVAL ... think you can use $(</sys/class/net/$interface/carrier) instead of cat. Reading from sysfs returns EINVAL
  • Stephen Boston
    Stephen Boston over 4 years
    ping -c1 google.com is simpler than wget.
  • Admin
    Admin almost 2 years
    Good job, good job!