Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Checking Internet connectivity in startup script?

Sun Jan 13, 2019 10:03 am

As I have described in this thread (as yet not replied to) I need to call a php script on my website when my RPi starts up in order to report the IP address it has received.
So I have written a shellscript, which is called in the startup process, and it works fine if the RPi is booted in a good environment (network is stable).
But if there has been a power outage bringing down the network router too, then the sending fails because the network is not connected to the Internet when the script runs...
So I want to enhance the script such that it can detect if the environment is OK (Internet accessible).

I have googled a bit and found that I should be able to add something like this to the top of the script to make it wait for the network before it executes the curl command towards the php script:

Code: Select all

#!/bin/bash
# Test connectivity to Internet at 1 minute intervals
while [ -z "$ONLINE" ]
do
   ONLINE=$(ping -q -c 1 www.*******.com | grep "1 received")
   if [ -z "$ONLINE" ]
     then
       sleep 60
   fi
done
echo "We are on line!"
#Reporting code follows..
I have tested this as far as I could and it seems to work except for the fact that it fails if the URL used for ping testing does not resolve.
In this case the script echoes the message:

Code: Select all

ping: unknown host bogus.********.com
So it seems like an error inside the ping command will send stuff to the screen, which I don't want.
I expect such a situation to be present when the mobile broadband router has not yet synced up following a power outage and there is no DNS server available.
I need to make sure this won't break the script and it is really hard to check too....
Is there a way to improve the on-line check such that it will not be affected by an error output from ping?
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 10:14 am

You could try something like this

Code: Select all

#!/bin/bash
# Test connectivity to Internet at 1 minute intervals
ONLINE=1
while [ $ONLINE -ne 0 ]
do
   ping -q -c 1 -w 1 www.*******.com >/dev/null 2>&1
   ONLINE=$?
   if [ $ONLINE -ne 0 ]
     then
       sleep 60
   fi
done
echo "We are on line!"
#Reporting code follows..
This checks the exit status of the ping command, not the output.

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 11:55 am

Thank you!
This works as I want for these cases:
OK - URL exists and responds (the one that will be used later in a curl call)
NOK - URL exists but does not respond (my home router set to not respond to ICMP tests)
NOK - URL does not exist so there will be a DNS error

No extraneous output in this case! :)
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 12:50 pm

Bosse_B wrote:
Sun Jan 13, 2019 11:55 am
Thank you!
You're welcome.

It wouldn't be too hard to add a check for "No DNS, but internet connectivity is working" by doing a ping to a fixed IP like 8.8.8.8 (Google DNS server).

PS. Love Sweden. Beautiful country. I have been there twice with my wife in the last three years and hope to go again soon :-)

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 1:48 pm

rpdom wrote:
Sun Jan 13, 2019 12:50 pm
PS. Love Sweden. Beautiful country. I have been there twice with my wife in the last three years and hope to go again soon :-)
Well, Stockholm has sent in its application for the 2026 Winter Olympics on Friday...
Only one competitor, Italy, but they hosted the games in 2006 so maybe February 2026 could be a target?
Of course that is not "soon", but stil...
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 1:56 pm

Sports aren't really an interest of ours, but we did visit a few places in Stockholm like the Vasa Museum and Skansen. We have a few friends there too. I did find it strange that the Swedes were wrapping up warm when it was only just under 5°C and I was wearing a t-shirt most of the time.

User avatar
HawaiianPi
Posts: 5840
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Checking Internet connectivity in startup script?

Sun Jan 13, 2019 7:29 pm

It seems like you found a solution, but I just thought I'd mention the "Wait for Network" setting that Raspbian has under boot options.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Checking Internet connectivity in startup script?

Mon Jan 14, 2019 10:24 am

Yes, I think I have it nailed now.
The application is for a remote installation where the RPi is monitoring the temp/humid in our summer home.
It is located remotely and uses a mobile broadband router.
There are occasional power outages and for these the RPi will start up almost instantly when power returns whereas the router takes a rather long time to start up and connect to the mobile network.
So that is why I need to condition the sending of the boot-up message to the presence of a working Internet connection.
Only having the RPi started up is not enough...
The final working solution is this:

Code: Select all

#!/bin/bash
#Check for initial delay on command line, default to 60 seconds
if [ -z "$1" ]; then
  DELAY="60"
else
  DELAY="$1"
fi
sleep $DELAY

# Test connectivity to Internet at 1 minute intervals, do not continue until Internet is available
URL="www.********.com"
ONLINE=1
while [ $ONLINE -ne 0 ]
do
   ping -q -c 1 -w 1 $URL >/dev/null 2>&1
   ONLINE=$?
   if [ $ONLINE -ne 0 ]
     then
       echo "Failed test to $URL so wait a while and repeat!"
       sleep $DELAY
   fi
done

#Here only if we have an Internet connection, so now check addresses and send message
....
Bo Berglund
Sweden

User avatar
HawaiianPi
Posts: 5840
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Checking Internet connectivity in startup script?

Tue Jan 15, 2019 1:06 am

And this is all so that you can receive a message about the Pi's IP address?

Can't you set or reserve an IP for the Pi (most routers have the ability to reserve an IP address for a device).
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Checking Internet connectivity in startup script?

Tue Jan 15, 2019 9:04 am

Several reasons for the report:
1) To know the IP address in order to connect
All of my RPi units are headless, and they are not always sitting on a network where I can manage the router DHCP reservations.
And sometimes I move them to new networks and so I always put this on my RPi units to help in finding them.

2) To find out if the RPi has rebooted for some unplanned reason (power outage, tampering etc)
This is where I need the extra stuff in the on-boot script to handle the case where the whole building has been experiencing a power outage and not only the RPi so the network may be unavailable for a while.

3) To check that the RPi is still on the network
In this case I have a cron job that reboots the RPi nightly and this causes the message to be sent.
This is usually the case for the RPi units I have deployed as an OpenVPN server for remote network access.
So if the network has been lost for some reason I will not get a message, so I can alert people on location to check up.
A few such instances have happened where the power unit has been disconnected from the outlet or the network cable has been unplugged, just enough to cause network loss. Thanks to the missing report I could alert (my daughter in this case) for a checkup.
Bo Berglund
Sweden

Return to “General discussion”