WAN test script - quick and dirty

I wanted to test my WAN connection so I can track internet outages. The thought is that if I get something like the Aeotec Z-Wave Outlet I could setup an automation that would power cycle my cable modem and/or router. Just wanted to share. I’m on Hassbian so your config may be a little different.

  1. Create a script (e.g. wan_test.sh) with the following in it. I put it in my HA config directory
#! /bin/bash
WGET="/usr/bin/wget"
$WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
if [ ! -s /tmp/google.idx ]
then
  echo "Down"
else
  echo "Up"
fi
  1. Make the script executable.
chmod +x wan_test.sh
  1. Change owner and group so homeassistant user can execute it
sudo chown homeassistant wan_test.sh && sudo chgrp homeassistant wan_test.sh
  1. Create a binary sensor
binary_sensor:
  - platform: command_line
    name: WAN
    command: "/home/homeassistant/.homeassistant/wan_test.sh"
    device_class: connectivity
    payload_on: "Up"
    payload_off: "Down"

Example:
WAN_sensor

1 Like

you could just use one of the ping components and ping some external server…maybe ping your own external IP

Agreed, if ICMP is enough for you to test your WAN then it would work, but you are making some assumptions. I see many network admins disabling ICMP for fear of ping flood attacks originating from their network.

I mean you could just ping one of Google’s nameservers (e.g. 8.8.8.8) but then your only testing IPv4 connectivity. Or you could ping google.com and test IPv4 and name resolution. I wanted to take it a step further and test a full web request as it’s a more accurate representation.

I guess it would be better to write a test script that tests all three options since your name resolution could be down while your internet connection is still up. That would help pinpoint the issue rather then just telling you there is an issue.

Good Point.

I like your idea (test all 3)

Your script needs to remove the google.idx after it runs or it will indicate up for any subsequent runs even if down.

Thanks, updated:

#! /bin/bash

WGET="/usr/bin/wget"

$WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
if [ ! -s /tmp/google.idx ]
then
  echo "Down"
else
  echo "Up"
  rm -f /tmp/google.idx
fi

I have something similar, however my script runs on my HA server, pings google, if no response it does a telnet to the router and does a reboot that way.

I’m not a fan of a hard reboot on the router, that the router is unaware of.

Also some routers support just a restart of the WAN interface, might be a telnet option for this.

Hi, Nice work…
I’m using small Custom component i created using fping.
works like charm and very fast.

make sure to install fping
sudo apt install fping

and this is the sensor entry in configuration.yaml:

  • platform: device_status
    scan_interval: 10
    devices:
    internet_connection:
    host: 8.8.8.8 8
    name: “Internet Connection”

@Robbrad I’m interested in setting up something similar would you mind sharing your setup. I’m a noob so I’m still learning. Thanks in advance.

Just in case you didn’t know: the DNS IP sensor turns its state to Unknown if name resolution fails. So you could have 2 sensors (IPv4 and IPv6) that indicate DNS issues if such an entity is in the unknown-state for time X.

My setup is slightly different to yours maybe - but here goes

Internet -> Billion 8800NL -> Linksys WRT3200 ACM

The Linksys is running Lede

Billion is in Bridge mode - Linksys does the connection (I have this so my Linksys can stay as my main router even if I change my modem)

On the HA Server I have 3 scrips - A master script and one for each device

And a crontab that runs hourly

0 * * * * /home/user/scripts/restart_inet_connection.sh >> /home/user/scripts/inet.log 2>&1

Main Script

#!/bin/bash
fping -u google.co.uk >& /dev/null
if [ $? -ne 0 ]; then
        echo "Date: $(date)"
        echo "$(date): Google.co.uk is not responding!! Attempting to repair internet connection."
        echo "$(date): Restarting Billion 8800NL"
        /home/user/scripts/restart_billion.sh 192.168.0.254 username password
        sleep 10s
        while ! fping -q 192.168.0.254 ; do echo "Billion has not rebooted yet"; sleep 10s ; done
        echo "$(date): Billion is online. Waiting 1 minute for ADSL to sync"
        sleep 1m
        echo "$(date): Restarting Linksys 3200"
        /home/user/scripts/router_reboot.sh 192.168.0.1
        sleep 10s
        while ! fping -q 192.168.0.1 ; do echo "Router has not rebooted yet"; sleep 10s ; done
#       while ! fping -q google.co.uk ; do echo "Internet is not responding yet"; sleep 10s ; done
        echo "$(date): Google.co.uk is now responding - Internet is now active again."
else
        echo "$(date): Google.co.uk is responding - Internet is active."
fi

On the Billion its running Telnet

Billion Script

#!/usr/bin/expect
#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name
#The script expects login
expect "Login:"
#The script sends the user variable
send "$user\r"
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password\r"
#This hands control of the keyboard over two you (Nice expect feature!)
#interact
#Reboot
expect "> "
# Change the following value to whatever your devices reboot command is.
send "reboot\r"
interact

On the Linksys running LEDE I have a SSH pre-shared key with the HA server - LEDE is just Unix

Linksys 3200

#!/bin/bash

if [ "$#" -ne 1 ]; then
    echo "Illegal number of parameters";
    echo "Usage: $0 ROUTER_IP";
    exit;
fi


RouterToCheck=$1;

command="ssh root@${RouterToCheck} \"ifdown wan; sleep 5; ifup wan\""

result=`eval ${command}`

It’s all a bit convoluted but it checks is the WAN is up and reboots the right devices in the right order. Feel free to change for your devices (I had it working on a different device before I got the Billion and the Linksys)