Pinging IP trouble

I am very new to Applescript and have ran into a problem. I am trying to set up a script that will constantly ping all ip adresses on my network. I deally I would want this script to run all the time. Whenever it finds one of three specific ip addresses, I would like it to perform a task. IN this case just say the date. If it doesnt find one of the three specific IP adresses then I'd like it to keep searching. And lastely if it finds a different IP adresses than the three specific ones, I'd like it to notify me.


My problem is that I can get it to search for only one address. If it finds that adress right off the bat then I'm good. If it doesn't then I get a applescript error that says my ping timeout. It does not even go to the else if statment, it simply ends my code. I also can not get the reapeat function to work. I tried in another script and it just kept repeating over and over again even if the ping successfully found the ip adress the first time.


The xxx.xxx.x.xxx in my code if my ip adress and they are the same. I really need for it to search not just for mine but for two other specific ones.





set myhour to get the (hours of (current date)) as string

set myminutes to get the (minutes of (current date)) as string

set mydate to date string of (current date)



set ping to (do shell script "ping -c 2 xxx.xxx.x.xxx)

if ping contains "xxx.xxx.x.xxx" then

tell application "iCal"


view calendarat (my (current date))


switch viewtoday view


activate

end tell

say "[[rate 160]] [[pitch 50]]Today is " & mydate using "Vicki"

say "[[rate 160]] [[pitch50]] The time is " & myhour & ":" & myminutes using "Vicki"

else if ping contains "timeout" then

say "User not found"

end if

Mac OS X (10.6.8)

Posted on Jun 4, 2012 5:40 AM

Reply
8 replies

Jun 4, 2012 6:04 AM in response to -Arch-

Is your goal here to learn AppleScript, or are you looking to determine uptime on your private network using available tools and ICMP ping requests? I'll assume it's the latter, and that alternatives are an option...


There are network-monitoring suites that are available, including Zenoss, Cacti, Ganglia, Nagios, and others. While support is ramping down, there's also Lithium. And I expect many other good choices.


There are smaller tools and shell scripts around, like pingcheck that might work here.


Python and other languages also have libraries or frameworks that simplify directly using ICMP ping.


SLAC has a ginormous list of monitoring tools, too.

Jun 4, 2012 9:25 AM in response to -Arch-

In order to find 'all IP addresses' on your network, you have to ping the broadcast address - it doesn't sound like you're doing that, but it's not clear from your obfuscation. If you're pinging a specific address then you will get an error when that address isn't found, because ping returns an error code indicating 'host not found' and AppleScript is responding to that error.


If you want to check if a specific IP address is/is not found on your network, do something like this:


try

set ping to do shell script "ping -c2 10.20.30.40"

display dialog "Host is found!"

on error

display dialog "Host not found"

end try


In this way the ping command runs. If it exits cleanly then you know the host exists. If the host doesn't exist then the 'on error' clause is triggered.


So, you could use this technique multiple times, one per host. That will work for 'known' hosts, but it won't help you find unknown/rogue devices because you're looking for specific IP addresses. In that case, the alternative approach would be to use the broadcast address of your network. This should solicit a response from every device on your network, from which you can deduce which machines are there, and whether there are any rogue devices, e.g.:


try

set ping to do shell script "ping -c2 10.20.30.255"


if ping contains "10.20.30.254" then

display dialog "router exists"

else

display dialog "router not found"

end if

if ping contains "10.20.30.1" then


display dialog "host .1 found"

else

display dialog "host .1 not found"

end if

end try


Set the first line to ping your broadcast address, then repeat the specific checks as needed.

There are various methods of finding unknown hosts - the easiest might be to check the output of the standard command to see how many hosts should exist (remember, you'll have addresses for more than the three hosts you expect - you may have one for your router, your base station, your printer, etc., etc.), then you can just check if the number of lines in the response matches what you expect.

Jun 4, 2012 1:43 PM in response to Camelot

This is what I have so far.


set myhour to get the (hours of (current date)) as string

set myminutes to get the (minutes of (current date)) as string

set mydate to date string of (current date)


try

set ping to (do shell script "ping -c 2 first device IP")

if ping contains "first device IP" then

tell application "iCal"


view calendarat (my (current date))


switch viewtoday view


activate

end tell

say "[[rate 160]] [[pitch 50]]Today is " & mydate using "Vicki"

say "[[rate 160]] [[pitch50]] The time is " & myhour & ":" & myminutes using "Vicki"

end if

on error

say "User unknown"

end try


try

set ping to do shell script "ping -c2 second device IP"

if ping contains "second device IP" then

say "hello"

end if

on error

say "user unknown"

end try


It searches for two different IP addresses. If it doesnt find them then it returns with a user unknown message without any errors. This is getting close to what I want.


Is there a way for this to repeat itself constantly until it finds a device. For example, I would want it to constantly search for device one without saying "User unknown" over and over again. Then when it finally finds device 1; it would stop searching but it would keep searching for the other 2 devices.


If this is possible, is there a way to make it so it starts the search for device 1 again as soons as it notices there is no connection with that device.


Jun 4, 2012 5:21 PM in response to -Arch-

Is there a way for this to repeat itself constantly until it finds a device.


Sure, stick the commands in a repeat loop.


As for the rest of your requests, you need to keep state on each iteration and decide only notify the user when the state changes. This is as simple as using a variable to store whether the device was seen the last time the script ran. For example:


set previousResult to false-- assume it wasn't seen last time


repeat

try

set ping to do shell script "ping -c2 <device IP>"

set thisResult to true-- if we get here, the device responded to a ping

if thisResultpreviousResult then

say "hello"

set previousResult to thisResult

end if

on error

set thisResult to false-- the device is not present

if thisResultpreviousResult then

say "user unknown"

set previousResult to thisResult

end if

end try

end repeat

In this case I start off assuming the device does not exist, using a variable called previousResult, then I start an endless loop.

For each iteration of the loop I try to ping the address. If the address responds, I set thisResult to true, indicating that the device does exist this time. I then compare thisResult to the previousResult. If they differ then it means the host was not available last time, but it is now, so I alert the user, then I store theResult in previousResult so that next time around I know the host did exist.


Conversely, if the host does not exist, the ping fails and triggers the on error code. Here I do a similar thing except I first set thisResult to false. I then compare thisResult to previousResult to see if the state has changed and alert the user accordingly, before storing thisResult for the next iteration.


Does that make sense.

Jun 4, 2012 8:14 PM in response to -Arch-

Do i continue with my initial if statement of:


if ping contains "device 1" then


That line is pointless. If the ping succeed, the result will ALWAYS include the IP address of the pinged device. If the ping fails (because the host is not online) then this line will never get executed since the code falls down to the 'on error' statement.


Since there's no need to run an 'if' for a condition that's always true, you can safely omit this line.

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.

Pinging IP trouble

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