Author Topic: Is there luup code for sending ssh commands to a server?  (Read 19931 times)

Offline garrettwp

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6376
  • Karma: +226/-128
  • Vera 3, Lite, ISY994
Re: Is there luup code for sending ssh commands to a server?
« Reply #30 on: January 10, 2014, 11:01:31 am »
The -vvv option just gives a more details verbose logging. It is part of the -v option, just that adding additional v's will provide more details. You need to make sure that authorized_keys(2) is change to chmod 600 or 400.

- Garrett

Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #31 on: January 10, 2014, 11:41:06 am »
The usual way I copy ssh key is with the ssh-copy-id command. On the client after you have created your keys, run ssh-copy-id USERID@IP

It should prompt you for a password and then it will copy your public key over and exit back out. Then you can test with just regular ssh and you should be able to login. The following settings in sshd_config can affect logging in with root with or without a key.
PermitRootLogin
PasswordAuthentication
PubkeyAuthentication
StrictModes
AuthorizedKeysFile

There are more than just the above options but those are usually the gotchas, along with the Port option. You can take a look here for the other settings: http://www.openssh.org/cgi-bin/man.cgi?query=sshd_config

On another note, to do this the other way around you have to put a public key on Vera, at least in Vera Lite, the authorized_keys file goes into /etc/dropbear/

On the server you are trying to put your key on, make sure your .ssh folder is 0700 and that root owns the folder, then make all the files in that folder 0400.

To make those changes you can do.
Code: [Select]
chown -R root:root ~/.ssh
chmod 0700 ~/.ssh
chmod 0400 ~/.ssh/*

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #32 on: January 10, 2014, 02:57:08 pm »
well... I tried everything and.... It worked!  on my test VM.

So now I am going to figure out just what I did right and try on a production machine..

Thanks Guys!


Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #33 on: January 10, 2014, 02:57:58 pm »
If you need help, PM me and we can set up a WebEx.

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #34 on: January 10, 2014, 03:08:28 pm »
Thanks Gibby,

I am further.. just trying to get it fired off from luup code now

EDIT... YES!!!!!!!!!
os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 reboot")
worked,  all on my test VM.  This is so sweet 

Thanks again everyone!

And Now working on production Server..

So I think .. I didn't edit authorized_keys correctly  ::) and keys2 I deleted.



« Last Edit: January 10, 2014, 03:29:09 pm by jimpapa »

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #35 on: January 10, 2014, 04:48:43 pm »
On last hurdle...

I have:

os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now")

I need to execute that on two servers.. in my Lua I put that line in twice with the correct IP's 
I'm getting an error .. so clearly I am not formatting this correctly:
"ERROR : Error in lua for scenes and events"

I tested both servers separately so that logic works if you have one line... I just need a hand running that command twice:

os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now")
os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.105 shutdown -h now")

seems to be not a valid argument

« Last Edit: January 12, 2014, 08:59:01 am by jimpapa »

Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #36 on: January 10, 2014, 04:50:35 pm »
Try this
Code: [Select]
os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now &;ssh -i ~/.ssh/id_dss -l root 192.168.15.105 shutdown -h now &")

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #37 on: January 10, 2014, 04:59:07 pm »
Try this
Code: [Select]
os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now &;ssh -i ~/.ssh/id_dss -l root 192.168.15.105 shutdown -h now &")

I tried it, I got  no errors, But I ran the scene and the servers did not reboot  :(

Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #38 on: January 10, 2014, 05:05:00 pm »
Did you try running the below from the CLI on Vera?

Code: [Select]
ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #39 on: January 10, 2014, 05:32:15 pm »
Yes... From vera CLI and Lua I can reboot or shutdown the server now...
I just ned to figure out how to code shutting down the 2 servers

os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now")
os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.105 shutdown -h now")

gives me an error

Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #40 on: January 10, 2014, 05:37:27 pm »
but using just 1 of those lines works right?

If so, put your commands in a script on vera, then just call the script.

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #41 on: January 10, 2014, 05:50:08 pm »
right if i put

os.execute("ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now")
 
in a scene in vera  and call the scene by anything or just press RUN it totally works..
 

Offline gibby

  • Full Member
  • ***
  • Posts: 119
  • Karma: +3/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #42 on: January 10, 2014, 06:08:28 pm »
Ok so on the CLI of the vera just create a script /root/shutdown_esxi.sh
and put in it

Code: [Select]
#/bin/ash
ssh -i ~/.ssh/id_dss -l root 192.168.15.104 shutdown -h now &
ssh -i ~/.ssh/id_dss -l root 192.168.15.105 shutdown -h now &

Add execute to it with
Code: [Select]
chmod +x /root/shutdown_esxi.sh

Then in your scene put
Code: [Select]
os.execute("/root/shutdown_esxi.sh")

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Is there luup code for sending ssh commands to a server?
« Reply #43 on: January 10, 2014, 06:27:43 pm »
I took the easy way out.. and created another scene that is triggered off the same countdown timer
(Incase the power is restored.. countdown timer cancels the reboot)

so I have two separate scenes getting fired off of the one event and both servers are going down.. 
Both send a Pushover notification that they are going down etc. 

Works for me !

Thanks again everyone... no way would I have got this done without your help!

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1512
  • Karma: +189/-83
Re: Is there luup code for sending ssh commands to a server?
« Reply #44 on: March 08, 2014, 02:09:36 pm »
I am struggling still with getting the os.execute function to work on my mac server

I Created a public key on my Vera
I copied that into my authorized_keys on my Server
I also appended the key with "root@vera's_ip_address"

when I try to ssh from vera to mac, i get this error:

ssh: Connection to root@192.168.1.9:22 exited: No auth methods could be used.

I attached my permissions on client and server...

any assistance would be appreciated

« Last Edit: March 08, 2014, 07:32:34 pm by Bulldoglowell »