diag.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Copyright 2013 Manuel Munz <freifunk@somakoma.de>
  4. -- Licensed to the public under the Apache License 2.0.
  5. module("luci.controller.freifunk.diag", package.seeall)
  6. function index()
  7. local uci = require("luci.model.uci").cursor()
  8. local page
  9. page = node("freifunk", "status", "diagnostics")
  10. page.target = template("freifunk/diagnostics")
  11. page.title = _("Diagnostics")
  12. page.order = 60
  13. page = entry({"freifunk", "status", "diag_ping"}, call("diag_ping"), nil)
  14. page.leaf = true
  15. page = entry({"freifunk", "status", "diag_nslookup"}, call("diag_nslookup"), nil)
  16. page.leaf = true
  17. page = entry({"freifunk", "status", "diag_traceroute"}, call("diag_traceroute"), nil)
  18. page.leaf = true
  19. page = entry({"freifunk", "status", "diag_ping6"}, call("diag_ping6"), nil)
  20. page.leaf = true
  21. page = entry({"freifunk", "status", "diag_traceroute6"}, call("diag_traceroute6"), nil)
  22. page.leaf = true
  23. end
  24. function diag_command(cmd, addr)
  25. if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
  26. luci.http.prepare_content("text/plain")
  27. local util = io.popen(cmd % luci.util.shellquote(addr))
  28. if util then
  29. while true do
  30. local ln = util:read("*l")
  31. if not ln then break end
  32. luci.http.write(ln)
  33. luci.http.write("\n")
  34. end
  35. util:close()
  36. end
  37. return
  38. end
  39. luci.http.status(500, "Bad address")
  40. end
  41. function diag_ping(addr)
  42. diag_command("ping -c 5 -W 1 %s 2>&1", addr)
  43. end
  44. function diag_traceroute(addr)
  45. diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
  46. end
  47. function diag_nslookup(addr)
  48. diag_command("nslookup %s 2>&1", addr)
  49. end
  50. function diag_ping6(addr)
  51. diag_command("ping6 -c 5 %s 2>&1", addr)
  52. end
  53. function diag_traceroute6(addr)
  54. diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)
  55. end