6 ms·
Waking My Computer from Afar
- muxator 3y agoNo big deal, but instead of running "wakeonlan" as a subprocess: import subprocess [...] result = subprocess.run(["wakeonlan", "MY:MA:CA:DD:RE:SS"], capture_output=True) The magic packet could be built and broadcast in pure python, without introducing a dependency on an external binary. For example, one could take inspiration from the code at https://github.com/remcohaszing/pywakeonlan/blob/main/wakeonlan/__init__.py https://github.com/remcohaszing/pywakeonlan/blob/main/wakeon...
- tekknolagi 3y agoI am very lazy :)
- tekknolagi 3y ago(Though, to be clear, I do like the idea of doing it purely in Python!)
- iam-TJ 3y agoI wrote a pure bash shell version for use on my IPv6-only networks. I initially wanted to make pure POSIX sh but the ease of being able to use ${param,,} and more importantly /dev/udp/$IP/$PORT makes bash more suitable for the use-case. #!/usr/bin/env bash config_sys="${config_sys:-/etc/wol}" config_user="${config_user:=$HOME/.config/wol}" PORT="${PORT:-9999}" cat <<EOF WoL Wake-On-LAN Magic Packet transmitter Copyright 2023 Tj <hacker@iam.tj> Licensed on the terms of the GNU General Public Licence version 3. EOF ls() { shopt -s nullglob for f in $@; do printf "%s\n" "${f##*/}" done } if [ -z "$1" ]; then cat <<-EOF Please specify the hostname. Currently defined: System ($config_sys/): $(ls "$config_sys/*" 2>/dev/null) User ($config_user/): $(ls "$config_user/*" 2>/dev/null) Config files should be named for the target host Content is shell variable definitions Required: IP= and MAC= Optional: PORT= (default is ${PORT}) E.g: MAC="60:45:cb:9f:42:fb" IP="fddc:7e00:e001:ee80::2" EOF exit 1 fi for conf in "$config_sys" "$config_user"; do if [[ -r "$conf/$1" ]]; then . "$conf/$1" # don't break; allow user override fi done if [[ -z "$IP" ]] || [[ -z "$MAC" ]]; then >&2 printf "Error: required configuration missing IP='%s' MAC='%s'\n" "$IP" "$MAC" exit 2 fi target="/dev/udp/$IP/$PORT" hex2char() # $1 = 2 hex digits { case "${#1},${1,,}" in (2,*[!0-9a-f]*) return 2 ;; (2,*) printf '%b' "\x$1" ;; (*) return 1 ;; esac } make_payload() # $1 = Ethernet MAC address with ":" separators { IFS=":" read -r -a mac < <(echo "$1") for h in "${mac[@]}"; do magic="$magic$(hex2char "$h")" done payload="" ff="$(hex2char "ff")" for ((i=0; i < 6; i++)); do payload="${payload}${ff}" done for ((i=0; i < 16; i++)); do payload="${payload}${magic}" done } printf "Sending magic packet to %s (%s port %s [MAC %s])\n" "$1" "$IP" "$PORT" "$MAC" make_payload "$MAC" printf "%s" "$payload" > "$target" || printf "error: failed to send (error %s)\n" "$?"
- politelemon 3y ago> I added the Pi to my Tailscale network (no, this is not an ad; just happy), which was pretty painless. I had a quick look at Tailscale and it seems to be a VPN network that you get to manage? So it's still a VPN just with convenience functions to make adding devices easy is that about right? Where does the DNS for this exist, is it on Tailscale's own servers. There must be some purpose to having to login. Does Tailscale also hold some connection open to figure out which devices are on or not. In a way this reminds me of AWS SSM Agent. I think it's talking to some central Amazon servers and I don't need to open a port on an EC2.
- zrail 3y agoTailscale is a mesh overlay network that uses Wireguard under the hood. Traffic moves peer to peer as much as possible. DNS happens locally (the special 100.100.100.100 address actually points at the tailscale daemon running on each machine, which already holds the name -> address mapping that MagicDNS needs to work). You login to Tailscale from each device in order for the central Tailscale control plane to authenticate the device and for it to distribute that device's public Wireguard key out to the rest of the mesh.
- CharlesW 3y agoI haven't personally used this, but many folks on HN also like that there's an open-source, self-hosted implementation of the control server. https://github.com/juanfont/headscale https://github.com/juanfont/headscale
- angott 3y agoTailscale runs its own local DNS resolver on your computer at 100.100.100.100, if you have enabled that in settings. Your DNS queries don't go to any Tailscale servers. By default, the client simply forwards all queries to the system DNS resolver, providing some added conveniences like automatic DNS over HTTPS upgrade when available. Also, you don't really have to use Tailscale for DNS if you're comfortable reaching your devices using their 100.x.y.z IP address. See https://tailscale.com/kb/1081/magicdns https://tailscale.com/kb/1081/magicdns for more details. And yes, Tailscale keeps a connection open to its servers to allow other devices to reach you when you're behind a firewall and direct connections can't be established.
- doctorhandshake 3y agoThis is my setup, without the GUI. I join the Tailscale network on my laptop, ssh into my pihole (slash homelab misc) pi, and run ‘wol’. I also have the PC set to boot in the early hours (in BIOS) in case I’m away for a long time and accidentally pick ‘shut down’ instead of ‘sleep’ at some point.
- ranger_danger 3y agoPeople shut down their computers? Are there places in the world where people have to actually care about the power consumption of their computer so much that this is a thing? Any examples?
- deleted 3y ago[deleted]
- dist-epoch 3y agoI know this is probably extremely surprising for you, but some people do the little things which help the environment even if it causes them mild inconvenience.
- GaryHak 3y agoPutting my systems to sleep at night saves me € 400,- a year. Enough to buy a mini pc to watch the fortress.
- pohuing 3y agoIt takes 20 seconds to boot and it warms up my room if on. So why would I keep it on all the time. I turn it on when I use it and turn it off when I don't.
- tarxvf 3y agoMany people with homelabs - or a NAS with a ton of disks - don't use it all that often and it can cost quite a bit in electricity over the course of a month. Or maybe they live in an apartment and prefer quiet over the noise of fans and disks.
- FridgeSeal 3y agoBecause it costs money to run and I’m not made of money? It might be cherish, but money I don’t have to spend is even better. It’s also better for the environment(!!) and I like my working space to be as quiet as possible.
- avh02 3y agoSome people live in places with rolling blackouts and the UPS doesn't last long enough, so easier to shutdown cleanly and power off the UPS so you're not constantly cycling the battery for no good reason.
- stzsch 3y agoI used a similar setup to reboot my home server remotely, with gpio pins instead of wakeonlan. I eventually replaced it with an aliexpress tuya pcie power switch. I just wish I'd gotten the zigbee version. Homeassistant with local tuya wifi devices is quite janky.
- aktuel 3y agoAnd how is this better or easier than just setting up the port forwarding on the router?
- tekknolagi 3y agoa) Come on, be kind b) I don't have to think about doing systems administration on the very resource-limited stripped-down OpenWRT device c) It's probably marginally more secure
- aktuel 3y agoSorry, I don't want to be rude. If you are happy with your setup that is really all that matters. I was just seriously puzzled that's all. If someone has an OpenWrt router instead of something off the shelf, I would assume that they have it exactly for the tinkering and setting up port forwarding should be a piece of cake. Though I don't have a OpenWrt device myself so maybe this is just me being naive. On my MikroTik router this is pretty easy once you know how to do it :) Security-wise I prefer the port forwarding because there is no third party involved and less complexity in general, but sure, you can find pros and cons here and there.
- tekknolagi 3y agoI've tried. But the Linux environment is pretty limited and Luci has stumped me more than once over the years. It's also slightly riskier: if I bork the router, it may take me (a significant amount of) time to get my network back up and running.
- IshKebab 3y agoHave you ever actually tried that? I have. It's possible with some router, but IIRC I had to install some custom config file from some random site. Definitely wasn't easy. I think with most routers it isn't possible at all. If you're imagining you just forward the port to your computers IP address... no that doesn't work. Because while your computer is asleep it doesn't have an IP address. What you need to do is configure the router to forward broadcast packets to the local subnet. Something like that anyway, I can't remember the exact details.
- wzdd 3y agoI recently had a similar problem for my Linux-based desktop, but found that it didn't support wake on LAN. So instead I wired an ESP8266 to the ATX power pins on the motherboard (via an opto-isolator, in parallel with the actual switch so both could be used). Code on the esp8266, which connects to a network server, can then "depress" the power line for a configurable number of milliseconds. Apart from working on my PC, this is also quite nice as it gives you a way to force-power-off remotely if necessary.
- nick238 3y agoI converted an old system to a server, and the WOL was super unreliable (poor mobo support, crappy network, ??). Wound up getting a PiKVM setup for $100 + RPi5 (Geekworm KVM-A8 Kit "PCIe" Version) which works pretty well, if perhaps overkill. The "official" PiKVM hardware is a bit more expensive (~$300 but also has a fancy steel case and HDMI passthrough) There's a BliKVM "card" which also looks interesting and I'd probably use if I did it again because I was trying to fit the card above the GPU and it took some modding.
- leptons 3y agoI did something similar recently. I wanted to wake my laptop via wifi. There's no way to do it using WoL with the laptop's wifi card. For some reason the laptop's wifi card won't be powered during sleep or never responds to WoL packets, but the USB port is powered during sleep. I got an ESP32-S3 which has built-in USB. I cobbled together the examples from the ESP-IDF framework. Using one wifi example I got the ESP32 to detect WoL packets sent to it, and the other example was the USB HID example that acts like a mouse and moves the cursor around. So now I can send a WoL request to the ESP32, and it will then "move the mouse" and wake the laptop. It worked like a charm and only took me about 2 evenings to put together.
- ahartmetz 3y agoOn my desktop, wake on LAN stopped working after a BIOS update, so I set it to "power on after power fail" (when the power comes back) and plugged it into a WiFi switchable power socket on which I installed the Tasmota firmware. Power outages and therefore undesired activation are extremely rare where I live. Probably less than one in five years.
- scottlamb 3y ago> Now, you might be wondering to yourself “Max, you said you weren’t running as root. How are you binding to port 80?” Well, sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/busybox. But you could also run on an unprivileged port like 8080. As you're launching from systemd anyway, I'd try something like `AmbientCapabilities=CAP_NET_BIND_SERVICE` in the unit file (caveat: untested). Then it's scoped to that unit, where it's (1) needed and (2) clear why it's needed. Instead of the busybox executable that (1) likely is used in a bunch of places where that's unneeded and (2) may get reinstalled by the package manager without this perm, quietly breaking your setup. systemd also supports binding the port for you, but afaict from a quick skim, Busybox httpd doesn't support accepting the socket from systemd, so that wouldn't be as easy a change.
- tekknolagi 3y agoOh, neat! Thanks. I didn't know about AmbientCapabilities.
- squarefoot 3y agoThis seems a use case where the smallest the better since the small system only has to close a contact according to a condition on the local network. This could also be done with a very small uC with Ethernet (example: Arduino and others + w5100 or compatible Ethernet chip; I'm not a fan of WiFi in these contexts). There is also a SSL library that can be used to run a minimal secure web server to host the magic button that would trigger the switch on through the gpio. https://www.arduino.cc/reference/en/libraries/ethernetwebserver_ssl/ https://www.arduino.cc/reference/en/libraries/ethernetwebser... Or it could employ some sort of port knocking on the uC+Eth board so that it wakes up the main system only when the right sequence is used. The advantage of using a uC, lower power draw aside, is that micro SD cards (and their sockets) are less reliable than internal flash.
- deleted 3y ago[deleted]
- y04nn 3y agoOne solution that I use and like because it is reliable, is to change the BIOS to "Power On" after a power failure and use a smart plug to turn on the computer. So turning the smart plug on my phone on would start the computer. I use it for my backup server with a JBOD that is only turned on when I need it. You can also schedule on/off time on the app. And if you want to programmatically turn it on/off you can setup Home Assistant with the plug.
- theflyingkiwi42 3y agoIt's what we use for our simulator center computers. Works like a charm!
- fastasucan 3y agoThis is a really good solution for the problem, imo. How do you power off without cutting electricity completely? Or do you power off computer then the socket?
- y04nn 3y agoYes, turn off the computer like you would normally to it, then turn off the socket.
- matthewfcarlson 3y agoI just use the wakeonlan plugin for Homebridge. Just tell Siri to do it or press a button on my phone. Works anywhere as long as I have internet.
- natrys 3y agoDid something similar when I was behind CGNAT and had no IPv6 or port forwarding. Instead of a pi, I cheaped out even more and used an old android phone. Wrote a little Rust program that listened on a ntfy[1] channel, cross-compiled with cargo-ndk[2] and ran it in termux (which had the wol tool, a perl script that sends magic packet). The web UI simply pinged the channel (and showed the pong). [1] https://ntfy.sh/ https://ntfy.sh/ [2] https://github.com/bbqsrc/cargo-ndk https://github.com/bbqsrc/cargo-ndk