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 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 broadcast = s:option(Flag, "broadcast",
53 translate("Send to broadcast address"))
55 broadcast:depends("binary", "/usr/bin/etherwake")
59 function host.write(self, s, val)
60 local host = luci.http.formvalue("cbid.wol.1.mac")
61 if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
63 local util = luci.http.formvalue("cbid.wol.1.binary") or (
64 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
67 if util == "/usr/bin/etherwake" then
68 local iface = luci.http.formvalue("cbid.wol.1.iface")
69 local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
70 cmd = "%s -D%s %s %q" %{
71 util, (iface ~= "" and " -i %q" % iface or ""),
72 (broadcast == "1" and " -b" or ""), host
75 cmd = "%s -v %q" %{ util, host }
78 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
79 translate("Starting WoL utility:"), cmd
82 local p = io.popen(cmd .. " 2>&1")
85 local l = p:read("*l")
87 if #l > 100 then l = l:sub(1, 100) .. "..." end
88 msg = msg .. l .. "<br />"
96 msg = msg .. "</code></p>"