network ping applescript to python

Posted on
Thu Oct 25, 2018 2:02 pm
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

network ping applescript to python

hello, im in the process of converting my AppleScript's to python and this one is giving me issues. below is my attempt at python and below that is the actual working AppleScript. any help is much appreciated! I'm not a programmer so a lot of the syntax is taken from bits and pieces. thanks!

Code: Select all
# New Ping
dev = indigo.devices[1619321171] # Your dev ID here
var = indigo.variables[78070398] # Your var ID here
ip = 192.168.0.10
timeout = 20
result = "/sbin/ping -qon -c 1 -W " & theTimeout & " " & theIP & " | /usr/bin/grep % | /usr/bin/sed \"s/.*,\\ //\" | /usr/bin/sed \"s/%.*//\""

If result >0 then
indigo.variable.updateValue(var, value="true")

else
indigo.variable.updateValue(var, value=“false”)



Original APPLESCRIPT:
Code: Select all
-- Ping
set theIP to "192.168.0.10" -- The IP of the computer to check.
set theTimeout to 20 -- The time, in milliseconds, to wait before assuming the ping failed.
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
end if

if theResult as number > 0 then
   tell application "IndigoServer"
      set value of variable "var_internet_PING" to "false"
   end tell
else
   tell application "IndigoServer"
      set value of variable "var_internet_PING" to "true"
   end tell
end if

Posted on
Thu Oct 25, 2018 3:34 pm
jay (support) offline
Site Admin
User avatar
Posts: 16821
Joined: Mar 19, 2008
Location: Austin, Texas

Re: network ping applescript to python

Try this:

Code: Select all
import subprocess

ip = "192.168.0.10"
timeout = 20
destination_var_id = 78070398

command = "/sbin/ping -c 1 -W {} {}".format(timeout, ip)
reply = subprocess.call(command, shell=True)
if reply == 0:
    # successful ping
    indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Oct 25, 2018 7:55 pm
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: network ping applescript to python

works GREAT, thank you! here is my last script to convert. I took the code from above and modified the ping command to use tcpping. if all is okay it outputs open. I wanted to remove the trailing . but it was too much work for me to figure it out. so what I can't figure out is I want the if/else to be if reply == "open." then update the variable. for some reason, I'm guess its looking for integers not characters, I cant get it to evaluate that. you know what I might be doing wrong? the only part im having issues with is on the if. sooo close! thanks again!


Code: Select all
#Office Echo
import subprocess

ip = "192.168.0.1"
port = 443
destination_var_id = 11110415

command = "/usr/local/Cellar/tcping/1.3.5/bin/tcping -t 2 {} {} | grep -e open -e timeout | sed 's/192.168.0.* //'".format(port, ip)
reply = subprocess.call(command, shell=True)
if reply == "open":
    # successful ping
    indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")
#END

Posted on
Fri Oct 26, 2018 5:57 am
kw123 offline
User avatar
Posts: 7695
Joined: May 12, 2013
Location: Dallas, TX

network ping applescript to python

use:
Code: Select all
reply = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
reply has 2 components
reply[0] is what you want, standard output
reply[1] contains a potential error message if something goes wrong
use
Code: Select all
if reply[0] =="open":


Karl

Posted on
Fri Oct 26, 2018 7:43 am
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: network ping applescript to python

thank you for the response. I do understand the suggestions you mentioned. Im still getting false for every run though. I checked and ran the command manually and got "open." dont know if the . at the end matters (I tried it with and without in the reply). I made all the modifications you suggested but its always false. any ideas?

Posted on
Fri Oct 26, 2018 7:56 am
kw123 offline
User avatar
Posts: 7695
Joined: May 12, 2013
Location: Dallas, TX

network ping applescript to python

Add this to check:
indigo.server.log(unicode(reply))
After reply=

... and which .?
Sent from my iPhone using Tapatalk

Posted on
Fri Oct 26, 2018 10:31 am
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: network ping applescript to python

thank you, that helped. I had 2 issues. first the port, ip was reversed and second the eval term should have "open./n" all is working perfect now and im done converting all my applescripts!!! thank you so much! you guys are always so helpful!

Posted on
Sat Oct 05, 2019 12:39 pm
hamw offline
Posts: 1214
Joined: Mar 31, 2008

Re: network ping applescript to python

Hi guys,

Trying to get this to work. Here's what I put together:

Code: Select all
Import time
ip = "10.0.1.20”  ##Cameras_Pool_Equipment
timeout = 20
destination_var_id = 711613744
command = "/sbin/ping -c 1 -W {} {}".format(timeout, ip)
reply = subprocess.call(command, shell=True)
    if reply == 0:
    # successful ping
indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")


When run in the action group dialog box python editor, it returns an "EOL while scanning string literal" error.

Code: Select all
import subprocess
   ip = "10


I opened the script in Xcode and the above is also highlighted in red.

this came back in the log:

Code: Select all
Oct 5, 2019, 2:56:49 PM
   Script Error                    Ping Cameras.py: compile() expected string without null bytes
   Script Error                    Exception Traceback (most recent call shown last):

TypeError: compile() expected string without null bytes


Thoughts?

Also, I'd like to string together several pings in one script as below. Do you foresee any trouble with that? I did put in an "import time" so that I could space the pings out.

Code: Select all
Import time
import subprocess
ip = "10.0.1.20” ##Cameras_Pool_Equipment
timeout = 20
destination_var_id = 711613744
command = "/sbin/ping -c 1 -W {} {}".format(timeout, ip)
reply = subprocess.call(command, shell=True)
    if reply == 0:
    # successful ping
indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")

delay 2

ip = "10.0.1.21” #Fam Rm Patio Camera
timeout = 20
destination_var_id = 990844776
command = "/sbin/ping -c 1 -W {} {}".format(timeout, ip)
reply = subprocess.call(command, shell=True)
if reply == 0:
    # successful ping
    indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")


delay 2

ip = "10.0.1.22”  #Cameras2_Fam_Rm_Walkway
timeout = 20
destination_var_id = 145747591
command = "/sbin/ping -c 1 -W {} {}".format(timeout, ip)
reply = subprocess.call(command, shell=True)
if reply == 0:
    # successful ping
    indigo.variable.updateValue(destination_var_id, value="true")
else:
    # unsuccessful ping
    indigo.variable.updateValue(destination_var_id, value="false")


Posted on
Sat Oct 05, 2019 1:27 pm
FlyingDiver offline
User avatar
Posts: 5351
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: network ping applescript to python

First, import should not be capitalized. Fix that.

Second, show the complete script you're using for one ping. The first code you posted is not complete, because it doesn't have the import subprocess line that's giving an error.

Third, watch your indents. The indigo.variable.updateValue call for the if conditional for a success is not indented. And it looks like you might have some other indent errors. Don't mix tabs and spaces. Or use an editor that converts them. I recommend BBEdit for Python programming, if you're not going to use an IDE.

Fourth, no need to use a delay between pings to different devices.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Oct 05, 2019 3:42 pm
hamw offline
Posts: 1214
Joined: Mar 31, 2008

Re: network ping applescript to python

Thanks! I got BB edit and found a bunch of editing issues. Fixed those and it seems fine. Appreciate the tip!

Posted on
Sat Oct 05, 2019 3:44 pm
FlyingDiver offline
User avatar
Posts: 5351
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: network ping applescript to python

hamw wrote:
Thanks! I got BB edit and found a bunch of editing issues. Fixed those and it seems fine. Appreciate the tip!


You're welcome.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest