Ping Script

Posted on
Tue Jul 31, 2012 4:49 pm
kcossabo offline
Posts: 38
Joined: Dec 02, 2007

Ping Script

I wanted to have a scrip to set a variable if results of a Ping'd host was positive or negitive.

I put this into Apple Script editor on the Indigo Server
Code: Select all
tell application "IndigoServer"
   try
      if (do shell script "ping -c1 68.87.73.242 > ~/Documents/junk") = "" then
         set value of variable "InternetUp" to "true"
      end if
   on error
      set value of variable "InternetUp" to "false"
   end try
end tell


and it works if I hit run. Changes the variable InternetUp if the ping fails to false, and true if it passes.

I then create a schedule to call the applescript, and it always set the variable to false. I change it to "sdfsdfS" and it sets it to false. I re-run the script in script editor, and it flips to true

Posted on
Thu Aug 02, 2012 5:43 am
kcossabo offline
Posts: 38
Joined: Dec 02, 2007

Re: Ping Script

Maybe I can ask this differently

Why does this not work as an embedded script?

Code: Select all
try
   if (do shell script "ping -c1 68.87.73.242 > ~/Documents/junk") = "" then
      set value of variable "InternetUp" to "true"
      log true
   else
      set value of variable "InternetUp" to "false"
   end if
on error
   set value of variable "InternetUp" to "false"
end try



The IP address does nto respond to the ping, and the ping command does exit with an error, and in AppleScript the event log will show;
--> error "The command exited with a non-zero status." number 2

Posted on
Thu Aug 02, 2012 6:59 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Ping Script

This works for me:

Code: Select all
-- First we'll test for the iPhone via wi-fi:

with timeout of 10 seconds
   -- Test for Ryan's iPhone
   log "Attempting to detect Ryan's iPhone over Wi-Fi..."
   set theResult to do shell script "ping -o -t 4 -c 4 192.168.15.135"
   if theResult contains "1 packets transmitted, 1 packets received, 0% packet loss" then
      tell application "IndigoServer"
         set value of variable "isHouseOccupied" to "true"
         set value of variable "isRyansPhoneHome" to "true"
         log "Ryan's iPhone was detected on the network"
         return true
      end tell
   else if theResult contains "0 packets received" then
      tell application "IndigoServer"
         set value of variable "isHouseOccupied" to "false"
         set value of variable "isRyansPhoneHome" to "false"
         log "Ryan's iPhone wasn't detected on the network"
      end tell
   end if
end timeout

Posted on
Thu Aug 02, 2012 7:41 am
kcossabo offline
Posts: 38
Joined: Dec 02, 2007

Re: Ping Script

ryanbuckner Thank You.

Is this a AppleScript file or Embedded and is this Version 5?

I am getting the following as embedded;

Script Attempting to detect Ryan's iPhone over Wi-Fi...
Error script error: around characters 175 to 225
Error script error: PING 192.168.15.135 (192.168.15.135): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2

--- 192.168.15.135 ping statistics ---
4 packets transmitted, 0 packets received, 100.0% packet loss (2)


and the Dialog at the bottom of the window has a red "Ping 192.168.15.135 (192.168.15.135): 56 Data Bytes"

I will tried it as a script file and it resulted also in no variables set. If I run it in the AppleScript Editor I get;

(*Attempting to detect Ryan's iPhone over Wi-Fi...*)
tell current application
do shell script "ping -o -t 4 -c 4 192.168.15.135"
--> error "PING 192.168.15.135 (192.168.15.135): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2

--- 192.168.15.135 ping statistics ---
4 packets transmitted, 0 packets received, 100.0% packet loss" number 2
Result:
error "PING 192.168.15.135 (192.168.15.135): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2

--- 192.168.15.135 ping statistics ---
4 packets transmitted, 0 packets received, 100.0% packet loss" number 2


thank you again for the help.

Posted on
Thu Aug 02, 2012 8:17 am
kcossabo offline
Posts: 38
Joined: Dec 02, 2007

Re: Ping Script

Must be differences in the OS. I am using 10.7 and the ping result does not have 0% but rather 0.0% for starters.

it failed until I put in a 'try' and I am back to the situation that if I run the script file, it works, but if I call it from an event it does not.

Code: Select all
-- First we'll test for the iPhone via wi-fi:

with timeout of 10 seconds
   -- Test for Ryan's iPhone
   log "Attempting to detect Brandon's iPhone over Wi-Fi..."
   set theResult to " "
   try
      set theResult to do shell script "ping -o -t 4 -c 4 192.168.11.1"
   on error
      log "errorHandler"
      tell application "IndigoServer"
         set value of variable "isHouseOccupied" to "false"
         set value of variable "isBrandonsPhoneHome" to "false"
         log "Brandon's iPhone wasn't detected on the network"
      end tell
   end try
   
   if theResult contains "1 packets transmitted, 1 packets received, 0.0% packet loss" then
      tell application "IndigoServer"
         set value of variable "isHouseOccupied" to "true"
         set value of variable "isBrandonsPhoneHome" to "true"
         log "Brandon's iPhone was detected on the network"
         return true
      end tell
   else if theResult contains "0 packets received" then
      tell application "IndigoServer"
         set value of variable "isHouseOccupied" to "false"
         set value of variable "isBrandonsPhoneHome" to "false"
         log "Ryan's iPhone wasn't detected on the network"
      end tell
   end if
end timeout

Posted on
Thu Aug 02, 2012 9:28 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Ping Script

maybe it's a permission issue.?

Code: Select all
set theResult to do shell script "ping -o -t 4 -c 4 192.168.11.1" user name "admin" password "myadminpassword" with administrator privileges

Posted on
Thu Aug 02, 2012 11:03 am
kcossabo offline
Posts: 38
Joined: Dec 02, 2007

Re: Ping Script

Once again, thank you, but no go....

What is odd is that it fails when launched as a file QUICK. If I put an IP that will fail, and run the script from ScriptEditor it takes the expected few seconds to fail out (then riggers the 'on error' part). I run it from with in Indigo and it is immediate error.

So the permission thing did not work, but I think you put me on the right path, there is an error occurring before the 4 pings time out.

http://www.youtube.com/watch?v=KR7oQPV_mZ4

Posted on
Tue Jan 15, 2013 10:28 pm
SpencerJRoberts offline
User avatar
Posts: 256
Joined: Dec 09, 2012
Location: Mountain View, CA

Re: Ping Script

I am attempting to the the same thing as kcossabo and have run into the same error he did, Jay or Matt, do you have any idea what is going on here? I'm stumped. Here's the code I'm using:

Code: Select all
set spencerHome to true

try
   do shell script "ping -s 1 -i 11 -o -t 2 192.168.1.131"
on error
   set spencerHome to false
   tell application "IndigoServer"
      set value of variable "spencerHome" to "false"
   end tell
end try

if spencerHome is true then
   tell application "IndigoServer"
      set value of variable "spencerHome" to "true"
   end tell
end if


To reiterate what the OP said, this works flawlessly when executed through applescript, but when trying to launch it via Indigo in ANY way (embedded or linked) it fails.

Posted on
Wed Jan 16, 2013 12:50 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Ping Script

I'm not sure what's going on with those AppleScripts and shell scripts not working. However, here's the code for the external AppleScript I have executed from an Indigo scheduled task that works fine for me.

Code: Select all
-- Check to see if a static IP is up.
set theResult to do shell script "/sbin/ping -qon -c 1 -W 20 192.168.100.100 | /usr/bin/grep % | /usr/bin/sed \"s/.*,\\ //\" | /usr/bin/sed \"s/%.*//\""
if theResult is "" then
   set theResult to 100
end if
-- The value of theResult is the percent packet loss.  If theResult is greater than 0, then the 1 ping sent was not returned within the 20 milisecond timeout specified in the script above.
if theResult as number > 0 then
   tell application "IndigoServer"
      set value of variable "TestVariable" to "false"
   end tell
else
   tell application "IndigoServer"
      set value of variable "TestVariable" to "true"
   end tell
end if

I have the Indigo schedule that executes this run every 12 seconds. It's useful for testing high availability because it only uses 1 ping to test for pass/fail. If the 1 ping fails, it sets the Indigo variable to "false". If 100% ping returns aren't required, you can either increase the count "-c 1" flag in the above script from 1 to whatever number of pings you want to use to verify availability, or you can decrease the wait period "-W 20" to something lower like 5 ms with "-W 5". Also, by executing the above as an AppleScript, you don't have to wait for it to finish executing for Indigo to continue all other processes. If the above were to be executed as an embedded AppleScript, all of Indigo would freeze until the script finished running.

Posted on
Wed Jan 16, 2013 1:17 am
SpencerJRoberts offline
User avatar
Posts: 256
Joined: Dec 09, 2012
Location: Mountain View, CA

Re: Ping Script

Thanks for the reply, I'll give this a shot!

Posted on
Sun May 12, 2013 10:22 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Ping Script

nsheldon... I made a small change to the script you posted to make it a little simpler. Instead of processing the output of the ping command through grep and sed, you can just look at the return code. This is a nice trick when all you want to know is if something worked.

For those not familiar with shell scripting, >/dev/null 2>&1 causes all output (standard output as well as errors) from the ping command to be ignored (sent to the bit-bucket). The ";" is an easy way to stack multiple commands on a line. In this case, the shell executes the ping command and then executes the echo command. The shell built-in variable $? contains the result code for the preceding command - in this case, the result of the ping command. A return code of 0 is always success and any non-0 value is an error.

Code: Select all
-- Check to see if a static IP is up - also works with a FQDN.

set theResult to do shell script "/sbin/ping -qon -c 10 -W 20 192.168.5.34 >/dev/null 2>&1;/bin/echo $?"
-- The value of theResult is the return code from the ping command.  If theResult is non-0, then none of the 10 pings sent were returned within the 20 milisecond timeout specified.

if theResult as number is not 0 then
   tell application "IndigoServer"
      set value of variable "TestVariable" to "false"
   end tell
else
   tell application "IndigoServer"
      set value of variable "TestVariable" to "true"
   end tell
end if

Posted on
Sun May 12, 2013 11:33 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Ping Script

Hey. Nice modification. I seem to remember considering that but chose the more complex grip + sed just in case I wanted to allow a few failed pings later. In any case, thanks for sharing the modification.

Posted on
Sun May 12, 2013 3:01 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Ping Script

I did change the ping settings slightly to try up to 10 times, but quit immediately on the first success. That gave a little elasticity. But, I see your point about parsing the output, especially if you wanted more of a measure of quality instead if an absolute pass/fail result.

Posted on
Tue May 28, 2013 12:14 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Ping Script

-- Applescript:
-- do a ping and see if any device is present, if one answers, someone is home, uses 2 variables in indigo: IP_numbers and minutes without any response, see details below
-- works with every router, no other prerequisites
--
-- ping the IP numbers several times, if not successful increment ip_devices_not_online_minutes in indigo
-- in indigo:
-- ... create variable ip_devices_not_online_minutes this where the answer is returnd. count number of minutes since last answer by listed IP devices
-- ... create variable ip_devices_IP_of_devices_to_look_for and fill with IP numbers separated by space eg "192.168.1.122 192.168.1.125 192.168.1.134" without the quotes
-- ... put this file into a folder on your mac and in scheduler set once a minute execute as apple script file, do not use embedded, as indigo will "stop" / wait for this to end
-- ... create a trigger if ip_devices_not_online_minutes > 60 ( for 60 minutes) then set e.g. away_mode = 1 ...
-- works ok if the iphone/other devices are not asleep
-- must / should set DHCP IP number fixed in router for pinged devices
-- Karl Wachs
-- v 1.6
-- only 2 indigo variables needed 1. ip numbers and 2. return the number of minutes without any response
-- may 28, 2013
--
-- use basic post from http://macstuff.beachdogs.org/blog/?p=34 for ping and error capture

tell application "IndigoServer"
set ip_devices_not_online_minutes to (get value of variable "ip_devices_not_online_minutes")
set ip_devices to (get value of variable "ip_devices_IP_of_devices_to_look_for")
end tell

set No_of_devices_to_look_for to count words of (ip_devices) -- count the number of IP numbers

set devices_failed to 0
set NN to 0
repeat while NN < No_of_devices_to_look_for
set NN to NN + 1
set IP_NUMBER to (word NN of ip_devices)
set command to "/sbin/ping -o -c 5 " & IP_NUMBER -- ping max 5 times, stop if device answers
set done to 1
try
set thePing to do shell script command
on error
set devices_failed to devices_failed + 1
set done to 0 -- we are not done yet
end try
if done = 1 then set NN to 9999 -- found one live device, exit
end repeat

if devices_failed = No_of_devices_to_look_for then -- no device answered
set ip_devices_not_online_minutes to ip_devices_not_online_minutes + 1 -- increment minutes offline counter
else -- .... found at least one alive, reset minutes counter to 0
set ip_devices_not_online_minutes to 0
end if

try
tell application "IndigoServer"
set value of variable "ip_devices_not_online_minutes" to ip_devices_not_online_minutes
end tell
end try

Posted on
Tue May 28, 2013 7:31 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

locate iphone using apples find iPhone, match to house loc.

has anyone tried to use "find iPhone" and match the coordinates of the found iPhone to the house coordinates?.

That will not require the iphone / ipad / laptop to be actively browsing the internet, could be asleep (not off). Normally its takes less than 5 minutes to find the iPhone, even if not actively used.

thanks

Karl

Who is online

Users browsing this forum: No registered users and 5 guests