textfsm-rust 0.3.1

Template-based state machine for parsing semi-formatted text based on Google's TextFSM
Documentation
//! Example: Parsing network device output
//!
//! This example shows a realistic use case: parsing Cisco "show interfaces" output.
//!
//! Run with: cargo run --example network_parsing

use textfsm_rust::{validate_template, Template};

// Validate the template at compile time
validate_template!("templates/cisco_show_interfaces.textfsm");

fn main() {
    // Parse the template at runtime
    let template = Template::parse_str(include_str!("../templates/cisco_show_interfaces.textfsm"))
        .expect("validated at compile time");

    // Simulated "show interfaces" output from a Cisco router
    let cli_output = r#"
GigabitEthernet0/0 is up, line protocol is up
  Hardware is iGbE, address is 0050.56ab.1234 (bia 0050.56ab.1234)
  Internet address is 192.168.1.1/24
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,
     reliability 255/255, txload 1/255, rxload 1/255
GigabitEthernet0/1 is administratively down, line protocol is down
  Hardware is iGbE, address is 0050.56ab.5678 (bia 0050.56ab.5678)
  Internet address is 10.0.0.1/8
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,
     reliability 255/255, txload 1/255, rxload 1/255
Serial0/0/0 is up, line protocol is up
  Hardware is GT96K Serial
  Internet address is 172.16.0.1/30
  MTU 1500 bytes, BW 1544 Kbit/sec, DLY 20000 usec,
     reliability 255/255, txload 1/255, rxload 1/255
"#;

    // Parse the output
    let mut parser = template.parser();
    let results = parser.parse_text(cli_output).unwrap();

    // Display results as a table
    let header = template.header();
    println!("{:<25} {:<25} {:<10} {:<18} {:<6}",
        header[0], header[1], header[2], header[3], header[4]);
    println!("{}", "-".repeat(84));

    for record in &results {
        println!("{:<25} {:<25} {:<10} {:<18} {:<6}",
            record[0].as_string(),
            record[1].as_string(),
            record[2].as_string(),
            record[3].as_string(),
            record[4].as_string(),
        );
    }

    println!();
    println!("Total interfaces parsed: {}", results.len());
}