Telling when the internet connection is on or off...

Posted on
Sat Nov 21, 2009 12:45 pm
berkinet offline
User avatar
Posts: 3427
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Trace route

BlkSwrd wrote:
OK, so I ran the Trace route using my Network Utility app and got this:...
traceroute to www.perceptivesystems.com [74.117.222.18], 64 hops max, 40 byte packets
1 10.0.1.1 (10.0.1.1) 1.370 ms 0.425 ms 0.253 ms
2 adsl-75-26-175-254.dsl.scrm01.sbcglobal.net (75.26.175.254) 8.800 ms 10.411 ms 11.766 ms
...
...
I'm assuming that number 2 (75.26.175.254) is what I want to use for my pinging purposes.... right? If so, I think I'm ready to try this puppy!
You got it. 75.26.175.254 is AT&T's router in downtown Sacramento. Good luck with the AppleScript.

Posted on
Sat Nov 21, 2009 4:18 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

(No subject)

Berkinet,

OK, Final check. Here's my prototype script for checking my network connection and resetting my network if the pinging brings back anything but a 0% lost packets (Please check my syntax... I'm a bit shaky on that):


Prototype Ping Internet Check Script


try
set this_result to do shell script ("/sbin/ping -c10 75.26.175.254|grep -c time=") as string
if this_result does not contain "0%" then
execute group "Reset Internet / Network Router"
log "Connection failure!"
end if
on error error_message
execute group "Reset Internet / Network Router"
log "Connection failure!"
end try


Now if I have this right, set up a Time / Date command in Indigo to check every 10 to 15 minutes or so by executing this script to check on my connection. As long as I return a string with a "0%" set of characters in it, I'm fine... anything else resets the network until the ping comes back with no loss.

If you think this is a GO, then I'm up and running.... and THANK YOU (berkinet, Matt, Jay, and anyone else I may have inadvertently forgotten) for all your help!

OH, P.S.

Using the Network Utility App, I pinged the router and got

Ping has started ...

PING 75.26.175.254 (75.26.175.254): 56 data bytes
64 bytes from 75.26.175.254: icmp_seq=0 ttl=63 time=6.941 ms
64 bytes from 75.26.175.254: icmp_seq=1 ttl=63 time=7.303 ms
64 bytes from 75.26.175.254: icmp_seq=2 ttl=63 time=7.446 ms
64 bytes from 75.26.175.254: icmp_seq=3 ttl=63 time=6.680 ms
64 bytes from 75.26.175.254: icmp_seq=4 ttl=63 time=7.297 ms
64 bytes from 75.26.175.254: icmp_seq=5 ttl=63 time=7.298 ms
64 bytes from 75.26.175.254: icmp_seq=6 ttl=63 time=7.448 ms
64 bytes from 75.26.175.254: icmp_seq=7 ttl=63 time=7.139 ms
64 bytes from 75.26.175.254: icmp_seq=8 ttl=63 time=6.855 ms
64 bytes from 75.26.175.254: icmp_seq=9 ttl=63 time=7.334 ms

--- 75.26.175.254 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss
round-trip min/avg/max/stddev = 6.680/7.174/7.448/0.250 ms


so in there is a "0%" set of characters which is what states that everything is OK... right. Sorry for the seemingly obvious questions but I'd rather make sure so I can stop buggin ya'll.

Yours,

Marcus

"Not everything that counts is counted, and not everything that's counted counts."

---Alber Einstien

Posted on
Sat Nov 21, 2009 5:19 pm
berkinet offline
User avatar
Posts: 3427
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

(No subject)

BlkSwrd wrote:
...so in there is a "0%" set of characters which is what states that everything is OK... right?
Not exactly. If you run just the shell script line in the Apple Script Editor (and open the Events & Results panes) you will get:
Code: Select all
tell current application
   do shell script "/sbin/ping -c10 75.26.175.254|grep -c time=" as string
      --> "10"
end tell
Result:
"10"
But, your AppleScript is looking for 0% and you will never find it. This is because in the do shell script command, the output of the ping command is sent as input (the "|" symbol) to the grep command. grep then counts (the -c argument) the number of lines containing the string "time=" (which indicates a successful ping response). Then grep returns the count to your AppleScript.

The reason for this is that even if you get only 1 packet out of 10 through to your ISP, it is highly probable that the equipment at your end is running properly, and no reset is required. So, you need to test the result to see if the response is greater than zero. Something like this:
Code: Select all
tell application "IndigoServer"
   try
      set this_result to do shell script ("/sbin/ping -c10 75.26.175.254|grep -c time=")
      if this_result = 0 as integer then
         execute group "Reset Internet / Network Router"
         log "No ping response received (0 of 10)"
      end if
      
   on error error_message
      execute group "Reset Internet / Network Router"
      log "Command failure testing network connection: " & error_message
   end try   
end tell
will do what you want.

However, you can simplify this to:
Code: Select all
tell application "IndigoServer"
   try
      set this_result to do shell script ("/sbin/ping -c10 75.26.175.254|grep -c time=")
   on error error_message
      execute group "Reset Internet / Network Router"
      log "Command failure testing network connection: " & error_message
   end try
end tell
since the shell script will return an error if there are no responses received. So, there is really no need to actually look at the count.

Finally, if you are running this as an embedded script, you can trim it down fi=urther to just:
Code: Select all
try
    set this_result to do shell script ("/sbin/ping -c10 75.26.175.254|grep -c time=")
on error error_message
    execute group "Reset Internet / Network Router"
    log "Command failure testing network connection: " & error_message
end try
since the tell block isn't necessary inside Indigo.

You can test your script by changing the IP Address you ping to: 75.26.175.253 since that address does not exist and no responses will be received.

Posted on
Sat Nov 21, 2009 8:15 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

Checking...

Berkinet,

Thanks lots! Now I understand what your version of the shell script does and that makes a lot more sense to me. I'm gonna try the script you wrote as a separate script called up from a Time/Date Action group that'll run every 15 minutes or so. I'll get back to you in the next few days as to how well it seems to work. Thanks!

P.S.

I'll just disconnect my Mac Mini from the net some time and see if that has the same effect as changing the IP Address for pinging... I figure that's a bit easier. Thanks...

Yours,

Marcus

"Not everything that counts is counted, and not everything that's counted counts."

---Alber Einstien

Posted on
Mon Aug 31, 2020 12:53 pm
thomasw offline
Posts: 135
Joined: Feb 13, 2011

Re: Telling when the internet connection is on or off...

I know this is an older post, but someone might need it. I needed to do the same thing, have Indigo check every 15 min to see if Internet connection was working. If not, turn off the z-wave power module I have the modem connected to. Then after 4 min, have it turn the modem back on to re-establish Internet connection.
I found the ping method Berkinet listed here to be the only way I could get it to work (but I had to modify it). I did the above mentioned trace route first, got the ISP IP number. Then I made a schedule event in Indigo that runs an Action Group every 15 minutes. I named the Action Group "Check Internet Connection". In this group, I used an Applescript that is a bit simpler than what is above in this topic. (I couldn't get anything else to work). Note: I am still using Indigo 6 pro and it is still running on 10.6.8.
(I will upgrade Indigo to 7 when I get a LOT of spare time to move it to one of my newer macs!)
Anyway, here are the screen shots in case anyone else needs to do this. It's working well for my setup.
Attachments
Screen shot 2020-08-31 at 2.44.24 PM.png
Screen shot 2020-08-31 at 2.44.24 PM.png (64.57 KiB) Viewed 396 times
Screen shot 2020-08-31 at 2.41.21 PM.png
Screen shot 2020-08-31 at 2.41.21 PM.png (149.24 KiB) Viewed 396 times

Posted on
Wed Sep 02, 2020 1:55 am
berkinet offline
User avatar
Posts: 3427
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Telling when the internet connection is on or off...

thomasw wrote:
I know this is an older post, but someone might need it. I needed to do the same thing, have Indigo check every 15 min to see if Internet connection was working. If not, turn off the z-wave power module I have the modem connected to. Then after 4 min, have it turn the modem back on to re-establish Internet connection. ...
Glad to see this worked for you. 1 tip, there is really no longer a need to use an AppleScript. Indigo now supports the direct execution of Shell Scripts. To force the router reboot, you could have the shell script use the Indigo RESTful API to execute an action, or set a variable, etc. Also, one other comment. I don't think you need a 4-minute delay before restarting the router. In most cases, 15 to 30 seconds is sufficient to be sure the capacitors in the power supply have fully discharged and the device has shut down completely.

Posted on
Wed Sep 02, 2020 8:01 pm
thomasw offline
Posts: 135
Joined: Feb 13, 2011

Re: Telling when the internet connection is on or off...

Thanks Berkinet, I didn't know that. Once I have the time to move Indigo over to a new mac and newer OS, I'll give that a try.

Who is online

Users browsing this forum: No registered users and 1 guest