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