Skip to main content
Solved

LUA Cisco parse interfaces + CSV output

  • November 26, 2024
  • 1 reply
  • 0 views

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:

  1. 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()).
  2. 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.
  3. Format the Information:
    • The parsed information is stored in a table (interfaces).
  4. 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

 

Best answer by yomi25

Here are key benefits of using this script:

  1. Capacity Planning:
    • The analysis of interface data facilitates the identification of network usage trends, enabling proactive capacity planning to accommodate growth and increased traffic without compromising performance.
  2. Troubleshooting:
    • Detailed interface data allows network administrators to swiftly identify and resolve issues such as misconfigurations, bottlenecks, or hardware failures, thereby minimizing downtime.
  3. Optimizing Network Performance:
    • Regular review of interface configurations and performance metrics supports the optimization of network efficiency, load balancing, and overall performance enhancement.
  4. Change Planning:
    • An up-to-date and comprehensive view of the current interface configuration is essential for effective change planning. The CSV data provides a detailed snapshot that aids in assessing the impact of proposed changes and ensuring their smooth implementation.

 

Actually this is a key missing feature in Restorepoint - working with device interfaces. Script integration into Restorepoint significantly enhances the management and maintenance of network infrastructures, at least for Cisco devices. 

A similar script could be developed for other vendors or device types. However, having a standardized method for managing device interfaces within Restorepoint would be significantly more advantageous.

1 reply

  • December 11, 2024

Great script, thanks for posting it!

Are you able to share anything about the use case that led you to write this?  How/why are you using this in Restorepoint?