How do I capture terminal ping results using apple script

I would like automate a process to monitor an IP address to check if it is reachable. My first thought is to use terminal to send out a couple of pings then capture the results.


Normally when a ping completes you back a few lines in terminal, such as;


'2 packets transmitted, 2 packets received, 0.0% packet loss'


I would use the results, positive or negative, to decide in a script or automator routine what to do next.


Is there a way of capturing that output string, or is there a better way to see if an IP address is up or down. Essentially passing back a true/false state?

Posted on Jun 9, 2011 11:36 AM

Reply
3 replies

Jun 9, 2011 10:40 PM in response to Dean

If you intend to do the automator stuff with AppleScript your script would contain something like this,


set x to do shell script "path-to-your-shell-script"


where x is the variable to receive the output (string) and path-to-your-shell-script is what it says (which can contain arguments).


You then write your shell script to do the ping and output (printf, echo, whatever) a yes/no, true/false, 1/0, or whatever you want to test for in the AppleScript.


Just remember the x is a string type, not numeric, so you need to convert appropriately if you use it in some other context than a string.

Jun 9, 2011 11:06 PM in response to X423424X

FWIW, here's a little bash shell line to test for the presence of a specific IP:


printf "%d" $((`ping -o IP | fgrep -c "IP"` != 0))


where IP is the desired IP number. This will output a 1 or a 0. Of course it's overkill since you could just test for non-zero and simply do just the ping without the comparision.


printf "%d" `ping -o IP | fgrep -c "IP"`


or even just,


echo -n `ping -o IP | fgrep -c "IP"`


[I would have added this to my original reply but I can't seem to edit it to update it -- guess someone reading it causes that.]

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.

How do I capture terminal ping results using apple script

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