Reboot computer on same network with vera

Hello ,

I would like to restart a computer with a scene from vera.
For example running a batch file when the scene is activated . Or maybe there is a better way.

But how would I do that?

Thanks ,
Cor

I think you’d have to setup a basic API that runs a restart script or shell command as administrator when it receives a command. In the scene, you’d have to code the command to run an api on the computer you’re wanting to reboot. You can probably google how to do each step and then tie them together.

One approach I guess you could take is to run a simple HTTP server on the PC. In python for example,you could try something like

[code]class myHandler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write("OK")
    try:
        #your code to parse the URL and activate reboot if that command is sent would go here
    except Exception as ex:
        print ex
        pass
    return

[/code]

In your scene, Vera could do something like:

luup.inet.wget("http://<IP address:port>?command=reboot")

One thing you’d probably need to look at is security. On Linux, you would not want to run the server as root (maybe use sudo?) and as that stands, you could reboot using the URL from any PC on the LAN.

??? ???

@ Jonbanjo: thanks for your info , but I have no idea how to run a http server on my windows 10 machine and what to do with that code.

@jlind: , also that is way beyond my programming skills I think :-\

I was hoping I could use this

shutdown -m \ipadres -t 0 -r -f

http://www.howtogeek.com/109655/how-to-remotely-shut-down-or-restart-windows-pcs/

But I don’t know if it is possible to insert this in a scene somehow.

Cor

Sorry I can’t help then Cor.

Just for info. Python is a language that I sort of take for granted is pretty much always already available but I’m on Linux… A more complete example of a python http server is at http://www.acmesystems.it/python_httpd

Anyway, I hope someone else can come up with a solution more suited to you.

Maybe a bit of a tangent but I suppose a question is why do you want to reboot? If for some reason you want say a nightly reboot, you might consider what scheduling options your OS has instead (on Linux, cron would do that but I don’t know about Windows).

I am away from home a lot , and for that reason I run about 14 cameras with blue Iris which runs 24/7

If I need to do something at home , I normally use teamviewer which works very well , and I can also reboot the computer via teamviewer ( windows 10) But sometimes it doesn’t work , the computer is still connected to the internet , but dropbox and teamviewer are not working, a simple reboot does the trick.

But to reboot the computer every night , that would be a bit too much. To activate a scene to reboot on those occasions , that would be just good.

I was hoping that I could just run this command via a scene and a bit of Luup, like a http command:
Code:

shutdown -m \ipadres -t 0 -r -f

http://www.howtogeek.com/109655/how-to-remotely-shut-down-or-restart-windows-pcs/

But as a non programmer , I have absolutelu no idea , also that complete example of a python server , it is abracadabra for me.

If a simple solution is not possible I think I will put the computer on a wallplug, when the power cuts off , it restarts automatically again. But a neater solution would be when it could be with a proper reboot command.

Cor

I’ve tried a bit of searching for you. Lua does provide a couple of methods to run a system command.

local result = os.execute("command")

This should run “command”, result should hold the return value - usually 0 for a successful command

local handle = io.popen("command") local result = handle:read("*a") handle:close()

In this version, result should hold the output for the command, eg. for ls.

That bit seems easy but I don’t seem to have net or shutdown on my Vera Plus and I don’t fancy trying to install software on it.

@ Jonbanjo;

Vera “talks” to that computer allready to change some settings for day and nighttime.
This is a code I use in a scene:
luup.inet.wget(“http://ipadress:port/admin?profile=1&user=xxxxxxr&pw=xxxxxxxx”)

But that is specific for that programm , You can not use that luup.inet.wget to run a batch file in the computer?

Cor

[quote=“Cor, post:9, topic:194795”]Vera “talks” to that computer allready to change some settings for day and nighttime.
This is a code I use in a scene:
luup.inet.wget(“http://ipadress:port/admin?profile=1&user=xxxxxxr&pw=xxxxxxxx”)

But that is specific for that programm , You can not use that luup.inet.wget to run a batch file in the computer?[/quote]

Assuming the luup version is like standard wget, it just supports http, https and ftp.

What does it mean , that this shutdown command can be inserted somehow in this luup.inet.wget?
Cor

That you would need to use an http server with it.

If you don’t mind having python installed, you could try this rough and ready attempt:

[code]import subprocess
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8081

class myHandler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write("OK")
    try:
        self.wfile.flush()
        self.wfile.close()
        self.rfile.close()
    except:
        pass
    try:
        p = urlparse.urlparse(self.path)
        query = urlparse.parse_qs(p.query)
        command = query['command'][0]
        if command == "reboot":
            subprocess.call(["shutdown", "-r"])
    except Exception as ex:
        print ex

try:
server = HTTPServer((’’, PORT_NUMBER), myHandler)
print 'Started httpserver on port ’ , PORT_NUMBER

server.serve_forever()

except KeyboardInterrupt:
print ‘^C received, shutting down the web server’
server.socket.close()
[/code]

Save it as say reboot.py and run it using python reboot.py

This will reboot Windows 10 on my dual boot laptop, at least when I’m logged in. For my laptop at the moment:

http://172.23.41.161:8081/?command=reboot

I think I am an admin user… I’d have to leave you to look at Windows specifics and this is probably as far as I can go with this - need to get on with other things.

@Jonbanjo.

hmmm , I installed the python programm on my computer.
opened idle
copy pasted your code , saved it as reboot.py
pressed F5 , but I get an error message.

see attached sceenshot
what am I doing wrong?

Thanks,
Cor

OK, you shouldn’t need to use an IDE to run it (although not a bad idea if you want to look at the code, debug, etc.)

The problem seems to be that my code is for Python 2 (2.7) and you are using a Python 3 version…

If you want to stick with that version, try using brackets with print,

eg. print ex needs to become print(ex)

Watch the indenting if you do edit. Python needs the code indented as it is there and will complain if things get out of line.

Installed the other version of python and it is working on my notebook!!!
cool! ;D

This evening I will install it on the computer where it is intended for.

One more question , what would be the best option to start this http server with this reboot.py file?

use the windows scheduler?
what would I insert here to run the reboot.py file: ( see attachment)

Many thanks for your help,
Cor

I’d guess something like the screenshot (don’t forget to add the name of the script) probably is the way but I really am out of touch with Windows. The last version used regularly here was Win2K. I keep a copy on the Laptop as a just in case (a home automation example would be to update firmware on an RFXCom) but I don’t really go further than that sort of thing with it these days.

It’s working , wasn’t hard at all.
Thanks a lot.

For people who whant to do the same:

-download and install Pyhton version 2.7
-save this code as reboot.py

[code]import subprocess
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8081

class myHandler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write("OK")
    try:
        self.wfile.flush()
        self.wfile.close()
        self.rfile.close()
    except:
        pass
    try:
        p = urlparse.urlparse(self.path)
        query = urlparse.parse_qs(p.query)
        command = query['command'][0]
        if command == "reboot":
            subprocess.call(["shutdown", "-r"])
    except Exception as ex:
        print ex

try:
server = HTTPServer((’’, PORT_NUMBER), myHandler)
print 'Started httpserver on port ’ , PORT_NUMBER

server.serve_forever()

except KeyboardInterrupt:
print ‘^C received, shutting down the web server’
server.socket.close()[/code]

-Make a task in windows scheduler to run when the computer starts, run “reboot.py”
-Run the schedule manually first, I got a message from windows firewall it blocked, but clicking once “allow” was good enough for future times.

-make a scene ; luup code: luup.inet.wget("http://Ipadres:8081/?command=reboot")

Cor

Obviously this remains a hack and although I don’t intend taking things further,one could still consider the comments I made in my first post, question whether than rather than try to dismiss Vera quickly with an OK, some greater degree of feedback would be better, look at better logging than the command line prints, etc. etc.

This attempt does try to be a little bit more flexible though

[code]import subprocess
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

commands = {
“reboot”: [“shutdown”, “-r”],
“shutdown”: [“shutdown”, “-h”]
}

PORT_NUMBER = 8081

class myHandler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write("OK")
    try:
        self.wfile.flush()
        self.wfile.close()
        self.rfile.close()
    except:
        pass
    try:
        p = urlparse.urlparse(self.path)
        query = urlparse.parse_qs(p.query)
    except:
        print "error parsing query"
        return
    try:
        command = query['command'][0]
    except:
        print "command paramater missing in URL"
        return
    try:
        toCall = commands[command]
    except:
        print command, " is not defined in python script"
        return
    try:
        subprocess.call(commands[command])
    except Exception as ex:
        print "error ", command, ":", ex

try:
server = HTTPServer((’’, PORT_NUMBER), myHandler)
print 'Started httpserver on port ’ , PORT_NUMBER

server.serve_forever()

except KeyboardInterrupt:
print ‘^C received, shutting down the web server’
server.socket.close()[/code]

You should be able to set up your own command(s) by editing the bit between the curly braces in the commands= section near the top. Each line except the last one should have a comma at the end. The format is:

For a program with no arguments:

“urlcommand”: [“program”]

For a program with one argument:

“urlcommand”: [“program”, “arg1”]

etc.

where urlcommand is what you want to use with command= in the url

@ Jonbanjo,
Thanks, but I leave it how it is. It is for me more as sufficient .

Again many thanks for help , it’s great to have full controll now when I am on the other side of the world

Cor

Sure, whatever works for you and you feel happy with.

OT but other side of the world!!! Just had relation from Australia stop with us (UK) for a few days but me, I don’t go very far.