Pinging a server using AppleScript
I’ve recently had a machine that suddenly seems to lose it’s network connection. I can’t figure out why but it always comes back after a reboot. The big problem with this is it’s my backup server and having that off the network is not good. So, I came up with an AppleScript that runs each hour to see if we’re still talking to the world. If not then I reboot the machine.
Here’s the basic script:
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
delay 60
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
do shell script "/sbin/shutdown -r now"
end try
end try
Breaking down the script
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
This executes the command line version of ping to see if we’re on the network. The “-o” means quit pinging as soon as we get a reply. Don’t want to flood Google with pings! The “-c 5” says only send 5 pings at the most.
If there is no response for what ever reason it throws an error. So the “on error” part of the script catches that and tells the script to delay itself for 60 seconds and then continue.
After 60 seconds we try again. If we get an error again the we tell the machine to restart itself using “shutdown -r”.
I run this script as a System cron job, running once an hour. By running as system I don’t have to include any passwords and if it needs to shutdown most likely no active processes can get in the way. I use Cronnix to set up my cron jobs. Saving the job as “ping.scpt” the cron job would look like this:
/usr/bin/osascript /path/to/ping.scpt
webmaster :: Aug.20.2007 :: Applescript, Management, Scripts, System :: 4 Comments »
4 Responses to “Pinging a server using AppleScript”
Nice and useful script! One question though, what if one wanted to add a “mount volume afp://10.192.168.1” command once thePing comes back positive?
Put your “mount volume” command right after the “set thePing” statement. If that statement comes back true (no error) then it will execute the statement after it. If not then it will drop to the “on error” section”
try
set thePing to do shell script “/sbin/ping -o -c 5 http://www.google.com”
mount volume “afp://10.192.168.1”
on error
——-etc., etc.,
I read this site and a few other and wrote my first script for mounting my Net Drive.
The computer tells me what going on. I am new to Mac so this is handy right now, but I am sure one day I will be sick of hearing the compute talk.
set testnet to 1
say “Hi Kev, I am attempting to PING the Net Drive on the network”
repeat with idx from 1 to 5
try
set ping_result to (do shell script “ping -c 1 -t 1 192.168.20.70”)
set testnet to 10
on error
say “Searching”
beep 3
set testnet to (testnet + 1)
delay 10
end try
if testnet = 10 then
exit repeat
end if
end repeat
if testnet = 10 then
delay 1
say “The Net Drive was identified on the Network”
say “I am now attempting to mount the Net Drive volumes”
mount volume “smb://W-NetDrive/Volume_1”
delay 7
say “The Net Drive volumes should now be mounted”
end if
if testnet = 6 then
say “The Net Drive could not be PINGED on the network”
say ” I am unable to mount the Net Drive Volumes”
end if
Hi
I would love to use a script like this to test my internet connection. How do I rewrite the script to ping once every hour and save it to a log file, thus telling me if the internet connection was up at that time.