Is there luup code for sending ssh commands to a server?

Hello!

I was able to successfully connect to the remote system from Vera 3 and execute remote script from console.

But I have troubles to make it work from Luup code. It succesfully connects and then disconnects - it looks like command is not sent. My code looks like this:

os.execute('ssh -l XXXXXX 192.168.XXX.XXX -y -i /root/.ssh/id_rsa "\"C:/Users/XXXXX/Desktop/XXXXXX.cmd\""')

I was trying with two different PCs (windows and ubuntu) and I see, that Vera connects and pass the auth successfully, but then just disconnects without command execution. I was trying with simple commands like “dir” or “ls” too.

I also don’t see any errors in Luup log files or in the command ouput (I was enabling stdout & stderr ouput to log file with “>> log.txt 2>&1”)

Has anyone seen such behaviour or does this method still works for those, who were using it before ?

I am trying it on Vera 3 with UI7 1.7.690.

– Andrey

It should still work. I’d be looking in the sshd log on the server, maybe with the log level turned up, to see what’s actually being executed on the server. It may be down to quoting, or that you are running without a tty, or something else similarly mundane.

I followed the steps here

http://forum.micasaverde.com/index.php/topic,11663.msg89171.html#msg89171

and after executing the command ssh -i ~/.ssh/id_dss -l root 192.168.xx.xx

I am getting the Error

Failed reading termmodes

Trying to ssh to a Synology NAS

Problem solved, needed to restart all devices. :smiley:

[quote=“Tarkus, post:63, topic:172513”]I followed the steps here

http://forum.micasaverde.com/index.php/topic,11663.msg89171.html#msg89171

and after executing the command ssh -i ~/.ssh/id_dss -l root 192.168.xx.xx

I am getting the Error

Failed reading termmodes

Trying to ssh to a Synology NAS[/quote]

I’m trying to optimize some luup code … is there a way to catch the log or the terminal when doing an os.execute ?

I have an ssh command started executing on a remote server, is there a way to store the terminal result in a variable?

like aa = os.execute .....

I would have to make some tests on the results

Greg

Yes indeed. Open with io.popen rather than os.execute.

http://www.lua.org/manual/5.1/manual.html#pdf-io.popen

local result
local f = io.popen "..."
if f then 
  result = f: read "*a"
  f: close()
end

thanks a lot, i’m trying to get it work, but I’m missing someting !

when executing

ssh -y -i /root/.ssh/vu73_dropbear root@192.168.0.73 sh /usr/script/nas_mount.sh

I get

Mount Directory exists Mounted

I’ve then followed your recommendation and inserted the following code

[code] local result

FileLog (“Start”)
local f = io.popen (“ssh -y -i /root/.ssh/vu73_dropbear root@192.168.0.73 sh /usr/script/nas_mount.sh”)

if f then
FileLog (“f exist”)
result = f: read “Mount
f: close()
end

FileLog (“Display Result”)
FileLog (result)
FileLog (“Stop”)[/code]

FileLog is a custom function creating a log file

In the file I get

2015-12-25 18:42:42 Started = Start 2015-12-25 18:42:42 Started = f exist

as I do not get

Display Result

it seems that the code is hanging somewhere on

result = f: read "*Mount*" f: close()

but I do not understand where and why ?

Thanks for your help !

Suggest you look at the syntax of the read statement. Only “*n”, “*a”, “*l” or a number is valid.

So I update the code as following

[code] local result

FileLog (“Start”)
local f = io.popen (“ssh -y -i /root/.ssh/vu73_dropbear root@192.168.0.73 sh /usr/script/nas_mount.sh”)

if f then
FileLog (“f exist”)
result = f: read “*a”
f: close()
end

FileLog (“Display Result”)
if result == nil then
FileLog “NIL”
else
FileLog (“-” … result … “-”)
end

FileLog (“Stop”)[/code]

it is now going through, but I only get a Blank Output

2015-12-25 20:30:37 Started = Start 2015-12-25 20:30:37 Started = f exist 2015-12-25 20:30:39 Started = Display Result 2015-12-25 20:30:39 Started = -- 2015-12-25 20:30:39 Started = Stop

is it sure that this is getting the echo output from the command line ?

[quote=“marcjw, post:60, topic:172513”]Thanks to this thread and specifically to this post [url=http://forum.micasaverde.com/index.php/topic,11663.msg120409.html#msg120409]http://forum.micasaverde.com/index.php/topic,11663.msg120409.html#msg120409[/url], I was able to successfully configure my Edge so that it could access my Zoneminder server in order to have some control over its cameras. Vera now determines when my cameras should run by remotely executing on my ZM server a couple little bash scripts I threw together. For a Vera newbie like me this thread saved me a lot of time.

Thanks again![/quote]

I Know its an old thread but this sounds interesting. How did you manage to do this? I recently set up a zoneminder server and im trying to figure out how to integrate it with my Vera edge.

I used to have this all working fine probably following these instructions some time ago but I had a support issue and think they must have reset a few files and now i’m having issues

when I run the command from the Vera cmd line

ssh -i ~/.ssh/id_key -l stu 192.168.xx.xxx 'sh /home/stu/androidscripts/test.sh'

it works absolutley fine but when i try and do this from LUA it doesn’t seem to authenticate properly

os.execute("ssh -i ~/.ssh/id_key -l stu 192.168.xx.xxx 'sh /home/stu/androidscripts/test.sh'") 

on my linux box i get the following logs

Mar 31 20:17:01 Squeeze02 CRON[15934]: pam_unix(cron:session): session opened for user root by (uid=0) Mar 31 20:17:01 Squeeze02 CRON[15934]: pam_unix(cron:session): session closed for user root Mar 31 20:18:39 Squeeze02 sshd[15945]: fatal: Read from socket failed: Connection reset by peer [preauth] Mar 31 20:22:12 Squeeze02 sshd[15957]: fatal: Read from socket failed: Connection reset by peer [preauth]

anyone got any ideas?

Guess: the known_hosts file on Vera has been wiped. It is connecting to your Linux server but can’t verify the SSH host key so it disconnects before attempting authentication.

There is an option for ssh to blindly accept unknown hosts. Is it -y? Check the docs.

I’ll try it when i get home, I did however try and ssh from the Linux box to the Vera and it produced an error about the known_hosts file on the linux machine having an incorrect entry so I have removed that one. will try with the -y flag later and report back…
Stu

os.execute blocks the LUA engine in vera.

If the code you are running (in this case the remote ssh code) takes any significant time (i.e. a problem connecting to the remote resource) it can trigger Vera to restart LUA because it thinks it is hung.

is there a better way??

I’m not sure if it is better (someone smarter like RTS would need to weigh in), but I use ncat on the remote host to make it listen and run a command when it receives traffic on a specific port, then I use wget on the vera (with a short timeout) to make the URL call to the remote server to trigger it.

I realize it is insecure but it is all within my LAN and only for low risk functions (like turning off a Mac’s display at night and turning on the HVAC in my car).

I can’t answer that question without knowing the details of he network between Vera and where you are sending the ssh command to.
Is the device guaranteed to be available and responsive 100.00 % of the time ?
Will the device always respond within a couple of seconds ?
Is the network between guaranteed to be up and responsive 100.00 % of the time ?
Will the network allow Vera to connect to the device within a couple of seconds ?

If the answer to any of this can occasionally be false, this solution can cause Vera to restrart.

There are many different ways to solve this depending on your programming skills in LUA (where you put timeouts on connections and responses) or in shell scripts … where you return right away but allow a sub process to do the communications in the background).

Yes/maybe not to all questions unless I have a bigger issue going on in which case a vera restart is least of my problems.

although the SSH code is set to have a 30 second delay in it which is maybe causing issues as the SSH code isnt completing.

[quote=“RichardTSchaefer, post:77, topic:172513”]I can’t answer that question without knowing the details of he network between Vera and where you are sending the ssh command to.
Is the device guaranteed to be available and responsive 100.00 % of the time ? YES
Will the device always respond within a couple of seconds ? possibly not if a response isnt provided until script complete.
Is the network between guaranteed to be up and responsive 100.00 % of the time ? YES
Will the network allow Vera to connect to the device within a couple of seconds ? YES

If the answer to any of this can occasionally be false, this solution can cause Vera to restrart.

There are many different ways to solve this depending on your programming skills in LUA (where you put timeouts on connections and responses) or in shell scripts … where you return right away but allow a sub process to do the communications in the background).[/quote]

Hi all

Reading through all the threads, and the various routes available via os.execute and io.popen plus it seems the added complication of the Lua syntax…

Has anyone come up with a foolproof way to send any ssh command, which is proven to work at the Vera command line…