applications/luci-wol: cope with host entries that have multiple MACs assigned
[project/luci.git] / applications / luci-wol / luasrc / model / cbi / wol.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11 ]]--
12
13 local uci = require "luci.model.uci".cursor()
14 local utl = require "luci.util"
15 local sys = require "luci.sys"
16 local fs = require "nixio.fs"
17
18 m = SimpleForm("wol", translate("Wake on LAN"),
19 translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
20
21 m.submit = translate("Wake up host")
22 m.reset = false
23
24
25 local has_ewk = fs.access("/usr/bin/etherwake")
26 local has_wol = fs.access("/usr/bin/wol")
27
28
29 s = m:section(SimpleSection)
30
31 local arp = { }
32 local e, ip, mac, name
33
34 if has_ewk and has_wol then
35 bin = s:option(ListValue, "binary", translate("WoL program"),
36 translate("Sometimes only one of both tools work. If one of fails, try the other one"))
37
38 bin:value("/usr/bin/etherwake", "Etherwake")
39 bin:value("/usr/bin/wol", "WoL")
40 end
41
42 if has_ewk then
43 iface = s:option(ListValue, "iface", translate("Network interface to use"),
44 translate("Specifies the interface the WoL packet is sent on"))
45
46 if has_wol then
47 iface:depends("binary", "/usr/bin/etherwake")
48 end
49
50 iface:value("", translate("Broadcast on all interfaces"))
51
52 for _, e in ipairs(sys.net.devices()) do
53 if e ~= "lo" then iface:value(e) end
54 end
55 end
56
57
58 for _, e in ipairs(sys.net.arptable()) do
59 arp[e["HW address"]:upper()] = { e["IP address"] }
60 end
61
62 for e in io.lines("/etc/ethers") do
63 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
64 if mac and ip then arp[mac:upper()] = { ip } end
65 end
66
67 for e in io.lines("/var/dhcp.leases") do
68 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
69 if mac and ip then arp[mac:upper()] = { ip, name ~= "*" and name } end
70 end
71
72 uci:foreach("dhcp", "host",
73 function(s)
74 if s.mac and s.ip then
75 if type(s.mac) == "table" then
76 local m
77 for _, m in ipairs(s.mac) do
78 arp[m:upper()] = { s.ip, s.name }
79 end
80 else
81 arp[s.mac:upper()] = { s.ip, s.name }
82 end
83 end
84 end)
85
86 host = s:option(Value, "mac", translate("Host to wake up"),
87 translate("Choose the host to wake up or enter a custom MAC address to use"))
88
89 for mac, ip in utl.kspairs(arp) do
90 host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
91 end
92
93
94 function host.write(self, s, val)
95 local host = luci.http.formvalue("cbid.wol.1.mac")
96 if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
97 local cmd
98 local util = luci.http.formvalue("cbid.wol.1.binary") or (
99 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
100 )
101
102 if util == "/usr/bin/etherwake" then
103 local iface = luci.http.formvalue("cbid.wol.1.iface")
104 cmd = "%s -D%s %q" %{
105 util, (iface ~= "" and " -i %q" % iface or ""), host
106 }
107 else
108 cmd = "%s -v %q" %{ util, host }
109 end
110
111 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
112 translate("Starting WoL utility:"), cmd
113 }
114
115 local p = io.popen(cmd .. " 2>&1")
116 if p then
117 while true do
118 local l = p:read("*l")
119 if l then
120 if #l > 100 then l = l:sub(1, 100) .. "..." end
121 msg = msg .. l .. "<br />"
122 else
123 break
124 end
125 end
126 p:close()
127 end
128
129 msg = msg .. "</code></p>"
130
131 m.message = msg
132 end
133 end
134
135
136 return m