|
|
|
@ -24,7 +24,7 @@ local ERROR_PATTERN = getenv( |
|
|
|
|
|
|
|
-- Optional Wake-on-LAN |
|
|
|
local WOL_MAC = getenv("WOL_MAC", "") -- e.g. "AA:BB:CC:DD:EE:FF" |
|
|
|
local WOL_BCAST = getenv("WOL_BCAST", "255.255.255.255") |
|
|
|
local WOL_BCAST = getenv("WOL_BCAST", "192.168.222.255") |
|
|
|
local WOL_PORT = tonumber(getenv("WOL_PORT", "9")) |
|
|
|
|
|
|
|
-- Optional: wait for service to come up (kept commented to stay minimal) |
|
|
|
@ -46,14 +46,26 @@ local function send_wol(mac_str, bcast_ip, port) |
|
|
|
local mb = mac_to_bytes(mac_str) |
|
|
|
if not mb then return false, "invalid MAC" end |
|
|
|
local packet = string.rep(string.char(0xFF), 6) .. mb:rep(16) |
|
|
|
|
|
|
|
local udp = assert(socket.udp()) |
|
|
|
udp:settimeout(2) |
|
|
|
pcall(function() assert(udp:setoption("broadcast", true)) end) |
|
|
|
|
|
|
|
-- Optional: bind to a specific source IP (helps in host network with multi-NIC) |
|
|
|
local WOL_SRC_IP = os.getenv("WOL_SRC_IP") |
|
|
|
if WOL_SRC_IP and WOL_SRC_IP ~= "" then |
|
|
|
assert(udp:setsockname(WOL_SRC_IP, 0)) |
|
|
|
end |
|
|
|
|
|
|
|
-- IMPORTANT: must be true or Linux will return EACCES on broadcast sendto() |
|
|
|
assert(udp:setoption("broadcast", true)) |
|
|
|
|
|
|
|
local ok, err = udp:sendto(packet, bcast_ip, port) |
|
|
|
udp:close() |
|
|
|
return ok ~= nil, err |
|
|
|
if not ok then return false, err end |
|
|
|
return true |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- Kept for reference; not used to keep parity with your minimal bash |
|
|
|
-- local function port_is_up(host, port, timeout_sec) |
|
|
|
-- local deadline = socket.gettime() + (timeout_sec or 1) |
|
|
|
|