Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created July 13, 2019 17:25
Show Gist options
  • Save kosh04/420a7c84b965f211881c3d2aecedfd06 to your computer and use it in GitHub Desktop.
Save kosh04/420a7c84b965f211881c3d2aecedfd06 to your computer and use it in GitHub Desktop.
Wake on LAN
#!/usr/bin/env newlisp
;; Wake-on-LAN
(define (usage)
(println "Usage: wol.lsp <IP address> <MAC address>"))
;; Convert MAC address to List format.
;; e.g."EE:EE:EE:00:00:01" to (0xEE 0xEE 0xEE 0x00 0x00 0x01)
(define (mac2list mac)
;; XX-XX-XX-XX-XX-XX
(when (regex "\\A..-..-..-..-..-..\\Z" mac)
(setq mac (replace "-" mac ":")))
(unless (regex "\\A([[:xdigit:]]{2}):(?1):(?1):(?1):(?1):(?1)\\Z" mac)
(throw-error (cons "Invalid MAC address format" mac)))
(map (lambda (hex)
(int hex 0 16))
(parse mac ":")))
;; Create magic packet
;; e.g. FF:FF:FF:FF:FF:FF + EE:EE:EE:00:00:01 * 16 (= 102 bytes)
(define (packet mac)
(append
(pack "cccccc" (mac2list "ff:ff:ff:ff:ff:ff"))
(dup (pack "cccccc" (mac2list mac)) 16)))
(define (main _nl _script addr mac)
(unless (and addr mac)
(usage)
(exit 1))
(let ((port 9))
(net-send-udp addr port (packet mac) true)
(when (net-error)
(println "error=" (net-error))
(exit 1)))
(exit))
(apply main (main-args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment