You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

  1. -- ------ hotplug script configuration ------ --
  2. fs = require "nixio.fs"
  3. sys = require "luci.sys"
  4. ut = require "luci.util"
  5. script = "/etc/hotplug.d/iface/16-mwancustom"
  6. scriptBackup = "/etc/hotplug.d/iface/16-mwancustombak"
  7. if luci.http.formvalue("cbid.luci.1._restorebak") then -- restore button has been clicked
  8. luci.http.redirect(luci.dispatcher.build_url("admin/network/mwan/advanced/hotplugscript") .. "?restore=yes")
  9. elseif luci.http.formvalue("restore") == "yes" then -- restore script from backup
  10. os.execute("cp -f " .. scriptBackup .. " " .. script)
  11. end
  12. m5 = SimpleForm("luci", nil)
  13. m5:append(Template("mwan/advanced_hotplugscript")) -- highlight current tab
  14. f = m5:section(SimpleSection, nil,
  15. translate("This section allows you to modify the contents of /etc/hotplug.d/iface/16-mwancustom<br />" ..
  16. "This is useful for running system commands and/or scripts based on interface ifup or ifdown hotplug events<br /><br />" ..
  17. "Notes:<br />" ..
  18. "The first line of the script must be &#34;#!/bin/sh&#34; without quotes<br />" ..
  19. "Lines beginning with # are comments and are not executed<br /><br />" ..
  20. "Available variables:<br />" ..
  21. "$ACTION is the hotplug event (ifup, ifdown)<br />" ..
  22. "$INTERFACE is the interface name (wan1, wan2, etc.)<br />" ..
  23. "$DEVICE is the device name attached to the interface (eth0.1, eth1, etc.)"))
  24. restore = f:option(Button, "_restorebak", translate("Restore default hotplug script"))
  25. restore.inputtitle = translate("Restore...")
  26. restore.inputstyle = "apply"
  27. t = f:option(TextValue, "lines")
  28. t.rmempty = true
  29. t.rows = 20
  30. function t.cfgvalue()
  31. local hps = fs.readfile(script)
  32. if not hps or hps == "" then -- if script does not exist or is blank restore from backup
  33. sys.call("cp -f " .. scriptBackup .. " " .. script)
  34. return fs.readfile(script)
  35. else
  36. return hps
  37. end
  38. end
  39. function t.write(self, section, data) -- format and write new data to script
  40. return fs.writefile(script, ut.trim(data:gsub("\r\n", "\n")) .. "\n")
  41. end
  42. return m5