wol.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local sys = require "luci.sys"
  4. local fs = require "nixio.fs"
  5. m = SimpleForm("wol", translate("Wake on LAN"),
  6. translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
  7. m.submit = translate("Wake up host")
  8. m.reset = false
  9. local has_ewk = fs.access("/usr/bin/etherwake")
  10. local has_wol = fs.access("/usr/bin/wol")
  11. s = m:section(SimpleSection)
  12. if has_ewk and has_wol then
  13. bin = s:option(ListValue, "binary", translate("WoL program"),
  14. translate("Sometimes only one of the two tools works. If one fails, try the other one"))
  15. bin:value("/usr/bin/etherwake", "Etherwake")
  16. bin:value("/usr/bin/wol", "WoL")
  17. end
  18. if has_ewk then
  19. iface = s:option(ListValue, "iface", translate("Network interface to use"),
  20. translate("Specifies the interface the WoL packet is sent on"))
  21. if has_wol then
  22. iface:depends("binary", "/usr/bin/etherwake")
  23. end
  24. iface:value("", translate("Broadcast on all interfaces"))
  25. for _, e in ipairs(sys.net.devices()) do
  26. if e ~= "lo" then iface:value(e) end
  27. end
  28. end
  29. host = s:option(Value, "mac", translate("Host to wake up"),
  30. translate("Choose the host to wake up or enter a custom MAC address to use"))
  31. sys.net.mac_hints(function(mac, name)
  32. host:value(mac, "%s (%s)" %{ mac, name })
  33. end)
  34. function host.write(self, s, val)
  35. local host = luci.http.formvalue("cbid.wol.1.mac")
  36. if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
  37. local cmd
  38. local util = luci.http.formvalue("cbid.wol.1.binary") or (
  39. has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
  40. )
  41. if util == "/usr/bin/etherwake" then
  42. local iface = luci.http.formvalue("cbid.wol.1.iface")
  43. cmd = "%s -D%s %q" %{
  44. util, (iface ~= "" and " -i %q" % iface or ""), host
  45. }
  46. else
  47. cmd = "%s -v %q" %{ util, host }
  48. end
  49. local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
  50. translate("Starting WoL utility:"), cmd
  51. }
  52. local p = io.popen(cmd .. " 2>&1")
  53. if p then
  54. while true do
  55. local l = p:read("*l")
  56. if l then
  57. if #l > 100 then l = l:sub(1, 100) .. "..." end
  58. msg = msg .. l .. "<br />"
  59. else
  60. break
  61. end
  62. end
  63. p:close()
  64. end
  65. msg = msg .. "</code></p>"
  66. m.message = msg
  67. end
  68. end
  69. return m