Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created July 14, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/d1dfba93834702dca40031e1e9532638 to your computer and use it in GitHub Desktop.
Save Hermann-SW/d1dfba93834702dca40031e1e9532638 to your computer and use it in GitHub Desktop.
minimal nodejs code to power on wakeonlan enabled PC with given mac address
// eslinted; send wakeonlan magic packet for mac address defined in m
//
const m = Buffer.from([0x9c, 0x6b, 0x00, 0x15, 0xbd, 0xc2]);
const p = Buffer.concat([Buffer.alloc(6,0xff),m,m,m,m,m,m,m,m,m,m,m,m,m,m,m,m]);
const sock = require('dgram').createSocket('udp4').on('listening', function() {
sock.setBroadcast(true);
sock.send(p, 0, p.length, 9, '255.255.255.255', function() {
sock.close();
});
}).bind();
@Hermann-SW
Copy link
Author

hermann@7600x:~$ sudo shutdown 0
[sudo] password for hermann: 
Shutdown scheduled for Fri 2023-07-14 12:16:11 CEST, use 'shutdown -c' to cancel.
hermann@7600x:~$ Connection to 7600x closed by remote host.
Connection to 7600x closed.
pi@pi400-64:~ $ nodejs wol.js 
pi@pi400-64:~ $ ping 7600x
PING 7600x.fritz.box (192.168.178.172) 56(84) bytes of data.
From pi400-64.fritz.box (192.168.178.67) icmp_seq=10 Destination Host Unreachable
From pi400-64.fritz.box (192.168.178.67) icmp_seq=11 Destination Host Unreachable
From pi400-64.fritz.box (192.168.178.67) icmp_seq=12 Destination Host Unreachable
64 bytes from 7600x.fritz.box (192.168.178.172): icmp_seq=13 ttl=64 time=1649 ms
64 bytes from 7600x.fritz.box (192.168.178.172): icmp_seq=14 ttl=64 time=648 ms
...

@Jibun-no-Kage
Copy link

I have found it of benefit to send magic packets 3 times, some times the first or second packet is lost, dropped, ignored, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment