Skip to content

Instantly share code, notes, and snippets.

@filewalkwithme
Created June 4, 2012 00:01
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save filewalkwithme/2865500 to your computer and use it in GitHub Desktop.
Lua Wake-On-Lan Script
function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
mac_dest = arg[1]
socket = require("socket")
ip="255.255.255.255"
port=9
udp = assert(socket.udp())
udp:setoption('broadcast', true)
mac_array=split(mac_dest, ":")
mac = ""
for i,v in ipairs(mac_array) do mac = mac..string.char(tonumber("0x"..v)); end
mac1=""
for i=1,16 do
mac1 = mac1..mac
end
mac2 = string.char(0xff,0xff,0xff,0xff,0xff,0xff)..mac1
assert(udp:sendto(mac2, ip, port))
print("Wake-On-Lan packet sent to MAC address "..arg[1])
@filewalkwithme
Copy link
Author

This script sends an Wake-On-Lan magic packet to a given target Mac Address. If everything succeed, the target machine should power on.

Usage:

$ lua wol.lua AA:BB:CC:DD:EE:FF
Wake-On-Lan sent to MAC address AA:BB:CC:DD:EE:FF

  • By Maiconio [pistaches]

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