1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local sys = require "luci.sys"
5 local ipc = require "luci.ip"
6 local fs = require "nixio.fs"
8 m = SimpleForm("wol", translate("Wake on LAN"),
9 translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
11 m.submit = translate("Wake up host")
15 local has_ewk = fs.access("/usr/bin/etherwake")
16 local has_wol = fs.access("/usr/bin/wol")
19 s = m:section(SimpleSection)
21 if has_ewk and has_wol then
22 bin = s:option(ListValue, "binary", translate("WoL program"),
23 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
25 bin:value("/usr/bin/etherwake", "Etherwake")
26 bin:value("/usr/bin/wol", "WoL")
30 iface = s:option(ListValue, "iface", translate("Network interface to use"),
31 translate("Specifies the interface the WoL packet is sent on"))
34 iface:depends("binary", "/usr/bin/etherwake")
37 iface:value("", translate("Broadcast on all interfaces"))
39 for _, e in ipairs(sys.net.devices()) do
40 if e ~= "lo" then iface:value(e) end
45 host = s:option(Value, "mac", translate("Host to wake up"),
46 translate("Choose the host to wake up or enter a custom MAC address to use"))
48 sys.net.mac_hints(function(mac, name)
49 host:value(mac, "%s (%s)" %{ mac, name })
53 broadcast = s:option(Flag, "broadcast",
54 translate("Send to broadcast address"))
56 broadcast:depends("binary", "/usr/bin/etherwake")
60 function host.write(self, s, val)
61 local host = luci.http.formvalue("cbid.wol.1.mac")
62 local mac = ipc.checkmac(host)
65 local util = luci.http.formvalue("cbid.wol.1.binary") or (
66 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
69 if util == "/usr/bin/etherwake" then
70 local iface = luci.http.formvalue("cbid.wol.1.iface")
71 local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
72 cmd = "%s -D%s %s %q" %{
73 util, (iface ~= "" and " -i %q" % iface or ""),
74 (broadcast == "1" and " -b" or ""), mac
77 cmd = "%s -v %q" %{ util, mac }
80 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
81 translate("Starting WoL utility:"), cmd
84 local p = io.popen(cmd .. " 2>&1")
87 local l = p:read("*l")
89 if #l > 100 then l = l:sub(1, 100) .. "..." end
90 msg = msg .. l .. "<br />"
98 msg = msg .. "</code></p>"