Applescript and ping

Hi guys,
I need to write an Applescript that allows me to do this:

Ping google. If google answer ping mywebsite 1, otherwise display an error message. Then ping my website 2, if this answer launch safari and connect it, if doesn't answer display an error message.
Please can you help me. I'm really newbie with applescript. Please can you help me.

iMac 20" Aluminium mid 2007, Mac OS X (10.6.5), 4GB Ram

Posted on Nov 16, 2010 8:13 AM

Reply
8 replies

Nov 16, 2010 10:51 AM in response to Lord_Nemesi

This is pretty straightforward, even though your logic path is a little convoluted 🙂

set networkUp to true -- assume the best
try
do shell script "ping -c 1 -t 2 www.google.com"
do shell script "ping -c 1 -t 2 www1"
on error -- network is down
set networkUp to false
display dialog "Oops. Network is down"
end try

if networkUp then
try
do shell script "ping -c 1 -t 2 www.apple.com"
open location "http://www.apple.com/"
on error -- network is down
display dialog "Oops. Site is down"
end try
end if


The idea here is to use AppleScript's try block to catch errors - if the ping succeeds then the script progresses normally, but if it fails then it triggers the 'on error' clause.
So in this case I start off by pinging the two sites (google and www1). If either of these fail I post a 'Network down' message and set a flag indicating that the network is down.

Then, if the network is up (i.e. the error clause didn't trigger) then I use another try block to ping the second web site (in this case www.apple.com). If that ping fails it triggers the second 'on error' clause, otherwise it proceeds to open the URL.

Note that I'm just using 'open location' to open the URL. That won't necessarily fire Safari, depending on your browser preferences (it will use your default browser, so if that's Firefox, Chrome, etc. then that's what will open the URL). If you want a specific browser to open the URL that's easy, too, just a little more code.

Nov 16, 2010 11:48 AM in response to rccharles

I'm not sure I understand the logic behind:

-- Ping google's numeric address. Avoids problems with DNS lookup


For all intents and purposes, DNS is part of a working internet connection, so if DNS is down the test should fail, IMHO.
Besides, Google are known to operate out of many different locations/IP addresses, so that IP address might not be optimal for the user in question (I get a completely different address, for example), or might not even be in service while 'www.google.com' maps to a different address.

Nov 16, 2010 7:29 PM in response to Camelot

Camelot wrote:
I'm not sure I understand the logic behind:

-- Ping google's numeric address. Avoids problems with DNS lookup


For all intents and purposes, DNS is part of a working internet connection, so if DNS is down the test should fail, IMHO.


No. DNS is Domain Network Server. It translates google.com to a numeric address. DNS can be down or confused or hijacked and a numeric ping will work.

There was a weird problem with my 10.2 system that I had to supply my own DNS address. If I didn't I could ping but not surf the web.
Besides, Google are known to operate out of many different locations/IP addresses, so that IP address might not be optimal for the user in question (I get a completely different address, for example), or might not even be in service while 'www.google.com' maps to a different address.

Well, pick some other address.

--------

Above I make a big technical difference. From a practical matter, if the DNS is down, the internet is down. On the other hand, you could try a different DNS server.

I wasn't providing a solution to the original poster. I was providing example code which might prove helpful in a solution.

Robert

Nov 16, 2010 11:55 PM in response to Lord_Nemesi

Camelot wrote:
"Note that I'm just using 'open location' to open the URL. That won't necessarily fire Safari, depending on your browser preferences (it will use your default browser, so if that's Firefox, Chrome, etc. then that's what will open the URL). If you want a specific browser to open the URL that's easy, too, just a little more code. "

Ok I undesrtand but is it possible to launch a browser installed on a USB drive? Thanks a lot again!

Nov 17, 2010 10:16 AM in response to rccharles

No. DNS is Domain Network Server. It translates google.com to a numeric address. DNS can be down or confused or hijacked and a numeric ping will work.


I know that.

From a practical matter, if the DNS is down, the internet is down. On the other hand, you could try a different DNS server


That was my point - as per the original poster's requirement, not being able to ping google.com indicates a failure state. In your example, being able to ping the IP address may succeed when DNS is down, indicating a false indication of network state (or, at least, usability).
In this case the OP is trying to determine the state of internet usability, of which DNS is a component.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript and ping

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.