iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

[ SOLVED ] how to verify internet connection

Mon Feb 17, 2020 9:35 am

Hi all,

my router seems to not work perfectly ...

so i would like to do a little script to verify if my pi can access internet , if no reboot my router .

i ' ve found a script to reboot my router , so this part is OK .

i ' ve already seen in this forum a script to verify the internet connection , but i can ' t find it anymore :oops:

i would like to test if it is possible to access internet , if no launch the script to reboot my router ( named as : redemarrer.livebox.sh ) :

Code: Select all

#!/bin/bash
curl -o rebootlivebox_context -X POST -i -H "Content-type: application/json" -c ./rebootlivebox_cookies.txt "http://192.168.1.1/authenticate?username=admin&password=mypassword"
ID=$(tail -n1 rebootlivebox_context | sed 's/{"status":0,"data":{"contextID":"//1'| sed 's/"}}//1')
curl -i -b ./rebootlivebox_cookies.txt -X POST -H 'Content-Type: application/json' -H 'X-Context: '$ID'' -d '{"parameters":{}}' http://192.168.1.1/sysbus/NMC:reboot
rm rebootlivebox_cookies.txt
rm rebootlivebox_context

any help would be appreciated
Last edited by iznobe on Mon Feb 17, 2020 3:52 pm, edited 1 time in total.

pcmanbob
Posts: 11946
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: how to verify internet connection

Mon Feb 17, 2020 9:45 am

Here's a short bash script I use to re-establish a wifi connection , which I have changed to do a check for an internet connection.

You just need to add your own code for restarting your router.

Code: Select all

#!/bin/bash

# keep LAN alive
echo "................................................"
date "+%Y.%m.%d %H:%M:%S"
echo " "
ping -c2 google.com 

 
if [ $? != 0 ] 
then 
  echo " "
  echo "No network connection, restarting eth0"
  
# put your router reboot code here
     
fi
echo "................................................"
echo " "
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 10:30 am

thanks for responding and giving your code ;)

questions : what is doing this part of code :

Code: Select all

if [ $? != 0 ] 


i know this a condition , but i don't understand the " ? "

what variable is it please?

tpyo kingg
Posts: 982
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: how to verify internet connection

Mon Feb 17, 2020 10:39 am

Every program has an exit code. If the program succeeded it quietly produces an exit code of 0. If it fails, then it will be a number between 1 and 255. The $? is a shell variable which contains the exit code of the most recent action.

Code: Select all

ping -c 2 www.google.com
echo $?

ping -c 2 skajghlaksfblakhflahgflkahsgf29egfi2hrfeabrfva.com
echo $?

/bin/true
echo $?

/bin/false
echo $?

#and so on
That also means the test can be condensed a little:

Code: Select all

#!/bin/sh

# keep LAN alive
echo "................................................"
date "+%Y.%m.%d %H:%M:%S"
echo " "
if ! ping -c2 google.com; then 
  echo " "
  echo "No network connection, restarting eth0"
  
# put your router reboot code here
     
fi
echo "................................................"
echo " "
So, yes, in the shell 0 is true and 1 through 255 is false.

tpyo kingg
Posts: 982
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: how to verify internet connection

Mon Feb 17, 2020 10:45 am

PS. If you're not overwhelmed by very long reference manuals, take a look at the section "Special Parameters" in the bash manual or the section "Special Parameters" in the POSIX shell manual:

Code: Select all

man bash
man sh
There are some other magic variables there that can come in handy from time to time.

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 10:46 am

Hi and thanks for your explanation ;)

i ' m learning and now i understand , thanks to you :D

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 11:01 am

ok so , i ' ve made a directory and put in two files the first to reboot the router and the second to test internet connection :

Code: Select all

#!/bin/bash

server=pool.ntp.org
#server=google.com
fails=0

for i in 1 2 3 4 5
    do
        ping -c 1 $server > /dev/null && echo ok || ((fails++))
        sleep 1
        done

if (( "$fails" > "1" )); then
    ./redemarrer.livebox.sh
    # lance le script pour redemarrer la live box
fi

echo fails = "$fails"
fails=0
and added this line to my crontab :

*/5 * * * * /home/pi/projets_auto/nouvelle/redemarrer_livebox/test.internet.sh

now , just see and wait , if it is working ;)

tpyo kingg
Posts: 982
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: how to verify internet connection

Mon Feb 17, 2020 11:05 am

Nice. You might want the comparison to be greater than or equal to >= so that an exit code of 1 is covered also.

pcmanbob
Posts: 11946
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: how to verify internet connection

Mon Feb 17, 2020 12:33 pm

You script wont work because you can't use

Code: Select all

 ./redemarrer.livebox.sh
 
when calling a script from a scrip run by cron, you need to replace the " ./ " with the full path to the file you are calling.

so for example if your script was saved in the pi home directory your would use

/home/pi/redemarrer.livebox.sh
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

GlowInTheDark
Posts: 2097
Joined: Sat Nov 09, 2019 12:14 pm

Re: how to verify internet connection

Mon Feb 17, 2020 12:59 pm

You script won't work because you can't use

./redemarrer.livebox.sh
Yes, you can. If your script is stored in your home directory.
when calling a script from cron, you need to replace the " ./ " with the full path to the file you are calling.
No, you don't. Assuming the script is in /home/pi, ./ will work just fine.
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 2:18 pm

thanks for replies .
pcmanbob wrote:
Mon Feb 17, 2020 12:33 pm
You script wont work because you can't use

Code: Select all

 ./redemarrer.livebox.sh
 
when calling a script from a scrip run by cron, you need to replace the " ./ " with the full path to the file you are calling.

so for example if your script was saved in the pi home directory your would use

/home/pi/redemarrer.livebox.sh
thanks for this precision , i put in the absolute path now ;)


i would like to understand .

this part

Code: Select all

$?
saves the result of the last command, but what is the result of this : when for example the first ping is ok and the second does not succeed in the ping command -c2 ?
Last edited by iznobe on Mon Feb 17, 2020 2:23 pm, edited 1 time in total.

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 2:59 pm

finally , for testing , i have changed file test.internet.sh to :

Code: Select all

#!/bin/bash

server=pool.ntp.org
#server=google.com
fails=0

for i in 1 2 3 4 5
    do
        #ping -c 1 $server > /dev/null && echo ok || ((fails++))
        ping -c 1 $server > /dev/null
        if [ $? != 0 ]
            then
            fails++
        fi
        sleep 1
        done

if (( "$fails" > "1" )); then
    /home/pi/projets_auto/nouvelle/redemarrer_livebox/redemarrer.livebox.sh
    # lance le script pour redemarrer la live box
    #heure=$(date +%Y.%m.%d %H:%M:%S)
    echo "redemarrage de la livebox en cours ."
else
    echo "ok"
fi

fails=0

also changed the crontab line to :
*/2 * * * * /home/pi/projets_auto/nouvelle/redemarrer_livebox/test.internet.sh >>/home/pi/projets_auto/logs/log.log 2>&1

GlowInTheDark
Posts: 2097
Joined: Sat Nov 09, 2019 12:14 pm

Re: how to verify internet connection

Mon Feb 17, 2020 3:05 pm

thanks for this precision , i put in the absolute path now
No need, but no harm in doing so.
i would like to understand .

this part

$?

saves the result of the last command, but what is the result of this : when for example the first ping is ok and the second does not succeed in the ping command -c2 ?
There's no general rule. You will have to test - each command can do things differently. Some commands document what exit statuses are possible and what each one means, but, AFAICT, "man ping" contains no information on this topic. So, you'll just have to test it and see.

Usually, though, these sorts of command will consider any error to be an error, so even if, so to speak, it "mostly" worked, but some part of it failed, the command will treat that as a failure.

BTW, a better way is not to deal with worrying about $? at all. Instead, use && or || to conditionally execute code. So, instead of:

Code: Select all

somecommand
if [ $? ... ] ...
do

Code: Select all

somecommand && echo "It worked!"

# or

somecommand || echo "Oh, no!  It failed!"
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

tpyo kingg
Posts: 982
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: how to verify internet connection

Mon Feb 17, 2020 3:07 pm

iznobe wrote:
Mon Feb 17, 2020 2:18 pm
but what is the result of this : when for example the first ping is ok and the second does not succeed in the ping command -c2 ?
If any packets get though it succeeds.

Code: Select all

$ ping -i 3 -c 2 -n 8.8.8.8; echo $?
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=19.7 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 7ms
rtt min/avg/max/mdev = 19.728/19.728/19.728/0.000 ms
0

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 3:24 pm

tpyo kingg wrote:
Mon Feb 17, 2020 3:07 pm
iznobe wrote:
Mon Feb 17, 2020 2:18 pm
but what is the result of this : when for example the first ping is ok and the second does not succeed in the ping command -c2 ?
If any packets get though it succeeds.

Code: Select all

$ ping -i 3 -c 2 -n 8.8.8.8; echo $?
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=19.7 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 7ms
rtt min/avg/max/mdev = 19.728/19.728/19.728/0.000 ms
0
so we can say this code :

Code: Select all

ping -c2 google.com 
 
if [ $? != 0 ] 
then 
#reboot code here
fi
and this one

Code: Select all

for i in 1 2 
    do
        ping -c 1 google.com 
        if [ $? != 0 ]
            then
            fails++
        done

if (( "$fails" > "1" )); then
#reboot code here
fi
do the same thing ?

they reboot if there is 2 ping errors ?

tpyo kingg
Posts: 982
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: how to verify internet connection

Mon Feb 17, 2020 3:38 pm

The both run the reboot section if there are two ping errors. They just go about it in two different ways.

One thing to notice is that the comparison operators = and != are for strings. For integers, it would be -eq and -ne correspondingly. See "man test" for the full range of options.

iznobe
Posts: 706
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: how to verify internet connection

Mon Feb 17, 2020 3:44 pm

tpyo kingg wrote:
Mon Feb 17, 2020 3:38 pm
The both run the reboot section if there are two ping errors. They just go about it in two different ways.

One thing to notice is that the comparison operators = and != are for strings. For integers, it would be -eq and -ne correspondingly. See "man test" for the full range of options.
ok thanks for all this precisions about the shell code ;)

so finally i think i 'm going to use this file :

Code: Select all

#!/bin/bash

ping -c2 google.com  > /dev/null

if [ $? -ne 0 ]
    then
        echo "No network connection , restarting livebox"
        /home/pi/projets_auto/nouvelle/redemarrer_livebox/redemarrer.livebox.sh
        # lance le script pour redemarrer la live box
    else
        echo "ok"
fi

Return to “Networking and servers”