Logic Machine Forum
ssh to shut down a remote pc - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: ssh to shut down a remote pc (/showthread.php?tid=176)



ssh to shut down a remote pc - managementboy - 04.01.2016

Hi... 

after watching a Movie on our HTPC we want to have the LM turn of the HTPC. I thought ssh + shutdown could do the trick, or is there a better / nicer way. The HTPC has a version of Ubuntu on it (not Windows).

Thanks


RE: ssh to shut down a remote pc - jorgenbl - 04.01.2016

Hi,

I'm running some shell commands on a unix box via http. Not the best (and secure) solution, but an option if nobody has a better one. Smile
http://thinkinginsoftware.blogspot.no/2011/09/nodejs-server-to-run-shell-commands.html

Event script to call command:

require('socket.http')
if (event.getvalue()) then
 url = 'http://x.x.x.x:8088/?cmd=/home/pi/tdtool.py%20--on%XXXXXXXX'
res, err = socket.http.request(url)
else
 url = 'http://x.x.x.x:8088/?cmd=/home/pi/tdtool.py%20--off%XXXXXXXX'
res, err = socket.http.request(url)
end

You could probably shutdown your server with something like:
 url = 'http://x.x.x.x:8088/?cmd=shutdown%20-h%20now'
res, err = socket.http.request(url)

Where x.x.x.x is your HTPC with node installed and the script in the url I posted running.

Jørgen


RE: ssh to shut down a remote pc - admin - 05.01.2016

HTTP is a quick solution, for SSH to work from Lua you would have to use key auth, as you cannot pass the password directly. We can also provide sshpass utility but it will take some time.


RE: ssh to shut down a remote pc - managementboy - 05.01.2016

(05.01.2016, 07:37)admin Wrote: HTTP is a quick solution, for SSH to work from Lua you would have to use key auth, as you cannot pass the password directly. We can also provide sshpass utility but it will take some time.

I would love to be able to use the ky auth... any example on how to do it in lua?


RE: ssh to shut down a remote pc - admin - 06.01.2016

First you need to generate your private key and extract public key from that, create a script and run it once:
Code:
key = '/lib/genohm-scada/mnt/storage/id_rsa'
os.execute('dropbearkey -t rsa -f ' .. key)
keyinfo = io.readproc('dropbearkey -y -f ' .. key)
log(keyinfo)

In logs you will see a line like "ssh-rsa AAAAB3Nz...", you have to copy it to authorized_keys (or authorized_keys2) file on your HTPC.

Then you can run any command via SSH which does not require user input:
Code:
key = '/lib/genohm-scada/mnt/storage/id_rsa'
user = 'root'
ip = '192.168.1.100'
port = 22
cmd = 'ls'

run = string.format('ssh -y -i %s %s@%s/%d %q', key, user, ip, port, cmd)
result = io.readproc(run)
log(result)



RE: ssh to shut down a remote pc - managementboy - 19.01.2016

Thanks! here are two user functions anyone can copy/paste to reuse the above code

Code:
--Functions to run ssh on the LM

--Run a ssh command on a remote system
function sshcommand(user, ip, port, cmd)
 key = '/lib/genohm-scada/mnt/storage/id_rsa'
 run = string.format('ssh -y -i %s %s@%s/%d %q', key, user, ip, port, cmd)
 result = io.readproc(run)
 log(result)
end

--Run once to create key
--copy into the file authorized_keys on system to be logged in
function sshcreatekey()
 key = '/lib/genohm-scada/mnt/storage/id_rsa'
 os.execute('dropbearkey -t rsa -f ' .. key)
 keyinfo = io.readproc('dropbearkey -y -f ' .. key)
 log(keyinfo)
end



RE: ssh to shut down a remote pc - baggins - 29.10.2016

(06.01.2016, 07:52)admin Wrote: First you need to generate your private key and extract public key from that, create a script and run it once:
Code:
key = '/lib/genohm-scada/mnt/storage/id_rsa'
os.execute('dropbearkey -t rsa -f ' .. key)
keyinfo = io.readproc('dropbearkey -y -f ' .. key)
log(keyinfo)

In logs you will see a line like "ssh-rsa AAAAB3Nz...", you have to copy it to authorized_keys (or authorized_keys2) file on your HTPC.

Does this work on firmware 20160714?
I tried to run the script to generate a key but nothing is returned in the log...


RE: ssh to shut down a remote pc - baggins - 30.10.2016

(29.10.2016, 10:40)baggins Wrote:
(06.01.2016, 07:52)admin Wrote: First you need to generate your private key and extract public key from that, create a script and run it once:
Code:
key = '/lib/genohm-scada/mnt/storage/id_rsa'
os.execute('dropbearkey -t rsa -f ' .. key)
keyinfo = io.readproc('dropbearkey -y -f ' .. key)
log(keyinfo)

In logs you will see a line like "ssh-rsa AAAAB3Nz...", you have to copy it to authorized_keys (or authorized_keys2) file on your HTPC.

Does this work on firmware 20160714?
I tried to run the script to generate a key but nothing is returned in the log...

I've figured out that the path has changed to /lib/genohm-scada/storage/...
I now have a key.


RE: ssh to shut down a remote pc - Igor - 25.06.2020

Hello,
I would like to ask for help connecting from Logic Machine to Mikrotik router.
I did all steps above (replacing file path to /lib/genohm-scada/storage/), but still unable to connect.
I repeated similar procedure with Mac PC and connection was successful, so Mikrotik part configuration seems working.
Tested if key present with io.exists(path), received true value. 
Mikrotik LOG says connection dropped. If I change key path in Logic Machine script - error in Mikrotik log remains same, so I suppose Mikrotik do not get correct key…


RE: ssh to shut down a remote pc - admin - 25.06.2020

There seems to be some kind of incompatibly between available ciphers in LM SSH client and Mirkotik SSH server. We'll have a look later.