2010-04-18 22:27:30 +00:00
|
|
|
--[[
|
|
|
|
LuCI - Lua Configuration Interface
|
|
|
|
|
2010-04-18 22:35:30 +00:00
|
|
|
Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
|
2010-04-18 22:27:30 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
]]--
|
|
|
|
|
2010-11-24 04:19:23 +00:00
|
|
|
local uci = require "luci.model.uci".cursor()
|
|
|
|
local utl = require "luci.util"
|
2010-04-18 22:27:30 +00:00
|
|
|
local sys = require "luci.sys"
|
|
|
|
local fs = require "nixio.fs"
|
|
|
|
|
|
|
|
m = SimpleForm("wol", translate("Wake on LAN"),
|
|
|
|
translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
|
|
|
|
|
|
|
|
m.submit = translate("Wake up host")
|
|
|
|
m.reset = false
|
|
|
|
|
|
|
|
|
|
|
|
local has_ewk = fs.access("/usr/bin/etherwake")
|
|
|
|
local has_wol = fs.access("/usr/bin/wol")
|
|
|
|
|
|
|
|
|
|
|
|
s = m:section(SimpleSection)
|
|
|
|
|
|
|
|
local arp = { }
|
|
|
|
local e, ip, mac, name
|
|
|
|
|
|
|
|
if has_ewk and has_wol then
|
|
|
|
bin = s:option(ListValue, "binary", translate("WoL program"),
|
|
|
|
translate("Sometimes only one of both tools work. If one of fails, try the other one"))
|
|
|
|
|
|
|
|
bin:value("/usr/bin/etherwake", "Etherwake")
|
|
|
|
bin:value("/usr/bin/wol", "WoL")
|
|
|
|
end
|
|
|
|
|
|
|
|
if has_ewk then
|
|
|
|
iface = s:option(ListValue, "iface", translate("Network interface to use"),
|
|
|
|
translate("Specifies the interface the WoL packet is sent on"))
|
|
|
|
|
|
|
|
if has_wol then
|
|
|
|
iface:depends("binary", "/usr/bin/etherwake")
|
|
|
|
end
|
|
|
|
|
|
|
|
iface:value("", translate("Broadcast on all interfaces"))
|
2010-11-24 05:05:39 +00:00
|
|
|
|
2010-04-18 22:27:30 +00:00
|
|
|
for _, e in ipairs(sys.net.devices()) do
|
|
|
|
if e ~= "lo" then iface:value(e) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
for _, e in ipairs(sys.net.arptable()) do
|
2010-11-24 04:19:23 +00:00
|
|
|
arp[e["HW address"]:upper()] = { e["IP address"] }
|
2010-04-18 22:27:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for e in io.lines("/etc/ethers") do
|
|
|
|
mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
|
2010-11-24 04:19:23 +00:00
|
|
|
if mac and ip then arp[mac:upper()] = { ip } end
|
2010-04-18 22:27:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for e in io.lines("/var/dhcp.leases") do
|
|
|
|
mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
|
2010-11-24 04:19:23 +00:00
|
|
|
if mac and ip then arp[mac:upper()] = { ip, name ~= "*" and name } end
|
2010-04-18 22:27:30 +00:00
|
|
|
end
|
|
|
|
|
2010-11-24 04:19:23 +00:00
|
|
|
uci:foreach("dhcp", "host",
|
|
|
|
function(s)
|
|
|
|
if s.mac and s.ip then
|
2011-07-21 15:26:55 +00:00
|
|
|
if type(s.mac) == "table" then
|
|
|
|
local m
|
|
|
|
for _, m in ipairs(s.mac) do
|
|
|
|
arp[m:upper()] = { s.ip, s.name }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
arp[s.mac:upper()] = { s.ip, s.name }
|
|
|
|
end
|
2010-11-24 04:19:23 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2010-04-18 22:27:30 +00:00
|
|
|
host = s:option(Value, "mac", translate("Host to wake up"),
|
|
|
|
translate("Choose the host to wake up or enter a custom MAC address to use"))
|
|
|
|
|
2010-11-24 04:19:23 +00:00
|
|
|
for mac, ip in utl.kspairs(arp) do
|
2010-04-18 22:27:30 +00:00
|
|
|
host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-04-21 21:25:58 +00:00
|
|
|
function host.write(self, s, val)
|
|
|
|
local host = luci.http.formvalue("cbid.wol.1.mac")
|
2010-11-24 05:05:39 +00:00
|
|
|
if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
|
2010-04-21 21:25:58 +00:00
|
|
|
local cmd
|
|
|
|
local util = luci.http.formvalue("cbid.wol.1.binary") or (
|
|
|
|
has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
|
|
|
|
)
|
|
|
|
|
|
|
|
if util == "/usr/bin/etherwake" then
|
|
|
|
local iface = luci.http.formvalue("cbid.wol.1.iface")
|
|
|
|
cmd = "%s -D%s %q" %{
|
|
|
|
util, (iface ~= "" and " -i %q" % iface or ""), host
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cmd = "%s -v %q" %{ util, host }
|
|
|
|
end
|
|
|
|
|
|
|
|
local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
|
|
|
|
translate("Starting WoL utility:"), cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
local p = io.popen(cmd .. " 2>&1")
|
|
|
|
if p then
|
|
|
|
while true do
|
|
|
|
local l = p:read("*l")
|
|
|
|
if l then
|
|
|
|
if #l > 100 then l = l:sub(1, 100) .. "..." end
|
|
|
|
msg = msg .. l .. "<br />"
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
p:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
msg = msg .. "</code></p>"
|
|
|
|
|
|
|
|
m.message = msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-04-18 22:27:30 +00:00
|
|
|
return m
|