checking for internet access

hi, hope someone can help with this. I developed a code that checks if there is internet :


		try
			
			do shell script "/usr/bin/curl" & space & "http://www.apple.com"
			
		on error
display dialog  "no internet try again later"
end try

this works just fine, but the other day something happend to my internet, pages never load but internet connection did exist, i run my script to see what will happend in this case and the script hanged on for ever about 30 seconds! so I added to it a time out


		try
			with timeout of 4 seconds
				do shell script "/usr/bin/curl" & space & "http://www.apple.com"
			end timeout
		on error
display dialog  "no internet try again later"
end try

it seam to be the solution but the timeout did nothing script hanged again for about 15 seconds! how can i fix this!

any idea will help!

brian

Hi,

timeout blocks don’t affect shell scripts.
Either you could use curl’s own timeout function or something like this


set testIP to chkUP("http://www.apple.com") 
if testIP then
	display dialog "Internet Connection is UP"
else
	display dialog "Internet Connection is DOWN"
end if

to chkUP(theURL)
	return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP

Hello Stefan

What’s the need for the component « & {dotted decimal form:“”} » in the posted instruction ?

I don’t see any difference in the behavior of :

get ((theURL as URL)'s host & {dotted decimal form:“”})'s dotted decimal form

and

get ((theURL as URL)'s host)'s dotted decimal form

Yvan KOENIG (VALLAURIS, France) samedi 3 novembre 2012 20:23:57

Timeout dont affect shell script… that is good to know! thank you for your answer!!! :smiley:

@Brian

a timeout block affects only code to send and receive Apple Events (e.g. talking to applications).
do shell script doesn’t send Apple Events at all (except when using osascript)

@Yvan

I found this snippet many years ago and saved it somewhere without thinking about the code

Thanks Stefan

With a bit of luck, somebody will give an explanation.
Maybe it was required in old times :wink:

Yvan KOENIG (VALLAURIS, France) samedi 3 novembre 2012 20:35:37

Yvan… how old are you?

Brian

Here’s the explanation:
In case of no internet connection, the record host returns only the keys DNS format and port.
Adding the empty dotted decimal form key “ which does not overwrite an existing one “ makes sure it can be read.

Thank you all!!! :smiley:

Hello.

The dotted decimal form, that was interesting! :slight_smile: (I found it in the ASLG.)

Here is another version, that can be optimized, but it works as it should, the two handlers checks directly if the networks interfaces for cable (en0), and airport(en1) are up.

I am sure somebody is just dying to optimize the sed statements. :wink:


set a to en0Up()
set b to en1UP()
on en0Up()
	try
		return (first word of (do shell script "ifconfig | sed -n '/^en0:/,/^en1:/s/status://p' |sed -n 's/inactive/NO/p;1p' |sed  -n 's/active/YES/p;1p'") as boolean)
	on error
		return false
	end try
end en0Up

on en1UP()
	try
		return (first word of (do shell script "ifconfig | sed -n '/^en1:/,/^vnic0:/s/status://p' |sed  's/inactive/NO/p;1p' |sed  -n 's/active/YES/p;1p'") as boolean)
	on error
		return false
	end try
end en1UP

I was born on 1943/12/31 so I will be 69 soon.

Yvan KOENIG (VALLAURIS, France) samedi 3 novembre 2012 22:57:57

I had the feeling you were not a teenager! any way! thank you a lot in contributing to this topic mr. Yvan! :slight_smile:

Oops

I was so surprised by the question about my age that I forgot to say thank you for the explanation.
This day is a good one, I learnt something.

Yvan KOENIG (VALLAURIS, France) samedi 3 novembre 2012 23:13:19

Hello.

I guess the right solution is somewhere between our lines.

I think my solution to state that there is a valid connection. StefanK’s and brian_donovan’s to test whether a site is up or not. :wink:

So you really need both, to check if the net can give you a connection to a site, or give you the correct answer as to why it fails.

(do shell script "ifconfig | sed -En '/^en0:/,/status/ {/^[^s]+status: / { s/// ; s/inactive/false/p ; s/active/true/p ; } ; }'") as boolean

(do shell script "ifconfig | sed -En '/^en1:/,/status/ {/^[^s]+status: / { s/// ; s/inactive/false/p ; s/active/true/p ; } ; }'") as boolean

Edit: Instead of deleting "status: " explicitly, we could simply lose it in one of the other two substitutions:

(do shell script "ifconfig | sed -En '/^en0:/,/status/ {/^[^s]+status: / { s/.+inactive/false/p ; s/.+active/true/p ; } ; }'") as boolean
-- Ditto the other shell script.

Or, to be really anal, we could hive off a successful “inactive” substitution into a function with a ‘q’ command to stop the “active” one being attempted as well. In this case, we substitute “true” or “false” for the entire line .

(do shell script "ifconfig | sed -En '/^en0:/,/status/ { /^[^s]+status: / { /inactive/ { s/.+/false/p ; q ; } ; s/.+/true/p ; } ; }'") as boolean

. which can also be done directly with a ‘c’ command:

(do shell script "ifconfig | sed -En '/^en0:/,/status/ { /^[^s]+status: / { /inactive/ { c\\'$'\\n''false'$'\\n'' ; q ; } ; c\\'$'\\n''true'$'\\n'' ;  } ; }'") as boolean

I don’t know, but I supposed it’s possible for the status to be something other than “active” or “inactive” while a connection attempt is in progress, in which case it would be safer to check for “active” and return “false” for anything else:

(do shell script "ifconfig | sed -En '/^en0:/,/status/ { /^[^s]+status: / { /[ :]active/ { c\\'$'\\n''true'$'\\n'' ; q ; } ; c\\'$'\\n''false'$'\\n'' ;  } ; }'") as boolean

@Yvan;

This phrase:

(count (get (("http://www.google.com" as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0 or (count (get (("http://www.google.com" as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0

was posted by Kai Edwards on August 22, 2006. I don’t know whether it originated with him or not. Several modifications of it have occurred since then.

@Adam.

It is still quite amazing! There aren’t much to go on in AppleScript Language guide to make that handler! From ASLG, you would, or at least I would believe that it was something that pertained to the eppc:// protocol only!

:slight_smile:

I was about to figure it out! I found back to this tutorial which is a good resource for the buddying sed-practitioner.

(The first error I had, was not leaving a space in front of the brace). I’ll give it a close study, writing consice sed statements doesn’t hurt! Though I have the same reasoning with writing large sed scripts as I have with perl. What is the point a times, if it is faster to rewrite it, than to read what you did in the first place. (When it has been put aside for a good while. Then it frankly look likes modem noise, both sed and perl.)

Your solution is so good, so that now the interface name can be passed in as a parameter, leaving us with one handler! :smiley:

Thanks!

MBP user? Probably, because in one of my machines there is an en0, en1, en2, en3, en4 and en5. Also in my newest MBP there is only a en0 (for airport) and nothing else.

This is a part of a script I’m using to check local network availability. The ifconfig does only say that the network connection is configured, not that it is working. I prefer to look for the default gateway and also ping to it to look if it’s available. Now you know at least you have a network connection.

set gw to do shell script "netstat -nr | grep ^default | awk '{print $2}'"
if gw = "" then return false
(do shell script "ping -o " & gw & " &>/dev/null && echo true || echo false") as boolean

Still the code above (it’s a part of a longer script of mine) isn’t water tight. Even if you know that your default gateway works it doesn’t say that websites will be transported through this gateway. To catch this issue traceroute is our best friend.

Hello!

I really didn’t know there were such differences. But on my machine, if the cable aren’t in, then en0 drops immediately to inactive. The same goes for airport. It is not enough for something to be plugged in, there has to be transmitted packages. And yes, I know that you just get to know that you are connected to the nearest access point, but at least you know you have an internet connection up to that point.

That’s why I said you needed both of those handlers to see if you have a connection. (To avoid traceroute, to somewhere. ( I have local DNS) ).

It is interesting to know, that they have put Airport onto en0! :slight_smile: I guess that goes for anything Apple, that doesn’t support a cable.

I am back to bed, it is late here.

Pulling out a cable or turning airport off is something else than bad routing. When your routing table is bad the activity will say it’s running while you can’t even ping the actual router. ifconfig just says if the car is running or not, not if you’re driving in the right direction.

edit: going to bed too. living in the same timezone