This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

ssh to shut down a remote pc
#1
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
Reply
#2
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/20...mands.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
Reply
#3
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.
Reply
#4
(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?
Reply
#5
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)
Reply
#6
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
Reply
#7
(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...
Reply
#8
(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.
Reply
#9
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…
Reply
#10
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.
Reply


Forum Jump: