--[[ /* * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ ]] -- Experimental prototype CLI using API to VPP, with tab completion -- -- Written by Andrew Yourtchenko (ayourtch@cisco.com) 2010,2016 -- vpp = require "vpp-lapi" local dotdotdot = "..." -- First the "readline" routine readln = { split = function(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = "(.-)" .. pat local last_end = 1 if str then local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end end return t end, reader = function() local rl = {} rl.init = function() os.execute("stty -icanon min 1 -echo") rl.rawmode = true end rl.done = function() os.execute("stty icanon echo") rl.rawmode = false end rl.prompt = ">" rl.history = { "" } rl.history_index = 1 rl.history_length = 1 rl.hide_cmd = function() local bs = string.char(8) .. " " .. string.char(8) for i = 1, #rl.command do io.stdout:write(bs) end end rl.show_cmd = function() if rl.command then io.stdout:write(rl.command) end end rl.store_history = function(cmd) if cmd == "" then return end rl.history[rl.history_length] = cmd rl.history_length = rl.history_length + 1 rl.history_index = rl.history_length rl.history[rl.history_length] = "" end rl.readln = function() local done = false local need_prompt = true rl.command = "" if not rl.rawmode then rl.init() end while not done do if need_prompt then io.stdout:write(rl.prompt) io.stdout:write(rl.command) need_prompt = false end local ch = io.stdin:read(1) if ch:byte(1) == 27 then -- CONTROL local ch2 = io.stdin:read(1) -- arrows if ch2:byte(1) == 91 then local ch3 = io.stdin:read(1) local b = ch3:byte(1) if b == 65 then ch = "UP" elseif b == 66 then ch = "DOWN" elseif b == 67 then ch = "RIGHT" elseif b == 68 then ch = "LEFT" end -- print("Byte: " .. ch3:byte(1)) -- if ch3:byte(1) end end if ch == "?" then io.stdout:write(ch) io.stdout:write("\n") if rl.help then rl.help(rl) end need_prompt = true elseif ch == "\t" then if rl.tab_complete then rl.tab_complete(rl) end io.stdout:write("\n") need_prompt = true elseif ch == "\n" then io.stdout:write(ch) done = true elseif ch == "\004" then io.stdout:write("\n") rl.command = nil done = true elseif ch == string.char(127) then if rl.command ~= "" then io.stdout:write(string.char(8) .. " " .. string.char(8)) rl.command = string.sub(rl.command, 1, -2) end elseif #ch > 1 then -- control char if ch == "UP" then rl.hide_cmd() if rl.history_index == #rl.history then rl.history[rl.history_index] = rl.command end if rl.history_index > 1 then rl.history_index = rl.history_index - 1 rl.command = rl.history[rl.history_index] end rl.show_cmd() elseif ch == "DOWN" then rl.hide_cmd() if rl.history_index < rl.history_length then rl.history_index = rl.history_index + 1 rl.command = rl.history[rl.history_index] end rl.show_cmd() end else io.stdout:write(ch) rl.command = rl.command .. ch end end if rl.command then rl.store_history(rl.command) end return rl.command end return rl end } --[[ r = reader() local done = false while not done do local cmd = r.readln() print("Command: " .. tostring(cmd)) if not cmd or cmd == "quit" then done = true end end r.done() ]] --------- MDS show tech parser local print_section = nil local list_sections = false local curr_section = "---" local curr_parser = nil -- by default operate in batch mode local batch_mode = true local db = {} local device = {} device.output = {} local seen_section = {} function start_collection(name) device = {} seen_section = {} end function print_error(errmsg) print("@#$:" .. errmsg) end function keys(tbl) local t = {} for k, v in pairs(tbl) do table.insert(t, k) end return t end function tset (parent, ...) -- print ('set', ...) local len = select ('#', ...) local key, value = select (len-1, ...)
from __future__ import print_function
import unittest
import vpp_papi
import pot, snat
print('Plugins:')
vpp_papi.plugin_show()
r = vpp_papi.connect('ole')

r = vpp_papi.show_version()
print('R:', r)

r = snat.snat_interface_add_del_feature(1, 1, 1)
print('R:', r)

list_name = 'foobar'
r = pot.pot_profile_add(0, 1, 123, 123, 0, 12, 0, 23, len(list_name), list_name)
print('R:', r)
vpp_papi.disconnect()
print("ARBITRARY CLI" .. vpp.dump(rl.choices)) print("LUACLI command: " .. table.concat(rl.choices, " ")) local sub = {} -- for i=4, #rl.choices -1 do table.insert(sub, rl.choices[i]) end local cli = table.concat(sub, " ") print("Running CLI: " .. tostring(cli)) paged_write(run_cli(vpp, cli)) end device.output["vpp debug cli " .. m .. " " .. dotdotdot] = function(rl) print("ARGH") end local ret = run_cli(vpp, "help " .. m) device.output["help vpp debug cli " .. m] = { ret } end end for linenum, line in ipairs(vpp_clis) do -- print(line, ret) end for msgnum, msgname in pairs(vpp.msg_number_to_name) do local cli, numspaces = string.gsub(msgname, "_", " ") device.output["call " .. cli .. " " .. dotdotdot] = function(rl) print("ARGH") end device.output["call " .. cli] = function(rl) print("LUACLI command: " .. table.concat(rl.choices, " ")) print("Running API: " .. msgname) -- vpp.dump(rl.choices)) local out = {} local args = {} local ntaken = 0 local argname = "" for i=(1+1+numspaces+1), #rl.choices-1 do -- print(i, rl.choices[i]) if ntaken > 0 then ntaken = ntaken -1 else local fieldname = rl.choices[i] local field = vpp.msg_name_to_fields[msgname][fieldname] if field then local s = rl.choices[i+1] s=s:gsub("\\x(%x%x)",function (x) return string.char(tonumber(x,16)) end) args[fieldname] = s ntaken = 1 end end end -- print("ARGS: ", vpp.dump(args)) local ret = vpp:api_call(msgname, args) for i, reply in ipairs(ret) do table.insert(out, "=================== Entry #" .. tostring(i)) for k, v in pairs(reply) do table.insert(out, " " .. tostring(k) .. " : " .. toprintablestring(v)) end end -- paged_write(vpp.dump(ret) .. "\n\n") paged_write(table.concat(out, "\n").."\n\n") end device.output["call " .. cli .. " help"] = function(rl) local out = {} for k, v in pairs(vpp.msg_name_to_fields[msgname]) do table.insert(out, tostring(k) .. " : " .. v["ctype"] .. " ; " .. tostring(vpp.dump(v)) ) end -- paged_write(vpp.dump(vpp.msg_name_to_fields[msgname]) .. "\n\n") paged_write(table.concat(out, "\n").."\n\n") end -- vpp.msg_name_to_number = {} end local r = readln.reader() local done = false r.prompt = "VPP(luaCLI)#" r.help = device_cli_help r.tab_complete = device_cli_tab_complete print("===== CLI view, use ^D to end =====") r.tree = populate_tree(device.output) -- readln.pretty("xxxx", r.tree) for idx, an_arg in ipairs(arg) do local fname = an_arg if fname == "-i" then pager_lines = 23 interactive_cli(r) else pager_lines = 100000000 for line in io.lines(fname) do r.command = line local func = device_cli_exec(r) if func then func(r) end end end end if #arg == 0 then print("You should specify '-i' as an argument for the interactive session,") print("but with no other sources of commands, we start interactive session now anyway") interactive_cli(r) end vpp:disconnect() r.done()