applescript to ping computer to check status/restart?

Posted on
Sat Apr 21, 2012 11:09 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

applescript to ping computer to check status/restart?

I have a computer on my home ethernet network that has been hanging up occasionally. I'd like to use an applescript in Indigo to monitor its status and then, if it does not respond, use an appliance link to restart it. The reset portion is easy, but I don't really know where to begin to get the computer's status and then use that info to trigger the action. Any ideas?

Posted on
Sat Apr 21, 2012 12:10 pm
lalisingh offline
Posts: 166
Joined: Mar 27, 2007

Re: applescript to ping computer to check status/restart?

hamw wrote:
I have a computer on my home ethernet network that has been hanging up occasionally. I'd like to use an applescript in Indigo to monitor its status and then, if it does not respond, use an appliance link to restart it. The reset portion is easy, but I don't really know where to begin to get the computer's status and then use that info to trigger the action. Any ideas?


To check computers I use 'ping The following applescript snippet is what I use that runs every five minutes. Variable D-LAN can trigger any action you want.
save the following code as an applescript file in the attachment folder and call it from a schedule.


Code: Select all
using terms from application "IndigoServer"
   on dashboardUpdate()
      local thePing, tempCSVholder, oldDelims
      local LANIPList
      --
                -- assume indigo variables are defined D-LANIP and D-LAN. D_LANIP contains a list of IP's you want to ping comma separated.
                --
      set oldDelims to AppleScript's text item delimiters -- save their current state
      set AppleScript's text item delimiters to {","} -- declare new delimiters   
      --
      -- LAN access checking
      set tempCSVholder to value of variable "D-LANIP"
      set LANIPList to text items of tempCSVholder
      
      set value of variable "D-LAN" to 1
      repeat with i from 1 to (count of LANIPList)
         try
            set thePing to do shell script "ping -o -t 2 -c 2  " & item i of LANIPList & "|grep -e %"
            if thePing contains "0 packets" then
               set value of variable "D-LAN" to 0
            end if
         on error
            set value of variable "D-LAN" to 0
         end try
      end repeat
      
      set AppleScript's text item delimiters to oldDelims -- restore them
   end dashboardUpdate
   
end using terms from

[url]https://www.VillageWorker.com[/url]
Extreme data analytics, Sensing, Control integration work.
Indigo • Barix • Kentix • Mobotix • Mikrotik • Apple

Posted on
Sat Apr 21, 2012 1:03 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: applescript to ping computer to check status/restart?

Though not as robust as lalisingh's solution, I do much the same thing to check online status of one or two of my home computers every 12 seconds. I created the "Check Mac Pro Online.scpt" AppleScript below and put it in the (Indigo Home)/Scripts/Background Tasks folder.

Code: Select all
-- Set some variables.
set theIP to "192.168.1.101" -- The IP of the computer to check.
set theTimeout to 20 -- The time, in milliseconds, to wait before assuming the ping failed.

-- Check to see if the Mac Pro (staic IP) is on the network.
--   This shell script compound command pings theIP with a timeout of theTimeout milliseconds, then pipes it through grep to grab only the line containing the percent of packet loss.  That's then piped through sed twice to remove the leading and trailing data around the packet loss percentage. The result returned from the compound command is number string (either 0.0 or 100, or nothing at all, indicating failure to ping as well).
set theResult to do shell script "/sbin/ping -qon -c 1 -W " & theTimeout & " " & theIP & " | /usr/bin/grep % | /usr/bin/sed \"s/.*,\\ //\" | /usr/bin/sed \"s/%.*//\""
if theResult is "" then
   set theResult to 100  -- Set to 100 if no result was returned to allow numerical comparison below.
end if
-- If the result is greater than 0, then the 1 ping sent was not returned within the 20 milisecond timeout specified in the script above, thus the computer is likely not awake or not connected to the network.  The "isMacProOnline" variable is assumed to already exist in Indigo.
if theResult as number > 0 then
   tell application "IndigoServer"
      set value of variable "isMacProOnline" to "false"
   end tell
else
   tell application "IndigoServer"
      set value of variable "isMacProOnline" to "true"
   end tell
end if

Note that the above script assumes that a "isMacProOnline" variable already exists in Indigo. You can change the variable name in the script.

Once this script is saved, create a Schedule in Indigo that repeats at the interval you desire (I use 0.20 minutes, i.e. 12 seconds), though I wouldn't recommend using an interval less than 5 seconds, lest it sucks up the resources of the server Indigo is running on by executing so many shell commands consecutively. The Schedule should contain an Execute Script action, with the above script file selected as the script to run.

Posted on
Sun Apr 22, 2012 9:56 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: applescript to ping computer to check status/restart?

That's terrific. Got it working - thanks, guys!

Posted on
Tue Oct 28, 2014 3:01 am
darghorn offline
Posts: 7
Joined: Oct 27, 2014

Re: applescript to ping computer to check status/restart?

Hello,

I've tried to run this last script, but there is an error :

error "unable to convert \"0.0\" in type number." number -1700 from "0.0" to number

As I understand the result is not in an expected format ... Does it ring a bell for someone ?

Posted on
Tue Oct 28, 2014 3:13 am
darghorn offline
Posts: 7
Joined: Oct 27, 2014

Re: applescript to ping computer to check status/restart?

I've found the answer :)

This error was related to my separate number value in my system preference, as my system is in french (my value was "," instead of ."")

This is now working like a charm :)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests