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

Posted on
Fri Nov 13, 2009 6:46 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

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

OK, so I get that I can use an event in Indigo to ascertain when the controller is initialized and if there is a possible power outage. Now, since sometimes I have to re-initialize my network after an outage because the Time Capsule and the DSL Modem didn't quite talk to each other nicely, how can I monitor if there is a working internet connection?

Is there a way to monitor the prism reflector and have it's status show up as a variable state?

Is there a way to monitor my Time Capsule for problems using the Finder or the Airport Utility along with some Apple Scripting?

I have set up the Time Capsule and the Modem on appliance controllers and Indigo and turn them off then on after a 30 second delay.... is there a way to monitor some condition so that after a power outage I can have Indigo check the status of the network's connection to the net and restart things if it's not right?

Guys, girls, ya'll out there... I am very much open to suggestions. Thanks.

Marcus

Yours,

Marcus

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

---Alber Einstien

Posted on
Sun Nov 15, 2009 8:33 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

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

Hello Marcus,

Take a look at this forum thread. I believe it will help get you started.

Image

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

Thanks Matt... I may have some more questions...

Matt,

Thanks for the link to the previous thread. As you know, I'm still learning some scripting and don't feel too confident yet so I still have some questions. I have set up an action group to turn off and on the modem and the router. If I use the shell script to check the internet connection, how might I trigger the action group in the apple script? I've titled it "Reset Internet / Network Router" in the Action Groups menu. Do I then send a 'tell' to Indigo to execute the action group? Can you give me an example of the shell script with the action group tell just to make sure I'm thinking correctly. Thanks again for the help.

Marcus

Yours,

Marcus

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

---Alber Einstien

Posted on
Tue Nov 17, 2009 8:17 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Thanks Matt... I may have some more questions...

Hi Marcus,

To execute your Action Group from an AppleScript you would do this:
Code: Select all
execute group "Reset Internet / Network Router"

Additionally, here is Indigo's complete AppleScript dictionary.

Image

Posted on
Tue Nov 17, 2009 6:53 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

Let's see if I have this now...

Matt,

OK, so if I get this straight, the script will look something like this:
Code: Select all
try
   set this_result to do shell script ("/sbin/ping -c 1 " & this_URL) as string
   if this_result does not contain "0%" then
      execute group "Reset Internet / Network Router"
      log "Connection Failure!"
   end if
end try


Then I am guessing that you set up a Time/ Date action to execute this script every so often ( say every 10 or 15 minutes) to check you connection and reset the network if the failure occurs. Sound right to you?

Now I'm not so sure of the "set this_result..." part as I know almost nothing of Unix shell scripts. Where / who could I check this to be sure this is the correct shell script / format/ etc. ? Thanks... this is heading in the right direction...

Yours,

Marcus

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

---Alber Einstien

Posted on
Thu Nov 19, 2009 10:24 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Let's see if I have this now...

That looks basically correct, but you didn't include the property that defines this_URL that the ping is testing a connection against. And you should probably execute your Action Group in the error handler as well like the original script on the other thread. Something like this:
Code: Select all
property this_URL : "www.apple.com"
try
   set this_result to do shell script ("/sbin/ping -c 1 " & this_URL) 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

The do shell script line looks correct to me. Just test it by unplugging your ethernet cable before you run it to see if it works or not. And yes, I'd create a Time/Date Action that executes the script as an external file (save the script above in the Script Editor, then point Indigo to execute that file) every 15 minutes or so. Don't use the embedded edit field in the Actions panel.

Image

Posted on
Thu Nov 19, 2009 11:07 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Let's see if I have this now...

Code: Select all
property this_URL : "www.apple.com"
try
   set this_result to do shell script ("/sbin/ping -c 1 " & this_URL) 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


This code won't always work anyway - "0%" would also match the line:

4 packets transmitted, 0 packets received, 100.0% packet loss


Which is what Snow Leopard's PING command returns (it changed from Leopard to Snow Leopard).

Also, you can't use www.apple.com because they use Akamai to do load distribution and pings are blocked so it will always fail - it's not a sign that you internet connection is down... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Nov 19, 2009 12:30 pm
oktemplar offline
Posts: 20
Joined: Jan 22, 2009
Location: Newark, OH

(No subject)

I have done something similar to what you are doing with ping, instead, I use curl. Like Jay suggests, ping does not always work due to load distribution being used by some websites.

The script is setup as a time/date action that calls this script every X minutes:
Code: Select all
try
   do shell script "curl www.apple.com"
   --   my LogInternetCheck("Internet is up.", "", 0)   
on error
   RestartAirport()
   
end try

on LogInternetCheck(errText, errString, errNum)
   tell application "IndigoServer"
      try
         if errString is not "" then
            log errText & ":" & errString & "(" & (errNum as string) & ")" using type "Internet Check"
         else
            log errText using type "Internet Check"
         end if
      end try
   end tell
end LogInternetCheck

on RestartAirport()
   my LogInternetCheck("Internet down- restarting.", "", 0)
   do shell script "sudo /usr/sbin/networksetup -setairportpower en1 off" password "1234567" with administrator privileges
   delay 2
   do shell script "sudo /usr/sbin/networksetup -setairportpower en1 on" password "1234567" with administrator privileges
end RestartAirport

The password above should be the administrator password on your Indigo server. The RestartAirport procedure is taking the Airport connection down and bringing it back online which reconnects my Indigo machine to the wireless network (provided of course the router is online).

The reason I wrote this script was because my Indigo server is currently on a wireless network. I had a bad router issue that was causing my internet connection to go down. To deal with this issue on my Indigo Server I implemented the script above which would reset the machine's Airport connection after the router had rebooted essentially. It will log in your event log whenever the connection has been restarted. There is a commented line farther up in the code that indicates the connection is up, I am no longer using that part to prevent extra junk in the event log.

I actually had to do a bit of snooping around to find the airport command so perhaps this will be useful. Also, the curl command is very handy to have in your tool belt as the errors reported are consistent.

I am using this script on Snow Leopard by the way, you will have some minor syntax to change on Airport restart if you are using Leopard or Tiger.

Posted on
Thu Nov 19, 2009 9:50 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

Let's try this again...

OK Matt, Jay, and OKTemplar (whoever you may be),

Let's see if I get this. It seems that I have two options:
1. Try this with the curl command as follows:

try
do shell script "curl www.apple.com"
on error error_message
execute group "Reset Internet / Network Router"
log "Connection failure!"
end try

OR

2. I could ignore the curl script and do the next best thing:

property this_URL : "www.some_website_that_works.com"
try
set this_result to do shell script ("/sbin/ping -c 1 " & this_URL) 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


assuming that I figure a website that I can ping that will allow pings to be returned ( and any good suggestions is most appreciated). Now I should state that I am running Leopard as up to date as I can make it but I haven't put my Mac Mini running Indigo up to Snow Leopard (yet). What are the pros and cons for each of these techniques? By the way, thanks to all of you guys for the help... this rocks!

Yours,

Marcus

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

---Alber Einstien

Posted on
Thu Nov 19, 2009 10:01 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

(No subject)

set this_result to do shell script ("/sbin/ping -c 1 " & this_URL) as string
if this_result does not contain "0%" then
If you choose to go the ping route, I would suggest that you send more that one packet. The nature of IP is that packets can get lost and be resent. With today's networks, lost packets are rarer than they were a few years ago, but an occasional dropped packet is still fairly common. A value like -c10 might serve your needs better.

Also, you probably should ping or try to connect to something close to you, like your ISP's side of your Internet connection. Otherwise you might end up resetting your equipment when the problem is actually on the other side of your ISP.

Posted on
Fri Nov 20, 2009 8:02 am
oktemplar offline
Posts: 20
Joined: Jan 22, 2009
Location: Newark, OH

(No subject)

The advantage of the curl route is that there is less in the way of return to deal with. In your ping method you are having to look for certain responses and parse them. With curl, if it returns an error, you have found the condition, which is inherently easier.

Generally programmers will look for the route with less code, provided it achieves the same goal at the same level of performance, so that's why I prefer the curl route, it just has less steps to deal with.

Posted on
Fri Nov 20, 2009 11:55 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

(No subject)

oktemplar wrote:
The advantage of the curl route is that there is less in the way of return to deal with... ...Generally programmers will look for the route with less code, provided it achieves the same goal at the same level of performance...
That is generally good advice. However, I am not sure it argues more for using a web server connection vs. a ping response as the criteria for determining network availability between a local host and an ISP.

For example, the following shell script call would not require any post-processing in AppleScript
    do shell script "ping -c10 209.204.179.1|grep -c time="
(where a.b.c.d is the IP address of your ISP's router) will return a number between 0 and 10, depending on the number of ping responses received. Basically, 1 response or greater tells you that you have a connection to your ISP, even if it is flakey. But, if it is flakey, a reset is usually not the cure. You probably have telco line problems.

BTW, for some guidance on using ping to test network connections, take a look at the ping man page, which states in part
    When using ping for fault isolation, it should first be run on the local host, to verify that the local network interface is up and running. Then, hosts and gateways further and further away should be ``pinged''.

Posted on
Fri Nov 20, 2009 8:22 pm
BlkSwrd offline
Posts: 14
Joined: Apr 04, 2007
Location: California

Which IP to use to ping?

OK, I'm not the most knowledgeable at networking. So, when you state:

"...where a.b.c.d is the IP address of you ISP's router..."

are you saying that I ping my own DSL modem (which doesn't sound right) or the actual router on the other end of the internet connection which gives my DSL modem it's IP Address via DHCP? I think I know the answer but that brings me to another question: How do I find the IP address for my ISP's router to check my connection (because at this time I like the idea of pinging 10 packets and seeing if they all fail before resetting).

I'm in the position of trying to learn on the fly so-to-speak and fill my gaps in knowledge so any help from you guys is MUCH appreciated.

Yours,

Marcus

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

---Alber Einstien

Posted on
Fri Nov 20, 2009 9:39 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

(No subject)

...or the actual router on the other end of the internet connection which gives my DSL modem it's IP Address via DHCP? I think I know the answer but that brings me to another question: How do I find the IP address for my ISP's router to check my connection...
That's it, the first router on the far side of your DSL/Cable link. You can find this address a couple of ways:

1. It should be configured in your router as the "Gateway Address" or something like that.

2. You can try traceroute from your Mac (in a terminal window). For example:
    $ traceroute www.perceptivesystems.com
    traceroute to www.perceptivesystems.com 74.117.222.18 64 hops max, 52 byte packets
    1 192.168.4.1 (192.168.4.1) 0.545 ms 0.098 ms 0.121 ms
    2 209-204-179-1.dsl.static.sonic.net (209.204.179.1) 7.428 ms 8.882 ms 7.886 ms
    3 109.at-4-0-0.gw3.200p-sf.sonic.net (208.106.28.117) 9.920 ms 12.599 ms 9.946 ms
    4 200.ge-0-1-0.gw.equinix-sj.sonic.net (64.142.0.210) 11.720 ms 12.588 ms 11.444 ms
    5 0.as0.gw2.equinix-sj.sonic.net (64.142.0.150) 11.927 ms 11.040 ms 11.861 ms
    6 cr1-eqix-peer.sje007.internap.net (206.223.116.134) 10.550 ms 11.430 ms 11.969 ms
    7 cr2-cr1.sje007.internap.net (66.79.146.190) 31.979 ms 31.535 ms 31.928 ms
    ...
(Note you can kill tracerouote with Ctrl-C once you get past the first few hops.) In the example above, the first hop is inside my LAN to my local gateway (192.168.4.1), the next hop is to my ISP's router (209.204.179.1). That is usually the address you want. You can also tell from looking at the response times. Response times to my own gateway are in the range of 0.120ms while responses from my ISP are in the range of 7 or 8ms. Your numbers will vary, but you should see a clear difference.

Feel free to come back if you have more questions, or need help with the AppleScript logic.

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

Trace route

Berkinet,

OK, so I ran the Trace route using my Network Utility app and got this:

Traceroute has started ...

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
3 dist1-vlan50.scrm01.pbi.net (64.171.152.66) 6.523 ms 14.376 ms 6.657 ms
4 151.164.98.64 (151.164.98.64) 32.508 ms 10.307 ms 16.690 ms
5 ex2-p5-0.eqsjca.sbcglobal.net (151.164.190.68) 16.939 ms 17.110 ms 16.419 ms
6 asn3356-level3.eqsjca.sbcglobal.net (151.164.251.246) 10.299 ms 13.128 ms 16.395 ms
7 vlan89.csw3.SanJose1.Level3.net (4.68.18.190) 33.999 ms 10.794 ms vlan79.csw2.SanJose1.Level3.net (4.68.18.126) 13.523 ms
8 ae-72-72.ebr2.SanJose1.Level3.net (4.69.134.213) 13.533 ms ae-92-92.ebr2.SanJose1.Level3.net (4.69.134.221) 22.404 ms ae-62-62.ebr2.SanJose1.Level3.net (4.69.134.209) 58.437 ms
9 ae-2.ebr2.LosAngeles1.Level3.net (4.69.132.14) 35.755 ms 31.692 ms 28.585 ms
10 ae-3.ebr3.Dallas1.Level3.net (4.69.132.78) 79.580 ms 67.640 ms 64.721 ms
11 ae-7.ebr3.Atlanta2.Level3.net (4.69.134.22) 97.567 ms 91.109 ms 113.027 ms
12 ae-22-52.car2.Atlanta1.Level3.net (4.68.103.35) 86.974 ms 86.292 ms 85.888 ms
13 NationalNet.Atlanta1.Level3.net (4.78.211.162) 84.184 ms 86.891 ms 86.147 ms
14 tengigabitethernet7-4.core1.nationalnet.com (66.115.128.109) 84.734 ms 85.022 ms 90.310 ms
15 tr2.atl1-1-2.national-net.com (66.115.128.30) 91.629 ms 103.949 ms 98.293 ms
16 directnic.distro5.atl5.nationalnet.com (66.115.128.202) 86.965 ms 86.919 ms 86.024 ms
17 74-117-222-82.directnic.com (74.117.222.82) 88.434 ms 86.874 ms 85.678 ms
18 74-117-222-18.directnic.com (74.117.222.18) 95.398 ms !<10> 86.149 ms !<10> 98.099 ms !<10>


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! Thanks either way.

Yours,

Marcus

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

---Alber Einstien

Who is online

Users browsing this forum: No registered users and 22 guests