applications: add WoL application
[project/luci.git] / applications / luci-wol / luasrc / model / cbi / wol.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.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 $Id: olsrd.lua 5560 2009-11-21 00:22:35Z jow $
13 ]]--
14
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 if luci.http.formvalue("cbi.submit") then
29         local host = luci.http.formvalue("cbid.wol.1.mac")
30         if host and #host > 0 then
31                 local cmd
32                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
33                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
34                 )
35
36                 if util == "/usr/bin/etherwake" then
37                         local iface = luci.http.formvalue("cbid.wol.1.iface")
38                         cmd = "%s -D%s %q" %{
39                                 util, (iface ~= "" and " -i %q" % iface or ""), host
40                         }
41                 else
42                         cmd = "%s -v %q" %{ util, host }
43                 end
44
45                 is = m:section(SimpleSection)
46
47                 function is.render()
48                         luci.http.write(
49                                 "<p><br /><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
50                                         translate("Starting WoL utility:"), cmd
51                                 }
52                         )
53
54                         local p = io.popen(cmd .. " 2>&1")
55                         if p then
56                                 while true do
57                                         local l = p:read("*l")
58                                         if l then
59                                                 if #l > 100 then l = l:sub(1, 100) .. "..." end
60                                                 luci.http.write(l .. "<br />")
61                                         else
62                                                 break
63                                         end
64                                 end
65                                 p:close()
66                         end
67
68                         luci.http.write("</code><br /></p>")
69                 end
70         end
71 end
72
73
74 s = m:section(SimpleSection)
75
76 local arp = { }
77 local e, ip, mac, name
78
79 if has_ewk and has_wol then
80         bin = s:option(ListValue, "binary", translate("WoL program"),
81                 translate("Sometimes only one of both tools work. If one of fails, try the other one"))
82
83         bin:value("/usr/bin/etherwake", "Etherwake")
84         bin:value("/usr/bin/wol", "WoL")
85 end
86
87 if has_ewk then
88         iface = s:option(ListValue, "iface", translate("Network interface to use"),
89                 translate("Specifies the interface the WoL packet is sent on"))
90
91         if has_wol then
92                 iface:depends("binary", "/usr/bin/etherwake")
93         end
94
95         iface:value("", translate("Broadcast on all interfaces"))
96         
97         for _, e in ipairs(sys.net.devices()) do
98                 if e ~= "lo" then iface:value(e) end
99         end
100 end
101
102
103 for _, e in ipairs(sys.net.arptable()) do
104         arp[e["HW address"]] = { e["IP address"] }
105 end
106
107 for e in io.lines("/etc/ethers") do
108         mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
109         if mac and ip then arp[mac] = { ip } end
110 end
111
112 for e in io.lines("/var/dhcp.leases") do
113         mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
114         if mac and ip then arp[mac] = { ip, name ~= "*" and name } end
115 end
116
117 host = s:option(Value, "mac", translate("Host to wake up"),
118         translate("Choose the host to wake up or enter a custom MAC address to use"))
119
120 for mac, ip in pairs(arp) do
121         host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
122 end
123
124
125 return m
126