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:
- The parse_interfaces function 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:
- If enable_csv_print is set to 0, the script prints the interface details in a human-readable format.
- If enable_csv_print is set to 1, the script converts the interface details into CSV format using the interfaces_to_csv function 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)
end
