Skip to content

Commit

Permalink
Fixes parsing of the ip commands on busybox systems. (#1581)
Browse files Browse the repository at this point in the history
* fix: Fixes parsing of the `ip` commands on busybox systems.

Embedded linux systems running busybox don't necessarily include the `iproute2` package, but instead leverage similar functionality from busybox. Unfortunately, the output from busybox `ip` commands appears to be a subset of the `iproute2` ip commands. This commit marks some fields as optional and also allows trailing whitespace (which busybox introduces) in order to parse `ip link show` and `ip address show` commands on busybox systems.

* style: Fixed yaml lint errors
  • Loading branch information
abates authored Dec 26, 2023
1 parent 78d9669 commit 6c722dd
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 12 deletions.
4 changes: 2 additions & 2 deletions ntc_templates/templates/linux_ip_address_show.textfsm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Value Required FLAGS (\S+)
Value Required MTU (\d+)
Value Required QDISC (\S+)
Value Required STATE (\S+)
Value Required GROUP (\S+)
Value GROUP (\S+)
Value QLEN (\d+)
Value MASTER (\S+)
Value Required TYPE (\S+)
Expand All @@ -17,7 +17,7 @@ Value List IPV6_MASKS (\d{1,3})

Start
^\d+: -> Continue.Record
^${ID}:\s+${INTERFACE}:\s+<${FLAGS}>\s+mtu\s+${MTU}\s+qdisc\s+${QDISC}(?:\s+master\s+${MASTER})?\s+state\s+${STATE}\s+group\s+${GROUP}(?:\s+qlen\s+${QLEN})?\s*$$
^${ID}:\s+${INTERFACE}:\s+<${FLAGS}>\s+mtu\s+${MTU}\s+qdisc\s+${QDISC}(?:\s+master\s+${MASTER})?\s+state\s+${STATE}(?:\s+group\s+${GROUP})?(?:\s+qlen\s+${QLEN})?\s*$$
^\s+link/${TYPE}(?:\s+${MAC_ADDRESS}\s+brd\s+${BROADCAST})?.*$$
^\s+altname.*$$
^\s+inet\s+${IP_ADDRESSES}/${IP_MASKS}\s+(:?brd|scope).*$$
Expand Down
18 changes: 8 additions & 10 deletions ntc_templates/templates/linux_ip_link_show.textfsm
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ Value Required FLAGS (\S+)
Value Required MTU (\d+)
Value Required QDISC (\S+)
Value Required STATE (\S+)
Value Required MODE (\S+)
Value Required GROUP (\S+)
Value MODE (\S+)
Value GROUP (\S+)
Value QLEN (\d+)
Value MASTER (\S+)
Value Required TYPE (\S+)
Value MAC_ADDRESS ([a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2})
Value BROADCAST ([a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2})
Value ALIAS (.+)

Start
^\d+ -> Continue.Record
# loopback
^${ID}: ${INTERFACE}: <${FLAGS}> mtu ${MTU} qdisc ${QDISC} state ${STATE} mode ${MODE} group ${GROUP}$$ -> Continue
# physical, bridge
^${ID}: ${INTERFACE}: <${FLAGS}> mtu ${MTU} qdisc ${QDISC} state ${STATE} mode ${MODE} group ${GROUP} qlen ${QLEN}$$ -> Continue
# vrf
^${ID}: ${INTERFACE}: <${FLAGS}> mtu ${MTU} qdisc ${QDISC} master ${MASTER} state ${STATE} mode ${MODE} group ${GROUP} qlen ${QLEN}$$ -> Continue
^${ID}: ${INTERFACE}: <${FLAGS}> mtu ${MTU} qdisc ${QDISC}(?: master ${MASTER})? state ${STATE}(?: mode ${MODE})?(?: group ${GROUP})?(?: qlen ${QLEN})?\s*$$ -> Continue
# Link type and MAC address
^\s+link/${TYPE}$$
^\s+link/${TYPE} ${MAC_ADDRESS} brd ${BROADCAST}$$
^\s+link/${TYPE}\s*$$
^\s+link/${TYPE} ${MAC_ADDRESS} brd ${BROADCAST}\s*$$
^\s+alias\s+${ALIAS}$$
^\d+
^\s+
^. -> Error

13 changes: 13 additions & 0 deletions tests/linux/ip_address_show/busybox_ip_address_show.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 01:02:03:04:05:06 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::302:03ff:fe04:0506/64 scope link
valid_lft forever preferred_lft forever

42 changes: 42 additions & 0 deletions tests/linux/ip_address_show/busybox_ip_address_show.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
parsed_sample:
- broadcast: "00:00:00:00:00:00"
flags: "LOOPBACK,UP,LOWER_UP"
group: ""
id: "1"
interface: "lo"
ip_addresses:
- "127.0.0.1"
ip_masks:
- "8"
ipv6_addresses:
- "::1"
ipv6_masks:
- "128"
mac_address: "00:00:00:00:00:00"
master: ""
mtu: "65536"
qdisc: "noqueue"
qlen: ""
state: "UNKNOWN"
type: "loopback"
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: ""
id: "2"
interface: "eth0"
ip_addresses:
- "192.168.1.1"
ip_masks:
- "24"
ipv6_addresses:
- "fe80::302:03ff:fe04:0506"
ipv6_masks:
- "64"
mac_address: "01:02:03:04:05:06"
master: ""
mtu: "1500"
qdisc: "noqueue"
qlen: ""
state: "UP"
type: "ether"
14 changes: 14 additions & 0 deletions tests/linux/ip_link_show/busybox_ip_link_show.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 01:02:03:04:05:06 brd ff:ff:ff:ff:ff:ff
alias WAN
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 01:02:03:04:05:07 brd ff:ff:ff:ff:ff:ff
alias LAN
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 01:02:03:04:05:08 brd ff:ff:ff:ff:ff:ff
alias WAN2
5: imq0: <NOARP,UP,LOWER_UP> mtu 16000 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 11000
link/void
alias Priority Queue
72 changes: 72 additions & 0 deletions tests/linux/ip_link_show/busybox_ip_link_show.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
parsed_sample:
- broadcast: "00:00:00:00:00:00"
flags: "LOOPBACK,UP,LOWER_UP"
group: ""
id: "1"
interface: "lo"
mac_address: "00:00:00:00:00:00"
master: ""
mode: "DEFAULT"
mtu: "65536"
qdisc: "noqueue"
qlen: ""
state: "UNKNOWN"
type: "loopback"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: ""
id: "2"
interface: "eth0"
mac_address: "01:02:03:04:05:06"
master: ""
mode: "DEFAULT"
mtu: "1500"
qdisc: "noqueue"
qlen: ""
state: "UP"
type: "ether"
alias: "WAN"
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: ""
id: "3"
interface: "eth1"
mac_address: "01:02:03:04:05:07"
master: ""
mode: "DEFAULT"
mtu: "1500"
qdisc: "noqueue"
qlen: ""
state: "UP"
type: "ether"
alias: "LAN"
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: ""
id: "4"
interface: "eth2"
mac_address: "01:02:03:04:05:08"
master: ""
mode: "DEFAULT"
mtu: "1500"
qdisc: "noqueue"
qlen: ""
state: "UP"
type: "ether"
alias: "WAN2"
- broadcast: ""
flags: "NOARP,UP,LOWER_UP"
group: ""
id: "5"
interface: "imq0"
mac_address: ""
master: ""
mode: "DEFAULT"
mtu: "16000"
qdisc: "pfifo_fast"
qlen: "11000"
state: "UNKNOWN"
type: "void"
alias: "Priority Queue"
8 changes: 8 additions & 0 deletions tests/linux/ip_link_show/linux_ip_link_show.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parsed_sample:
qlen: "1000"
state: "UNKNOWN"
type: "loopback"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: "default"
Expand All @@ -26,6 +27,7 @@ parsed_sample:
qlen: "1000"
state: "UP"
type: "ether"
alias: ""
- broadcast: ""
flags: "POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP"
group: "default"
Expand All @@ -39,6 +41,7 @@ parsed_sample:
qlen: "500"
state: "UNKNOWN"
type: "none"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "NO-CARRIER,BROADCAST,MULTICAST,UP"
group: "default"
Expand All @@ -52,6 +55,7 @@ parsed_sample:
qlen: ""
state: "DOWN"
type: "ether"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "NOARP,MASTER,UP,LOWER_UP"
group: "default"
Expand All @@ -65,6 +69,7 @@ parsed_sample:
qlen: "1000"
state: "UP"
type: "ether"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "NOARP,MASTER,UP,LOWER_UP"
group: "default"
Expand All @@ -78,6 +83,7 @@ parsed_sample:
qlen: "1000"
state: "UP"
type: "ether"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: "default"
Expand All @@ -91,6 +97,7 @@ parsed_sample:
qlen: "1000"
state: "UNKNOWN"
type: "ether"
alias: ""
- broadcast: "ff:ff:ff:ff:ff:ff"
flags: "BROADCAST,MULTICAST,UP,LOWER_UP"
group: "default"
Expand All @@ -104,3 +111,4 @@ parsed_sample:
qlen: "1000"
state: "UNKNOWN"
type: "ether"
alias: ""

0 comments on commit 6c722dd

Please sign in to comment.