source: luci/trunk/applications/luci-wol/luasrc/model/cbi/wol.lua

Last change on this file was 10467, checked in by soma, 3 years ago

luci/po: spelling and grammar fixes by Alex Henrie

File size: 2.5 KB
Line 
1--[[
2LuCI - Lua Configuration Interface
3
4Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11]]--
12
13local sys = require "luci.sys"
14local fs  = require "nixio.fs"
15
16m = SimpleForm("wol", translate("Wake on LAN"),
17    translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
18
19m.submit = translate("Wake up host")
20m.reset  = false
21
22
23local has_ewk = fs.access("/usr/bin/etherwake")
24local has_wol = fs.access("/usr/bin/wol")
25
26
27s = m:section(SimpleSection)
28
29if has_ewk and has_wol then
30    bin = s:option(ListValue, "binary", translate("WoL program"),
31        translate("Sometimes only one of the two tools works. If one fails, try the other one"))
32
33    bin:value("/usr/bin/etherwake", "Etherwake")
34    bin:value("/usr/bin/wol", "WoL")
35end
36
37if has_ewk then
38    iface = s:option(ListValue, "iface", translate("Network interface to use"),
39        translate("Specifies the interface the WoL packet is sent on"))
40
41    if has_wol then
42        iface:depends("binary", "/usr/bin/etherwake")
43    end
44
45    iface:value("", translate("Broadcast on all interfaces"))
46
47    for _, e in ipairs(sys.net.devices()) do
48        if e ~= "lo" then iface:value(e) end
49    end
50end
51
52
53host = s:option(Value, "mac", translate("Host to wake up"),
54    translate("Choose the host to wake up or enter a custom MAC address to use"))
55
56sys.net.mac_hints(function(mac, name)
57    host:value(mac, "%s (%s)" %{ mac, name })
58end)
59
60
61function host.write(self, s, val)
62    local host = luci.http.formvalue("cbid.wol.1.mac")
63    if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
64        local cmd
65        local util = luci.http.formvalue("cbid.wol.1.binary") or (
66            has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
67        )
68
69        if util == "/usr/bin/etherwake" then
70            local iface = luci.http.formvalue("cbid.wol.1.iface")
71            cmd = "%s -D%s %q" %{
72                util, (iface ~= "" and " -i %q" % iface or ""), host
73            }
74        else
75            cmd = "%s -v %q" %{ util, host }
76        end
77
78        local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
79            translate("Starting WoL utility:"), cmd
80        }
81
82        local p = io.popen(cmd .. " 2>&1")
83        if p then
84            while true do
85                local l = p:read("*l")
86                if l then
87                    if #l > 100 then l = l:sub(1, 100) .. "..." end
88                    msg = msg .. l .. "<br />"
89                else
90                    break
91                end
92            end
93            p:close()
94        end
95
96        msg = msg .. "</code></p>"
97
98        m.message = msg
99    end
100end
101
102
103return m
Note: See TracBrowser for help on using the repository browser.