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.
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
Make the script executable.
chmod +x wan_test.sh
Change owner and group so homeassistant user can execute it
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.
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.
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.
#!/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)