I was having intermittent problems not being able to connect to the internet and needed to find out when and how often it happened.

I also wanted to see if it was WIFI, or entire network that was the problem so i had multiple computers using different connections to the network. So i could run the script and compare logs

 

So here is the bash script:

#!/bin/bash

WGET="/usr/bin/wget"
DELAY=60 # every 60 sec

while true; do
TIME=
$WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
if [ ! -s /tmp/google.idx ]
then
echo `date` "--- Not Connected..!" 2>&1 | tee -a connectivity.log
else
echo `date` "--- Connected..!" 2>&1 | tee -a connectivity.log

fi
sleep $DELAY
done

The script check the internet every 60 seconds and displays date and time with the internet status and logs in to the file connectivity.log

Example output

Mon Sep 10 13:52:31 MDT 2018 --- Connected..!
Mon Sep 10 13:52:37 MDT 2018 --- Connected..!
Mon Sep 10 13:52:42 MDT 2018 --- Connected..!
Mon Sep 10 13:52:48 MDT 2018 --- Connected..!
Mon Sep 10 13:52:53 MDT 2018 --- Connected..!
Mon Sep 10 13:52:59 MDT 2018 --- Connected..!
Mon Sep 10 13:53:04 MDT 2018 --- Connected..!
Mon Sep 10 13:53:10 MDT 2018 --- Connected..!
Mon Sep 10 13:53:15 MDT 2018 --- Connected..!
Mon Sep 10 13:53:21 MDT 2018 --- Connected..!
Mon Sep 10 13:53:26 MDT 2018 --- Connected..!
Mon Sep 10 13:53:32 MDT 2018 --- Connected..!
Advertisements