Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to Ping Server, wait, retry

I have a script that will ping a server and if the server is pingable then it will mount AFPs from that server.

However, if the ping_result is negative, I need to tell the system to wait 5 seconds then retry the ping. And basically keep trying to ping until it's successful.

Does anyone know the correct syntax for scripting this part?

Mac Pro 8 core, 3Ghz, 8GB RAM, Mac OS X (10.4.11)

Posted on Nov 20, 2008 5:29 PM

Reply
Question marked as Best reply

Posted on Nov 20, 2008 5:43 PM

Sure, it's a simple repeat loop.

It would be helpful if you included your existing script as a baseline, but it will end up looking like:

repeat until (do shell script "/sbin/ping -c 2 -q server.net" contains "2 packets received")
delay 5
end repeat
-- mount command goes here


The idea here is that you run the ping command repeatedly until it returns a positive result (i.e. in the above example, it sends 2 pings and they both return). At that point you continue with the remaining commands.

You may also be able to use /sbin/ping -o which pings repeatedly until a reply is received, but this could run indefinitely and is harder to break out of (in the above example you could have additional code inside the repeat loop that fails gracefully after a preset time).
4 replies
Question marked as Best reply

Nov 20, 2008 5:43 PM in response to reelrootsryan

Sure, it's a simple repeat loop.

It would be helpful if you included your existing script as a baseline, but it will end up looking like:

repeat until (do shell script "/sbin/ping -c 2 -q server.net" contains "2 packets received")
delay 5
end repeat
-- mount command goes here


The idea here is that you run the ping command repeatedly until it returns a positive result (i.e. in the above example, it sends 2 pings and they both return). At that point you continue with the remaining commands.

You may also be able to use /sbin/ping -o which pings repeatedly until a reply is received, but this could run indefinitely and is harder to break out of (in the above example you could have additional code inside the repeat loop that fails gracefully after a preset time).

Nov 21, 2008 11:16 AM in response to Camelot

Camelot, thanks for helping with this. Here's the script as it currently exists:
tell application "Finder"
try
set server_Off to "Server Not Available"
set server_On to "Server Mounted!"
set server_AlreadyOn to "Server Already Mounted"
set ping_result to (do shell script "ping -c 1 10.192.168.1")

if "100% packet loss" is in ping_result then
tell system to wait 5 seconds then repeat ping here

end if

mount volume "afp://10.192.168.1"


end try
end tell


So I'm guessing that I would place your suggested code after the "If 100% packet loss" is in Ping_Result then"
correct?

Nov 21, 2008 8:11 PM in response to reelrootsryan

Hello

Something like this.

Good luck,
H

--SCRIPT
try
-- (0) check server's response
set max_retry to 60
set k to 0
repeat while (do shell script "ping -c 1 10.192.168.1") contains "100% packet loss"
delay 5
set k to k + 1
if k > max_retry then error "Server is not responding for predefined period." number 8000
end repeat

-- (1) mount server volume
mount volume "afp://10.192.168.1/" -- guest access
--mount volume "afp://username:password@10.192.168.1/volumename"

on error errs number errn
display dialog errs & " " & errn with icon 2
--error errs number errn
end try
--END OF SCRIPT

Nov 22, 2008 12:07 AM in response to Hiroto

Oops.

The 'ping' will return non-zero exit status if specified host is not responding and consequently 'do shell script' will throw error.
The following script should handle such error properly, I hope.

All the best,
H

--SCRIPT (corrected)
try
-- (0) check server's response
set max_retry to 60
set k to 0
repeat while (do shell script "ping -c 1 10.192.168.1 2>&1; exit 0") contains "100% packet loss"
delay 5
set k to k + 1
if k > max_retry then error "Server is not responding for predefined period." number 8000
end repeat

-- (1) mount server volume
mount volume "afp://10.192.168.1/" -- guest access
--mount volume "afp://username:password@10.192.168.1/volumename"

on error errs number errn
display dialog errs & " " & errn with icon 2
--error errs number errn
end try
--END OF SCRIPT

Applescript to Ping Server, wait, retry

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