name = "network_info_parser" category = "pipeline" mode = "convert" description = """ Parse network interface configuration from stdin (in "ip addr show" format) and extract a summary of each interface. For each interface block, output a line: IFACE: IP: MASK: / An interface block starts with a line like: 2: eth0: mtu 1500 ... and contains inet lines like: inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0 If an interface has no inet line, output: IFACE: IP: none MASK: none Skip the loopback interface (lo). """ bash_source = ''' #!/bin/bash current_iface="" found_ip="" found_mask="" flush_iface() { if [[ -n "$current_iface" && "$current_iface" != "lo" ]]; then if [[ -n "$found_ip" ]]; then echo "IFACE: $current_iface IP: $found_ip MASK: /$found_mask" else echo "IFACE: $current_iface IP: none MASK: none" fi fi } while IFS= read -r line || [[ -n "$line" ]]; do # Detect interface line: starts with a number followed by colon if echo "$line" | grep -qE '^[0-9]+:'; then flush_iface current_iface=$(echo "$line" | awk -F: '{print $2}' | sed 's/^[[:space:]]*//' | awk '{print $1}') found_ip="" found_mask="" fi # Detect inet line (IPv4 only, not inet6) if echo "$line" | grep -qE '^[[:space:]]+inet [0-9]'; then ip_cidr=$(echo "$line" | awk '{print $2}') found_ip=$(echo "$ip_cidr" | cut -d/ -f1) found_mask=$(echo "$ip_cidr" | cut -d/ -f2) fi done flush_iface ''' [[test_cases]] description = "Two interfaces with IPs" stdin = """1: lo: mtu 65536 inet 127.0.0.1/8 scope host lo 2: eth0: mtu 1500 inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0 3: wlan0: mtu 1500 inet 10.0.0.42/16 brd 10.0.255.255 scope global wlan0""" expected_stdout = """IFACE: eth0 IP: 192.168.1.100 MASK: /24 IFACE: wlan0 IP: 10.0.0.42 MASK: /16""" [[test_cases]] description = "Interface with no IP" stdin = """1: lo: mtu 65536 inet 127.0.0.1/8 scope host lo 2: eth0: mtu 1500 3: docker0: mtu 1500 inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0""" expected_stdout = """IFACE: eth0 IP: none MASK: none IFACE: docker0 IP: 172.17.0.1 MASK: /16""" [[test_cases]] description = "Single interface" stdin = """1: lo: mtu 65536 inet 127.0.0.1/8 scope host lo 2: enp3s0: mtu 9000 inet 10.10.10.5/8 brd 10.255.255.255 scope global enp3s0""" expected_stdout = "IFACE: enp3s0 IP: 10.10.10.5 MASK: /8"