1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local utl = require "luci.util"
5 local sys = require "luci.sys"
6 local ipc = require "luci.ip"
7 local fs = require "nixio.fs"
9 m = SimpleForm("wol", translate("Wake on LAN"),
10 translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
12 m.submit = translate("Wake up host")
16 local has_ewk = fs.access("/usr/bin/etherwake")
17 local has_wol = fs.access("/usr/bin/wol")
20 s = m:section(SimpleSection)
22 if has_ewk and has_wol then
23 bin = s:option(ListValue, "binary", translate("WoL program"),
24 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
26 bin:value("/usr/bin/etherwake", "Etherwake")
27 bin:value("/usr/bin/wol", "WoL")
31 iface = s:option(ListValue, "iface", translate("Network interface to use"),
32 translate("Specifies the interface the WoL packet is sent on"))
35 iface:depends("binary", "/usr/bin/etherwake")
38 iface:value("", translate("Broadcast on all interfaces"))
40 for _, e in ipairs(sys.net.devices()) do
41 if e ~= "lo" then iface:value(e) end
46 host = s:option(Value, "mac", translate("Host to wake up"),
47 translate("Choose the host to wake up or enter a custom MAC address to use"))
49 sys.net.mac_hints(function(mac, name)
50 host:value(mac, "%s (%s)" %{ mac, name })
54 broadcast = s:option(Flag, "broadcast",
55 translate("Send to broadcast address"))
57 broadcast:depends("binary", "/usr/bin/etherwake")
61 function host.write(self, s, val)
62 local host = luci.http.formvalue("cbid.wol.1.mac")
63 local mac = ipc.checkmac(host)
66 local util = luci.http.formvalue("cbid.wol.1.binary") or (
67 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
70 if util == "/usr/bin/etherwake" then
71 local iface = luci.http.formvalue("cbid.wol.1.iface")
72 local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
73 cmd = "%s -D%s %s %q 2>&1" %{
74 util, (iface ~= "" and " -i %s" % utl.shellquote(iface) or ""),
75 (broadcast == "1" and " -b" or ""), mac
78 cmd = "%s -v %q" %{ util, mac }
81 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
82 translate("Starting WoL utility:"), utl.pcdata(cmd)
85 local p = io.popen(cmd .. " 2>&1")
88 local l = p:read("*l")
90 if #l > 100 then l = l:sub(1, 100) .. "..." end
91 msg = msg .. l .. "<br />"
99 msg = msg .. "</code></p>"