ssh from inside python? Terrible idea?

Posted on
Thu Apr 28, 2016 11:29 am
Different Computers offline
User avatar
Posts: 2551
Joined: Jan 02, 2016
Location: East Coast

ssh from inside python? Terrible idea?

I'm trying to send a command to imagesnap on a remote machine on my LAN.

I looked up how to make python do ssh, and seems like it's possible ala:

Code: Select all
 commands.getstatusoutput("ssh user:pass@192.168.0.99 'imagesnap'")
as per http://stackoverflow.com/questions/3586 ... ith-python

but when I try this in Indigo I get an error. A python error about "commands" being undefined.

Am I heading the wrong way? Do I need some "commands" module? Is it a terrible idea in the first place?

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Thu Apr 28, 2016 11:43 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: ssh from inside python? Terrible idea?

You need to add the path to ssh.
When this is called from within indigo no environment variables are set. So the terminal session knows nothing about anything.

it is /usr/bin/ssh



Sent from my iPhone using Tapatalk
Last edited by kw123 on Thu Apr 28, 2016 1:43 pm, edited 1 time in total.

Posted on
Thu Apr 28, 2016 12:12 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

ssh from inside python? Terrible idea?

One more question: what do you want to do with it, just get status back or get a return value/ string?
Also you need to do
xxx= commands.....
Then check xxx.
If xxx=="whatever": indigo.server.log(" ok")

Otherwise you don't know what happened


Sent from my iPhone using Tapatalk

Posted on
Thu Apr 28, 2016 1:45 pm
Different Computers offline
User avatar
Posts: 2551
Joined: Jan 02, 2016
Location: East Coast

Re: ssh from inside python? Terrible idea?

I'm trying to make the remote computer run a command line tool called imagesnap, which takes a picture. And I'm saving it to the drop box on the indigo server.

but the error I'm getting when I try to run the python command suggests that the problem is with
Code: Select all
commands.getstatusoutput
rather than with the ssh.

I'm afraid I don't understand this next bit at all:

Code: Select all
xxx= commands.....
Then check xxx.
If xxx=="whatever": indigo.server.log(" ok")

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Thu Apr 28, 2016 1:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: ssh from inside python? Terrible idea?

The Python commands module was deprecated a while ago. Use the subprocess module instead.

I think I'd look at the solution that uses the paramiko library (the best solution on that stackoverflow page as represented by the green checkmark). If it's available for Python 2.6 that looks to be the easiest solution.

As a general rule, however, Python does an excellent job of just about every kind of network communication, even though it may require a 3rd party library. In face, aside from communicating with other Mac apps, Python will pretty much always be a better alternative.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Apr 28, 2016 2:18 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: ssh from inside python? Terrible idea?

This should work:
Code: Select all
import subprocess
userID  = "xxx"
passWD = "xxx"
ipNumber="1.1.1.1"
ret = subprocess.Popen("/usr/bin/ssh "+userID+":"+passWD+"@"+ipNumber+" 'imagesnap'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
indigo.server.log("return text :" + ret[0])
indigo.server.log("error text : " + ret[1])


Karl
Last edited by kw123 on Thu Apr 28, 2016 2:22 pm, edited 1 time in total.

Posted on
Thu Apr 28, 2016 2:18 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: ssh from inside python? Terrible idea?

This should work:
Code: Select all
import subprocess
userID  = "xxx"
passWD = "xxx"
ipNumber="1.1.1.1"
ret = subprocess.Popen("/usr/bin/ssh "+userID+":"+passWD+"@"+ipNumber+" 'imagesnap'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
indigo.server.log("return text :" + ret[0])
indigo.server.log("error text : " + ret[1])


Karl

Posted on
Thu Apr 28, 2016 2:21 pm
Different Computers offline
User avatar
Posts: 2551
Joined: Jan 02, 2016
Location: East Coast

Re: ssh from inside python? Terrible idea?

Well duh, I just noticed there's an option in Indigo to run a shell script.

That's all I need.

Thanks guys!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests