1 -- Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local sys = require "luci.sys"
5 local fs = require "nixio.fs"
7 m = SimpleForm("wol", translate("Wake on LAN"),
8 translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
10 m.submit = translate("Wake up host")
14 local has_ewk = fs.access("/usr/bin/etherwake")
15 local has_wol = fs.access("/usr/bin/wol")
18 s = m:section(SimpleSection)
20 if has_ewk and has_wol then
21 bin = s:option(ListValue, "binary", translate("WoL program"),
22 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
24 bin:value("/usr/bin/etherwake", "Etherwake")
25 bin:value("/usr/bin/wol", "WoL")
29 iface = s:option(ListValue, "iface", translate("Network interface to use"),
30 translate("Specifies the interface the WoL packet is sent on"))
33 iface:depends("binary", "/usr/bin/etherwake")
36 iface:value("", translate("Broadcast on all interfaces"))
38 for _, e in ipairs(sys.net.devices()) do
39 if e ~= "lo" then iface:value(e) end
44 host = s:option(Value, "mac", translate("Host to wake up"),
45 translate("Choose the host to wake up or enter a custom MAC address to use"))
47 sys.net.mac_hints(function(mac, name)
48 host:value(mac, "%s (%s)" %{ mac, name })
52 function host.write(self, s, val)
53 local host = luci.http.formvalue("cbid.wol.1.mac")
54 if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
56 local util = luci.http.formvalue("cbid.wol.1.binary") or (
57 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
60 if util == "/usr/bin/etherwake" then
61 local iface = luci.http.formvalue("cbid.wol.1.iface")
63 util, (iface ~= "" and " -i %q" % iface or ""), host
66 cmd = "%s -v %q" %{ util, host }
69 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
70 translate("Starting WoL utility:"), cmd
73 local p = io.popen(cmd .. " 2>&1")
76 local l = p:read("*l")
78 if #l > 100 then l = l:sub(1, 100) .. "..." end
79 msg = msg .. l .. "<br />"
87 msg = msg .. "</code></p>"