TextFSM is a template-based state machine designed to parse semi-structured text (such as CLI output) into structured data.
This is an Elixir implementation of the original TextFSM written in Python.
This implementation uses NimbleParsec to parse templates.
Given the template:
Value INTERFACE (\S+)
Value IP_ADDRESS (\d+\.\d+\.\d+\.\d+|unassigned)
Value STATUS (up|down|administratively down)
Value PROTOCOL (up|down)
Start
^${INTERFACE}\s+${IP_ADDRESS}\s+\w+\s+\w+\s+${STATUS}\s+${PROTOCOL} -> Record
and the input text:
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 192.168.1.1 YES NVRAM up up
GigabitEthernet0/1 unassigned YES NVRAM administratively down down
GigabitEthernet0/2 10.0.0.5 YES manual up up
Loopback0 127.0.0.1 YES unset up up
calling TextFSM.parse will return:
%{
"INTERFACE" => ["GigabitEthernet0/0", "GigabitEthernet0/1",
"GigabitEthernet0/2", "Loopback0"],
"IP_ADDRESS" => ["192.168.1.1", "unassigned", "10.0.0.5", "127.0.0.1"],
"PROTOCOL" => ["up", "down", "up", "up"],
"STATUS" => ["up", "administratively down", "up", "up"]
}- Comments in templates are yet to be supported
- The template parser is strict regarding how many whitespaces you use (e.g. when defining values)
def deps do
[
{:textfsm, "~> 0.1.0"}
]
end