AlexGustafsson / homebridge-wol

A Wake on Lan plugin for Homebridge
MIT License
301 stars 30 forks source link

shutdownCommand from Raspberry Pi to Windows #34

Closed Hidduh closed 6 years ago

Hidduh commented 6 years ago

Backstory

I'm trying to send a shutdownCommand to Windows to remotely shut down my computer through the Home app on my iPhone/Watch, however it returns an error when the shutdown command is sent by tapping the 'off' button in my Home app. Should be possible to get this working or am I doing something completely wrong?

Environment

Configuration

My configuration looks like this:

{
    "bridge": {
        "name": "Homebridge",
        "username": "<mac-address>",
        "port": 51826,
        "pin": "<snip>"
    },

    "description": "This is an example configuration file with one fake accessory and one$

    "accessories": [
        {
                "accessory": "NetworkDevice",
                "name": "Desktop",
                "mac": "<mac-address>",
                "ip": "192.168.178.10",
                "shutdownCommand": "shutdown -s -f -t 30 -mm \\192.168.178.10"
        },
        {
                "accessory": "Xbox",
                "name": "Xbox One",
                "ipAddress": "192.168.178.16",
                "liveId": "<snip>"
        }
    ]
}
IMPORTANT!
Remove any mac addresses from the configuration by 
exchanging them with <mac-address>

Log

When I follow these steps:

  1. Run DEBUG=* homebridge
  2. Let it run for five minutes
  3. Tap a switch
  4. Wait one minute
  5. Tap the same switch

I get the following log:

[4/12/2018, 9:25:49 PM] [Desktop] NetworkDevice shutdown cycle started for "Desktop" (192.168.178.10)
[4/12/2018, 9:25:49 PM] [Desktop] Stopping pinger
[4/12/2018, 9:25:49 PM] [Desktop] Starting pinger again in 15000 milli seconds
[4/12/2018, 9:25:49 PM] [Desktop] Attempting to shut down "Desktop" (192.168.178.10) using "shutdown -s -f -t 30 -mm \192.168.178.10"
[4/12/2018, 9:25:49 PM] [Desktop] NetworkDevice "Desktop" (192.168.178.10) went from status "Online" to "Shutting Down"
  EventedHTTPServer [::ffff:192.168.178.19] Sending HTTP event '2.10' with data: {"characteristics":[{"aid":2,"iid":10,"value":false}]} +61ms
  EventedHTTPServer [::ffff:192.168.178.22] Sending HTTP event '2.10' with data: {"characteristics":[{"aid":2,"iid":10,"value":false}]} +26ms
  EventedHTTPServer [::ffff:192.168.178.22] HTTP Response is finished +131ms
  EventedHTTPServer [::ffff:192.168.178.22] Writing pending HTTP event data +3ms
[4/12/2018, 9:25:49 PM] [Desktop] An error occured while trying to shut down "Desktop" (192.168.178.10): Error: Command failed: shutdown -s -f -t 30 -mm \192.168.178.10
shutdown: invalid option -- 's'

[4/12/2018, 9:25:49 PM] [Desktop] Stopping pinger
[4/12/2018, 9:25:49 PM] [Desktop] Starting pinger at an interval of 2000 milli seconds
[4/12/2018, 9:25:49 PM] [Desktop] Pinger toggled state change
[4/12/2018, 9:25:49 PM] [Desktop] NetworkDevice "Desktop" (192.168.178.10) went from status "Shutting Down" to "Online"
  EventedHTTPServer [::ffff:192.168.178.19] Sending HTTP event '2.10' with data: {"characteristics":[{"aid":2,"iid":10,"value":true}]} +163ms
  EventedHTTPServer [::ffff:192.168.178.22] Sending HTTP event '2.10' with data: {"characteristics":[{"aid":2,"iid":10,"value":true}]} +29ms

shutdown.exe with /:

[4/12/2018, 9:29:55 PM] [Desktop] NetworkDevice shutdown cycle started for "Desktop" (192.168.178.10)
[4/12/2018, 9:29:55 PM] [Desktop] Attempting to shut down "Desktop" (192.168.178.10) using "shutdown /s /f /t 30 /m \192.168.178.10"
[4/12/2018, 9:29:55 PM] [Desktop] NetworkDevice "Desktop" (192.168.178.10) went from status "Online" to "Shutting Down"
[4/12/2018, 9:29:55 PM] [Desktop] An error occured while trying to shut down "Desktop" (192.168.178.10): Error: Command failed: shutdown /s /f /t 30 /m \192.168.178.10
Failed to parse time specification: /s

[4/12/2018, 9:29:55 PM] [Desktop] NetworkDevice "Desktop" (192.168.178.10) went from status "Shutting Down" to "Online"

Notes

Typing out 'shutdown /s /f /t 30 /m \192.168.178.10' in cmd.exe on Windows works perfectly fine, so there's nothing wrong with the syntax.

AlexGustafsson commented 6 years ago

Are you running homebridge on a Windows computer?

The shutdown command is called on the same computer that hosts the server, which may change the way the command handles. I’m assuming you’re running homebridge on a unix-like server which usually doesn’t share the same or lack the shutdown command you’re trying to issue.

Hidduh commented 6 years ago

I'm running Homebridge on a Raspberry Pi.

I'm aware that I can't issue unix-like commands to Windows, however since I send the command to an IP on my local network with the correct windows params (/s /f /t /m \) I was under the impression it would work fine. But from what I understand it's not possible to achieve that?

Hidduh commented 6 years ago

Managed to solve this by using net rpc to shutdown Windows from Raspbian.. derp.

Thanks for the assistance.

AlexGustafsson commented 6 years ago

Great! That was what I was getting at with my somewhat unclear mention of differences in shutdown commands. Do you think that the documentation could be improved to make this more clear?

Right now it may not be obvious where the command is excecuted. Perhaps a shutdown example for Windows should be added to the configuration example already available. Any thoughts?

Hidduh commented 6 years ago

It'd be great if there was some sort of mention of how users running some sort of Linux distro can send (shutdown)commands to a Windows machine. Could definitely save them some time before having to resort to Google for information.

As an example, I used the following command which I send through Homebridge on my Raspberry Pi to my desktop PC running Windows 10. Be aware that the samba-common package is required in order to achieve this. (install via apt-get)

        {
                "accessory": "NetworkDevice",
                "name": "Desktop",
                "mac": "<mac>",
                "ip": "192.168.178.10",
                "shutdownCommand": "net rpc shutdown --ipaddress 192.168.178.10 --user <username>%<password>
        }

Note: if you're on Windows 10 and you're signing in with a Microsoft account, sign in using your local username instead of your Microsoft ID (e-mail).

AlexGustafsson commented 6 years ago

Great! I more or less copied your command and note and they're now available beside the other examples and notes in the readme. Thank you for your help.