Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to alert on network re-availability Network
I had a DSL outage this afternoon - Being a total geek I whipped-up this applescript to alert me when the Network came up again. Simple, but effective. Of course, if your net connection goes down and you want to use it, you won't be able to copy and paste from here -- so you'd better copy it now :)

The script needs to be saved as an "Application" and it should "Stay Open" (there are options for both of those in Script Editor's "Save As..." menu item). The return 120 line means that the script checks the network every two minutes (120 seconds); change this if you'd prefer a different interval.

on idle
  try
    do shell script "curl www.apple.com"
    say "Your DSL is now available"
    display dialog "DSL now available."

    on error
      -- say "DSL still down."
  end try
  return 120
end idle
[robg adds: Of course, since this script tests for any network connection, it's not restricted to testing your DSL connection; just change the text to match your connection type.]
    •    
  • Currently 2.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[22,924 views]  

An AppleScript to alert on network re-availability | 13 comments | Create New Account
Click here to return to the 'An AppleScript to alert on network re-availability' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
If behind a proxy server ...
Authored by: tbolt502 on Mar 01, '04 02:15:13PM
Just a wee pointer, if you are behind a proxy server, these settings will not transfer to this shell script and it seems it must be explicitly included.

on idle
  try
    do shell script "curl --proxy name_or_IP_of_proxy_server:port www.apple.com"
    say "Your DSL is now available"
    display dialog "DSL now available."

    on error
      -- say "DSL still down."
  end try
  return 120
end idle
(The "-x" flag does the same as "--proxy" as well.)

If anyone knows how to get Apple Scripts to pick up the network environement, please let us know!
Cheers.

[ Reply to This | # ]

If behind a proxy server ...
Authored by: tbolt502 on Mar 01, '04 02:22:20PM
Another addition.

In a previous hint, the author had iTunes turn down the volume before speaking.

I think this was a nice touch and use it often in scripts.
This would then become:


on idle
  try
    do shell script "curl www.apple.com"
		tell application "iTunes"
			set sound volume to 15
			delay 1
			say "Your Network Connection is now available"
			display dialog "Network Connection now available."
			delay 1
			set sound volume to 75
		end tell

    on error
      -- say "DSL still down."
  end try
  return 120
end idle
Cheers.

[ Reply to This | # ]
If behind a proxy server ...
Authored by: geordie on Mar 01, '04 06:40:41PM
Thanks for the hint about lowering the volume. However, no offense, that is just wrong to do it. You should never reset something to a different value than what it was before you started. Use the lines:

   set oldVolume to sound volume
   set sound volume to 15
   ...  say stuff ....
  set sound volume to oldVolume
I would actually probably set it to 20% percent of the previous volume instead of static value of 15 in the second line as well, but that is more a personal taste thing.

[ Reply to This | # ]
Updated
Authored by: Diggory on Mar 02, '04 10:18:07AM
Thanks for the tips, here's an updated script:

on idle
	try
		set curlScript to "curl www.apple.com"
		--	Uncomment the following line if you use a proxy server.
		--		set curlScript to "curl --proxy address_of_your_proxy_server:port www.apple.com"		
		do shell script curlScript
		tell application "iTunes"
			set oldVolume to sound volume
			set sound volume to (sound volume * 0.25)
			delay 1
			say "Your Network Connection is now available"
			delay 1
			set sound volume to oldVolume
			display dialog "Network Connection now available."
		end tell
		quit
	on error
		--		say "Network still down."
	end try
	return 120
end idle

---
*****
monkeyfood software - http://www.monkeyfood.com

[ Reply to This | # ]

or use scutil
Authored by: _lenny_ on Mar 02, '04 03:10:21PM

one could also use:

/usr/bin/scutil -r <hostname or ip address>

-or-

/usr/bin/scutil -vr <hostname or ip address>

to get verbose information.

with the -r option, scutil will output various strings depending on your current network state, so play around with it and your network settings to see the various output messages. scutil is basically an interface to configd.



[ Reply to This | # ]
Some, uh, "tweaks"!
Authored by: sinjin on Mar 02, '04 04:26:10PM
I love scripts like these, so much fun to play around with. I've modified the code somewhat. It is now more encapsulated (so that iTunes is turned down if the network is up or down) and determines if iTunes is running at all before turning it's volume down; otherwise the script would launch iTunes for no reason (I don't always have iTunes running). Oh, and now the applescript insults you. Vary to tastes! ;-)

on idle
	set availText to "Your Network Connection is now available, geek."
	set unavailText to "The network is still down, you loser."
	try
		set curlScript to "curl www.apple.com"
		--      Uncomment the following line if you use a proxy server.
		--              set curlScript to "curl --proxy address_of_your_proxy_server:port www.apple.com"                
		do shell script curlScript
		my networkStatus(availText)
		quit
	on error
		my networkStatus(unavailText)
	end try
	return 120
end idle

on networkStatus(speakText)
	tell application "System Events"
		if (exists process "iTunes") then
			tell application "iTunes"
				set oldVolume to sound volume
				set sound volume to (sound volume * 0.25)
				delay 1
				say speakText
				delay 1
				set sound volume to oldVolume
				--display dialog "Network Connection now available."
			end tell
		else
			say speakText
		end if
	end tell
end networkStatus


[ Reply to This | # ]
Version 3
Authored by: Diggory on Mar 03, '04 07:39:20AM
It would be nice if someone who uses proxies could check to see if it works as expected.

on idle
	set secondsToWaitBetweenChecks to 120 -- 2 Minutes between checks
	
	set availText to "Your Network Connection is now available."
	set unavailText to "The network is still down."
	
	set hostToCheck to "www.apple.com"
	set checkNetScript to "scutil -r " & hostToCheck
	
	
	try
		set netStatus to do shell script checkNetScript
		if netStatus is "Reachable" then
			my announceText(availText)
			activate
			quit
		else
			--		my announceText(unavailText)
		end if
	on error
		display dialog "Could not check the net status this time." buttons {"OK"} default button 1 giving up after 5 -- seconds
	end try
	return secondsToWaitBetweenChecks
end idle




to announceText(textToSpeak)
	tell application "System Events"
		
		--	If iTunes is running dip the Audio.
		if (exists process "iTunes") then
			tell application "iTunes"
				set oldVolume to sound volume
				set sound volume to (sound volume * 0.25)
				delay 1
				say textToSpeak
				delay 1
				set sound volume to oldVolume
			end tell
		else
			say textToSpeak
		end if
		
		--	If the User has Salling Clicker show a funky alert.
		if (exists process "SEC Helper") then
			tell application "SEC Helper"
				show screen message textToSpeak
			end tell
		else
			display dialog textToSpeak
		end if
		
	end tell
end announceText

---
*****
monkeyfood software - http://www.monkeyfood.com

[ Reply to This | # ]

Proxies...
Authored by: amacaulay on Mar 07, '04 06:47:05AM

There are options for controlling how you connect to a proxy, but these are more for when you _do_ want to and won't help you bypass the transparent proxies that many ISPs use.

However, if you use the --header option to send an appropriate Pragma: header (perhaps someone who understands this part of http can leap in here), then a well behaved proxy should let you pass through.

That said, the original script was to test whether your line was up, so as long as you don't run a home proxy server (or have one on site between your machine and the outside Internet) then why bother? A returned page must have come over your Internet line, so your line must be up.

Andy.



[ Reply to This | # ]
Perl-alternative
Authored by: unforeseen:X11 on Mar 15, '04 01:58:43PM
I like the Idea of the Script, and since I don't know any applescripting and prefer typing some letters to starting an application hidden elsewhere, I wrote a simple Perl-Script to which I aliased 'net', so typing 'net' in the Shell does the same for me. if someone's interested, here's the code.
#!/usr/bin/perl

# checks for network-availability and quits on success

$sleep = 120;             # in seconds
$url = 'http://www.apple.com';

print "testing...";

while(length `curl --connect-timeout 5 --head --silent $url` 
     

[ Reply to This | # ]
Perl-alternative second effort
Authored by: unforeseen:X11 on Mar 15, '04 02:02:35PM
Sorry, that little "<" messed my previous post, so here it is in (hopefully) full length: I like the Idea of the Script, and since I don't know any applescripting and prefer typing some letters to starting an application hidden elsewhere, I wrote a simple Perl-Script to which I aliased 'net', so typing 'net' in the Shell does the same for me. if someone's interested, here's the code.
#!/usr/bin/perl

# checks for network-availability and quits on success

$sleep = 120;             # in seconds
$url = 'http://www.apple.com';

print "testing...";

while(length `curl --connect-timeout 5 --head --silent $url` < 4) {
        ($sec, $min, $hr) = localtime;
        $min =~ s/^(\d{1})$/0\1/;
        $sec =~ s/^(\d{1})$/0\1/;
        print "\rNetwork Connection NOT available @ $hr:$min:$sec \tsleeping ~$sleep seconds | press CTRL-C to abort";
        sleep $sleep;
}

print "\n\a####\nyour Network Connection is up and running.\n####\n";

exit;
bye. unforeseen

[ Reply to This | # ]
Perl-alternative third effort
Authored by: unforeseen:X11 on Mar 15, '04 02:12:14PM
this gives me the creeps, there are some Backslashes missing. Sorry, I didn't use the code-option... line 12:
$min =~ s/^(\d{1})$/0\1/;
line 13:
$sec =~ s/^(\d{1})$/0\1/;
line 14:
print "\rNetwork Connection NOT available @ $hr:$min:$sec \tsleeping ~$sleep seconds | press CTRL-C to abort";
line 18:
print "\n\a####\nyour Network Connection is up and running.\n####\n";


[ Reply to This | # ]
Much easier way
Authored by: jonbauman on Mar 18, '04 04:33:35PM

There's really no need to go to the trouble of running an application that uses your Internet connection to confirm it's presence. Setting up the TCP connection and all is really unnecessary, since the kernel already knows if it has an active connection; just ask ifconfig. To check if your wired ethernet is connected, just do


(do shell script "ifconfig en0 | awk '/inet/ {print $2}'") is not equal to ""
To check your wireless, change en0 to en1 (probably, it could be different depending on your system config). Other interfaces can be checked similarly. If you want to check for IPv4 only, change /inet/ to /inet /, excluding /inet6/. It's probably apparent how to check for IPv6. As a bonus, you can extract your IP address this way, in case you wanted to use it for something.

---

jon

[ Reply to This | # ]

An AppleScript to alert on network re-availability
Authored by: m3llo on Jan 25, '13 02:34:35AM

I'm not that clued up with scripts, but I was wondering if one could use the base of this script to do the following: I'd like to be notified when certain (static ip addresses) devices enters the network, maybe using scripts to ping those addresse(s), but only getting notified if they enter?

Thanks!



[ Reply to This | # ]