Remotely Suspend or Resume Windows 10 PC from Linux

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Remotely Suspend or Resume Windows 10 PC from Linux

Post by ben53252642 »

Script I'm using to suspend / sleep my Windows 10 desktop PC from Linux and resume / wake it on demand.

Example usage from bash terminal:
./control.sh "wake"
./control.sh "sleep"

Requirements:
1) apt-get install etherwake sshpass fping
2) Make sure Wake on LAN is enabled in the PC's BIOS
3) Setup OpenSSH server on the Windows 10 PC.

control.sh

Code: Select all

#!/bin/bash

# Windows 10 PC Configuration
ip=""
mac=""
username=""
password=""

# Send WOL packet to PC (and repeat for up to 10 seconds if unable to ping the IP)
if [[ $1 == 'wake' ]]; then
wakeonlan "$mac" && timeout 10 bash -c -- "until fping -c1 -b 32 -t1000 $ip &>/dev/null; do wakeonlan $mac ; done"

elif [[ $1 == 'sleep' ]]; then
timeout 5s sshpass -p "$password" ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no "$username"@"$ip" 'powercfg -h off & rundll32.exe powrprof.dll,SetSuspendState 0,1,0'
fi

Example Lua device script:

Code: Select all

commandArray = {}

if (devicechanged["Desktop PC"] == 'On') then
os.execute ("/root/scripts/pcpowercontrol/control.sh 'wake' &")
end

return commandArray
Useful Windows commands (run in cmd)
  • See last device to wake the pc: powercfg -lastwake
  • See list of devices allowed to wake the pc: powercfg -devicequery wake_armed
  • Disable a device from being allowed to wake the pc: powercfg -devicedisablewake "DEVICE_NAME"

Notes:
1) The command disables Windows 10 hibernation before issuing the sleep command (I want the Windows 10 PC to sleep, not hibernate)
2) SSH certificate verification is disabled which may be a security concern in some situations. You can easily enable it by changing "StrictHostKeyChecking=no" to "StrictHostKeyChecking=yes"
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest