LUA Cisco parse interfaces + CSV output
This script is designed to retrieve and parse network interface information from a cisco device, and then optionally print this information in either a human-readable format or as a CSV file. Here's a breakdown of its objectives: Retrieve Interface Information: The script sends commands to a network device to get the interface details (sendget('show interfaces','#')). It captures the output of these commands (local output = before()). Parse the Output: Theparse_interfacesfunction processes the output to extract relevant details about each interface, such as the interface name, description, IP address, MTU, encapsulation type, and packet statistics. Format the Information: The parsed information is stored in a table (interfaces). Print the Information: Ifenable_csv_printis set to0, the script prints the interface details in a human-readable format. Ifenable_csv_printis set to1, the script converts the interface details into CSV format using theinterfaces_to_csvfunction and prints the CSV string. The enable_csv_print variable controls whether the output is printed in a human-readable format (0) or as a CSV file (1). This allows for flexible output depending on the user's needs. -- Enable CSV output? enable_csv_print = 0 sendget('', '#') sendget('show interfaces','#') waitprompt() local output = before() -- print(output) local function parse_interfaces(output) local interfaces = {} local current_interface = {} for line in output:gmatch("[^\r\n]+") do if line:match("^GigabitEthernet") or line:match("^TenGigabitEthernet") then if next(current_interface) then table.insert(interfaces, current_interface) end current_interface = { interface = line } elseif line:match("^%s+Description:") then current_interface.description = line:match("Description:%s+(.*)") elseif line:match("^%s+Internet address is") then current_interface.ip_address = line:match("Internet address is%s+(%S+)") elseif line:match("^%s+MTU") then current_interface.mtu = line:match("MTU (%d+)") elseif line:match("^%s+Encapsulation") then current_interface.encapsulation = line:match("Encapsulation (%S+)") elseif line:match("^%s+%d+ packets input") then current_interface.packets_input = line:match("(%d+) packets input") elseif line:match("^%s+%d+ packets output") then current_interface.packets_output = line:match("(%d+) packets output") end end return interfaces end local function interfaces_to_csv(interfaces) local csv = "Interface,Description,IP Address,MTU,Encapsulation,Packets Input,Packets Output\n" for _, intf in ipairs(interfaces) do csv = csv .. string.format("%s,%s,%s,%s,%s,%s,%s\n", intf.interface, intf.description or "N/A", intf.ip_address or "N/A", intf.mtu or "N/A", intf.encapsulation or "N/A", intf.packets_input or "N/A", intf.packets_output or "N/A" ) end return csv end local interfaces = parse_interfaces(output) if enable_csv_print == 0 then for _, intf in ipairs(interfaces) do print(string.format("Interface: %s, Description: %s, IP Address: %s, MTU: %s, Encapsulation: %s, Packets Input: %s, Packets Output: %s", intf.interface, intf.description or "N/A", intf.ip_address or "N/A", intf.mtu or "N/A", intf.encapsulation or "N/A", intf.packets_input or "N/A", intf.packets_output or "N/A")) end end local csv = interfaces_to_csv(interfaces) if enable_csv_print == 1 then print(csv) end36Views1like1CommentLua Script from the Restorepoint Automation PowerHour
This is an annotated version of the Lua device control script that was shown in last week's PowerHour about Restorepoint automation. The objective was to connect to a Cisco IOS device, identify any GigabitEthernet interfaces whose current MTU was >1500, and then configure those interfaces to use an MTU of 1500. We can do this by running the "show interfaces" command and processing the output. Here is the script I described during the PowerHour: timeout (10) -- Run the "show interfaces" command, collect the output, and store that output -- as a list of text lines. sendget("show interfaces","#") output = before() lines=splitlines(output) -- Start looping through the output lines. for i,line in pairs(lines) do -- Look for lines like "InterfaceName is up|down, line protocol is up|down". if line:match(".* is [a-z]+, line protocol is [a-z]+") then -- When we find one, save the interface name as intf. intf=line:match("^(.*) is [a-z]+, line protocol is [a-z]+") mtu=nil end -- If we find a line with MTU data, convert the MTU to a numeric value and save it. if line:match("MTU") then mtu = tonumber(line:match("MTU ([0-9]+) bytes")) -- Check if the interface meets our criteria of being a GigabitEthernet interface -- with a current MTU above 1500. if intf:match("GigabitEthernet") and mtu > 1500 then -- If it is, execute the Cisco IOS commands to change the configuration. We use a local -- variable 'in_config_mode' to avoid going in/out of config mode multiple times. if not in_config_mode then sendget("conf terminal","#") in_config_mode = true end sendget("int "..intf,"#") sendget("mtu 1500","#") sendget("exit","#") print("Changed MTU on "..intf.." from "..mtu.." to 1500 bytes") end end end -- Finally, exit from config mode if needed. if in_config_mode then sendget("exit","#") end72Views2likes0Comments