applications/luci-wol: handle command execution in write hook and use message facilit...
[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 sys = require "luci.sys"
14 local fs  = require "nixio.fs"
15
16 m = SimpleForm("wol", translate("Wake on LAN"),
17         translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
18
19 m.submit = translate("Wake up host")
20 m.reset  = false
21
22
23 local has_ewk = fs.access("/usr/bin/etherwake")
24 local has_wol = fs.access("/usr/bin/wol")
25
26
27 s = m:section(SimpleSection)
28
29 local arp = { }
30 local e, ip, mac, name
31
32 if has_ewk and has_wol then
33         bin = s:option(ListValue, "binary", translate("WoL program"),
34                 translate("Sometimes only one of both tools work. If one of fails, try the other one"))
35
36         bin:value("/usr/bin/etherwake", "Etherwake")
37         bin:value("/usr/bin/wol", "WoL")
38 end
39
40 if has_ewk then
41         iface = s:option(ListValue, "iface", translate("Network interface to use"),
42                 translate("Specifies the interface the WoL packet is sent on"))
43
44         if has_wol then
45                 iface:depends("binary", "/usr/bin/etherwake")
46         end
47
48         iface:value("", translate("Broadcast on all interfaces"))
49         
50         for _, e in ipairs(sys.net.devices()) do
51                 if e ~= "lo" then iface:value(e) end
52         end
53 end
54
55
56 for _, e in ipairs(sys.net.arptable()) do
57         arp[e["HW address"]] = { e["IP address"] }
58 end
59
60 for e in io.lines("/etc/ethers") do
61         mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
62         if mac and ip then arp[mac] = { ip } end
63 end
64
65 for e in io.lines("/var/dhcp.leases") do
66         mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
67         if mac and ip then arp[mac] = { ip, name ~= "*" and name } end
68 end
69
70 host = s:option(Value, "mac", translate("Host to wake up"),
71         translate("Choose the host to wake up or enter a custom MAC address to use"))
72
73 for mac, ip in pairs(arp) do
74         host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
75 end
76
77
78 function host.write(self, s, val)
79         local host = luci.http.formvalue("cbid.wol.1.mac")
80         if host and #host > 0 then
81                 local cmd
82                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
83                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
84                 )
85
86                 if util == "/usr/bin/etherwake" then
87                         local iface = luci.http.formvalue("cbid.wol.1.iface")
88                         cmd = "%s -D%s %q" %{
89                                 util, (iface ~= "" and " -i %q" % iface or ""), host
90                         }
91                 else
92                         cmd = "%s -v %q" %{ util, host }
93                 end
94
95                 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
96                         translate("Starting WoL utility:"), cmd
97                 }
98
99                 local p = io.popen(cmd .. " 2>&1")
100                 if p then
101                         while true do
102                                 local l = p:read("*l")
103                                 if l then
104                                         if #l > 100 then l = l:sub(1, 100) .. "..." end
105                                         msg = msg .. l .. "<br />"
106                                 else
107                                         break
108                                 end
109                         end
110                         p:close()
111                 end
112
113                 msg = msg .. "</code></p>"
114
115                 m.message = msg
116         end
117 end
118
119
120 return m
121