summaryrefslogtreecommitdiffstats
path: root/kud/tests/sdwan/build/commands.lua
blob: d99f4579faa843b0f99ce33445502e358daefb5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- Licensed to the public under the GNU General Public License v2.

module("luci.controller.commands", package.seeall)

sys = require "luci.sys"
ut = require "luci.util"
io = require "io"

ip = "ip -4 "

function index()
    entry({"admin", "config", "command"},
	call("execute")).dependent = false
end

function trim(s)
    return s:match("^%s*(.-)%s*$")
end

function split_and_trim(str, sep)
    local array = {}
    local reg = string.format("([^%s]+)", sep)
    for item in string.gmatch(str, reg) do
        item_trimed = trim(item)
        if string.len(item_trimed) > 0 then
            table.insert(array, item_trimed)
        end
    end
    return array
end

function execute()
    local commands = luci.http.formvalue("command")
    io.stderr:write("Execute command: %s\n" % commands)

    local command_array = split_and_trim(commands, ";")
    for index, command in ipairs(command_array) do
        sys.exec(command)
    end

    luci.http.prepare_content("application/json")
    luci.http.write_json("{'status':'ok'}")
end