RPI script to verify internet connection

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
User avatar
Teknor
Posts: 30
Joined: Monday 08 February 2016 13:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.48
Location: Norway
Contact:

RPI script to verify internet connection

Post by Teknor »

Hi
I have a RPI in my summerhouse (weather station, 10x temperature sensors, a few switches and Pikrellcam) for monitoring purposes. I use a stationary 4G router for internet connection. This connection is mostly stable, but occationally (a few times per year) there is need for a reboot of the 4G router. Does anybody have a script which checks for internet access, and if this is down runs a json command?

I think I need something like:

A job runs every 5 minute and checks for internet access
If internet access -> Do nothing
if internet access fails three times in a row, then run http://localhost/json.htm?type=command& ... tchcmd=Off
wait 1 minute
then run http://localhost/json.htm?type=command& ... itchcmd=On

The switch with IDX 46 is connected to a relay powering the 4G router.

Can somebody please point me in the right direction for writing such a script?
Raspberry Pi 3 | EasyESP NodeMCU v3 | RFXTRX433e | Mysensors NRF24L01 | MotionEYE | PikrellCam
User avatar
Teknor
Posts: 30
Joined: Monday 08 February 2016 13:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.48
Location: Norway
Contact:

Re: RPI script to verify internet connection

Post by Teknor »

Found a script I modified. Works like intended.

Code: Select all

#!/bin/ash
FirstIP="8.8.8.8" #(Google public DNS)
SecondIP="208.67.222.222" #(OpenDNS public DNS)
IDX="208"
DomoIP="192.168.1.103"
DomoPort="8080"

ping="ping -c 5 -w 3 -q "$FirstIP""
if $ping | grep -E "min/avg/max" > /dev/null
then
    echo "First IP ("$FirstIP") responds, do nothing"
else
    echo "--> No response from first IP ("$FirstIP"), now trying second one ("$SecondIP")"
      ping="ping -c 5 -w 3 -q "$SecondIP""
      if $ping | grep -E "min/avg/max" > /dev/null
      then
      echo "Second IP ("$SecondIP") is responding, do nothing"

   else
      echo "--> Both IP's not responding, toggling power to modem"
      # Send off command to switch twice to be sure
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=Off'
      sleep 2
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=Off'
      sleep 30

      # Send on command to switch, twice to be sure
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'
      sleep 2
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'
      sleep 2
   fi
fi
Raspberry Pi 3 | EasyESP NodeMCU v3 | RFXTRX433e | Mysensors NRF24L01 | MotionEYE | PikrellCam
Father
Posts: 1
Joined: Tuesday 20 October 2020 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: RPI script to verify internet connection

Post by Father »

Hi Teknor,

I have exact the same problem. But I can't seem to get it to work.
I'm new to scripting so that can be the problem.
I have tried to put it at location for Lua script and I have tried to do it in console
Can you explain how you did it.
Matthiasfr
Posts: 17
Joined: Saturday 10 August 2019 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: RPI script to verify internet connection

Post by Matthiasfr »

Had the exact same problem! Thnx for sharing this!
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: RPI script to verify internet connection

Post by erem »

@father.

this is an os script, not a lua/dzVents script.

first, copy the text into a file called chkrtr.sh and save it somewhere
second, make the file executable with chmod +x chkrtr.sh
third, add the file to cron with crontab -e, add a line like this: */5 * * * * /full/path/to/chkrtr.sh >/dev/null 2>&1

that will run the shell script every 5 minutes

if you do not have the ash shell (almquist shell) installed, change the 1st line of the script from #!/bin/ash to #!/bin/bash
of cause when you want to name the script differently you can.
Regards,

Rob
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests