Script to powercycle modem if ping fails?

Moderator: leecollings

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

I just used a Home Easy plug i had lying around doing nothing. My neighbours (age 60+) don't have home automation stuff.
I am not active on this forum anymore.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

I see that my script has triggered once, this morning at 04:20
Probably has something to do with the DDoS attack at the Ziggo DNS-servers yesterday (and the day before also). But i didn't notice anything (loss of internet or so) this morning, so the script does its job 8-)
The DDoS was repelled by Ziggo around 4:00, so seems logically that there may have been issues that caused the script to trigger at that time.

And for the DNS-servers being unavailable... i was already using different DNS-servers (OpenNIC) so i didn't have any issues during the DDoS-attacks.
I am not active on this forum anymore.
martjah
Posts: 15
Joined: Wednesday 11 June 2014 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to powercycle modem if ping fails?

Post by martjah »

Hello,

I've changed/updated your script so it turn a virtual switch on/off. I like to do everything with blockly :). (and logging of the connection availlability regardless of executing some action)

Code: Select all

#!/bin/bash
FirstIP="8.8.8.8" #Google DNS
SecondIP="208.67.222.222" #OpenDNS Public DNS
IDX="491" #IDX of the outlet that controls power to your modem
DomoIP="192.168.0.204"
DomoPort="8080"

COUNTER=0
while [ $COUNTER -lt 3 ] ## Try 3 times before resetting the modem. Modify as needed.
do
if ping -c 1 -w 5 $FirstIP &> /dev/null ## Determine if second IP-address is reachable.
   then
		echo "First IP responds, we are connected"
			curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=devices&rid='$IDX'' | grep '"Status" : "On",'
				if [ $? -eq 0 ]
					then
						echo "Switch was already on, so nothing has to be done"
					else 
						echo "The switch was off, so we will turn it on"
						curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'
		fi
		exit 1
elif ping -c 1 -w 5 $SecondIP &> /dev/null ## Determine if second IP-address is reachable.
   then
		echo "Second IP responds, we are connected"
			curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=devices&rid='$IDX'' | grep '"Status" : "On",'
				if [ $? -eq 0 ]
					then
						echo "Switch was already on, so nothing has to be done"
					else 
						echo "The switch was off, so we will turn it on"
						curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'		
				fi
		exit 1
else
	let COUNTER=COUNTER+1
fi
done

curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=devices&rid='$IDX'' | grep '"Status" : "Off",'
	if [ $? -eq 0 ]
		then
			echo "Switch was already off, so nothing has to be done"
		else 
			echo "The switch was on, so we will turn it off"
			curl 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=Off'				
	fi
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to powercycle modem if ping fails?

Post by jannl »

Just curious, why wait 5 seconds for the ping to return?
And why not ping the DNS server(s) of your ISP?
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

jannl wrote:Just curious, why wait 5 seconds for the ping to return?
And why not ping the DNS server(s) of your ISP?
You don't want the script to be TOO sensitive.

And pinging Google & OpenDNS is much better than pinging the DNS of your own ISP. If Google or OpenDNS are down, the internet will break :P So we can quite safely assume that it will never occur that both are down.

Few months ago there was a DDoS on the Ziggo DNS-servers. In this case the script would reboot your modem. But if you use OpenDNS & Google DNS like i do, you wouldn't even notice there was a problem with the Ziggo DNS-servers (which are crappy and slow anyway, just use OpenDNS & Google DNS, much more stable and faster responses).
I am not active on this forum anymore.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to powercycle modem if ping fails?

Post by jannl »

Haha ok, ziggo is may be a bad example ;-), but I think(hope) they protected themselves against future ddos attacks.

But still, waiting 5 seconds for a ping to time-out is way too long in my opinion, even when pinging googles dns servers.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

Might be true. But the script is not aimed at speed. It is meant to be run every 15min or so, through cron.
It will run in the background, so those 5 secs of waiting are not interesting.

If you would check the uptime by hand, waiting 5 secs is too long indeed.

But feel free to adapt the script to your own likings :)
I am not active on this forum anymore.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to powercycle modem if ping fails?

Post by jannl »

Not really needed. I can set my router to disconnect at a certain time each day if needed. But normally the connection comes back online automatically.
Evelen
Posts: 234
Joined: Thursday 09 July 2015 12:03
Target OS: Linux
Domoticz version: 2.4538
Location: Norway
Contact:

Re: Script to powercycle modem if ping fails?

Post by Evelen »

why not pung several DNS's and make the script run only if non of them replay
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

Have you read the script :mrgreen: ? That is exactly what i do :lol:
I am not active on this forum anymore.
Evelen
Posts: 234
Joined: Thursday 09 July 2015 12:03
Target OS: Linux
Domoticz version: 2.4538
Location: Norway
Contact:

Re: Script to powercycle modem if ping fails?

Post by Evelen »

ThinkPad wrote:Have you read the script :mrgreen: ? That is exactly what i do :lol:
not until now ;)
User avatar
Siewert308SW
Posts: 290
Joined: Monday 29 December 2014 15:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by Siewert308SW »

Total noob as it comes to bash and such.
But i hope some has a solution for my problem.
I have a new generation Cisco EPC3928AD EuroDocsis modem from ziggo with their custom firmware.
As out off curiosity i wanted to know if i could reboot my modem without the need of a extra wallplug.
After searching, copy and past i managed to create the below script.

It pings Google and if not reachable it will try to reboot my modem.
It does ping and then want to login in my modem.
And here it goes wrong.
As the page which is called is a .asp i get the message that my browser isn't supported as it is lacking java.
How to overcome this, any help is appreciated.
Is it even possible the fill-in a java form true bash?

Code: Select all

#!/bin/sh
ping -q -c1 google.com   /dev/null

if [ $? -ne 0 ] ; then
	rm /var/log/cookie
	curl -L -c /var/log/cookie -d "username_login=admin" -d "password_login=password" -d "login=Log+In" http://192.168.178.1/goform/Docsis_system /dev/null
	curl --cookie /tmp/cookie --referer http://192.168.178.1/Devicerestart.asp -d "mtenRestore=Device+Restart" -d "devicerestart=1" http://192.168.178.1/goform/Devicerestart /dev/null
fi
Setup:
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

You had also responded on my blog, where i told you to change the user-agent that cURL uses.
Then you said that didn't work, which is a pity. I don't know how to work around this Java stuff...

Try looking if your modem has telnet, maybe you can then issue a reboot command with a script that connects to your modem with telnet.

Otherwise just put a 433Mhz / Z-Wave plug between that you control with Domoticz. That's exactly how i did it in my setup.

I'm not using this anymore now, i switched from Ziggo to Telfort. With xDSL it is recommended to leave the modem on for 24/7, otherwise the streetcabinet might think there is something wrong with the copperline and adjust your speed (downwards! :( ) to improve stability.
I am not active on this forum anymore.
User avatar
Siewert308SW
Posts: 290
Joined: Monday 29 December 2014 15:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by Siewert308SW »

ThinkPad wrote:You had also responded on my blog, where i told you to change the user-agent that cURL uses.
Then you said that didn't work, which is a pity. I don't know how to work around this Java stuff...

Try looking if your modem has telnet, maybe you can then issue a reboot command with a script that connects to your modem with telnet.

Otherwise just put a 433Mhz / Z-Wave plug between that you control with Domoticz. That's exactly how i did it in my setup.

I'm not using this anymore now, i switched from Ziggo to Telfort. With xDSL it is recommended to leave the modem on for 24/7, otherwise the streetcabinet might think there is something wrong with the copperline and adjust your speed (downwards! :( ) to improve stability.
No worries mate.
Have been playing all day and it didn't had anything to do with the UserAgent.
I do have it working now, And the first part is based on your script except the reboot process...
Don't ask me why, but the problem was mainly caused by the cookie file which is needed for the ziggo login page.
This file was somehow not readable in /home/pi/domoticz.
So i set it to /var/tmp.
Also one variable for the login page /gotoform was wrong.

Code: Select all

#!/bin/bash
 
#-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
#
#	Ziggo_Modem_Reboot.sh
#	@author: Siewert Lameijer
#	@since: 23-12-2015
#	@version: 1.0
#   @Script for rebooting the ZIGGO Cisco EPC3928AD EuroDocsis 3.0 2-PORT Voice Gateway
#-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
#
#	@Credits to Thinkpad for the first part of the script (Ping and counter)
#
#-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

#--
#-- **********************************************************
#-- Modem Reboot Settings Begin
#-- **********************************************************
#--

 
FirstIP="8.8.8.8" #Google DNS
SecondIP="89.101.251.228" #Ziggo DNS
ModemIP="192.168.178.1" #Modem IP
DomoIP="127.0.0.1" #Domoticz local IP
DomoPort="8080" #Domoticz poort
 
#Modem Credentials
Modem_Username="<username>"
Modem_Password="<password>"
 
Count="5" #Aantal keren dat er gepinged moet worden voordat de modem word gereboot
 
#--
#-- **********************************************************
#-- Modem Reboot Settings End
#-- **********************************************************
#--
 
Counter=0
while [ $Counter -lt $Count ]
do
if ping -c 1 -w 5 $FirstIP &> /dev/null ## Bekijk of het eerste IP-address reageerd op de ping.
   then
   echo "Eerste IP-address reageerd, We gaan niks doen"
   exit 1
elif ping -c 1 -w 5 $SecondIP &> /dev/null ## Bekijk of het tweede IP-address reageerd op de ping.
   then
   echo "Tweede IP-address reageerd, We gaan niks doen"
   exit 1
else
   let Counter=Counter+1
fi
done
 
#Reboot Modem
   echo "Modem reageerd niet, we gaan de modem rebooten"
   curl -L -c /var/tmp/modem_reboot_cookie.txt --referer http://$ModemIP/login_zig.asp -d "Zigloginnaam=$Modem_Username" -d "Zigpassword=$Modem_Password" -d "login=Log+In" http://$ModemIP/goform/login_zig > /dev/null
   curl --cookie /var/tmp/modem_reboot_cookie.txt --referer http://$ModemIP/Devicerestart.asp -d "mtenRestore=Device+Restart" -d "devicerestart=1" http://$ModemIP/goform/Devicerestart > /dev/null
Setup:
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

Nice that you got it working, that's the real spirit :D How did you test if the script was really working? Putting in IP's that will never respond? (LAN range but not assigned to a device yet)
I am not active on this forum anymore.
User avatar
Siewert308SW
Posts: 290
Joined: Monday 29 December 2014 15:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by Siewert308SW »

ThinkPad wrote:Nice that you got it working, that's the real spirit :D How did you test if the script was really working? Putting in IP's that will never respond? (LAN range but not assigned to a device yet)
Yep... once want to work i get it to work ;-)
Indeed, i put in two IP-Addresses on the internet and not lan based which will never respond.
But i have the feling i'm missing something in the check, don't know what it could be...

[edit]
i know what i'm missing. ;-)
It's a cooldown periode, i mean when the modem is rebooted it could take up to 15 min to get a internet connection.
Setup:
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to powercycle modem if ping fails?

Post by ThinkPad »

If you run the script every 15 min through a cronjob, the modem has time enough to make the internet connection i think :)
I am not active on this forum anymore.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest