Sample Header Ad - 728x90

How to grep Cisco interface name and all IP Addresses under that section

0 votes
1 answer
683 views
This is sample of Cisco config file which I get from https://resources.intenseschool.com/mpls-basic-configuration/ and modified it a little bit to make it relevent to this question. I saved it to file.txt and trying to interface name and all IP addresses under that section using egrep. **file.txt** interface Loopback0 description ** test ** ip address 10.1.1.11 255.255.255.255 ! interface FastEthernet0/0 description ** Connection to ABC ** bandwidth 3000 ip address 10.0.12.1 255.255.255.0 mpls ip ! interface FastEthernet0/1 ip flow monitor all-traffic-monitor output ip address 10.0.100.1 255.255.255.0 ! interface Ethernet1/0 description ** Connection to XYZ ** ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 ! router ospf 1 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 ! router bgp 14 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 redistribute static no auto-summary ! ip route 192.168.100.0 255.255.255.0 10.0.100.10 ! **Desired Output** interface Loopback0 ip address 10.1.1.11 255.255.255.255 interface FastEthernet0/0 ip address 10.0.12.1 255.255.255.0 interface FastEthernet0/1 ip address 10.0.100.1 255.255.255.0 interface Ethernet1/0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 Here are a few attempts but none of them producing the output that I wanted. **Attempt 1** Syntax: egrep ^interface file.txt Problem: No IP captured [user@linux ~]$ egrep ^interface file.txt interface Loopback0 interface FastEthernet0/0 interface FastEthernet0/1 interface Ethernet1/0 [user@linux ~]$ **Attempt 2** Syntax: egrep -A2 ^interface file.txt Problem: Too much data, I just want interface line and all IP addresses under that section [user@linux ~]$ egrep -A2 ^interface file.txt interface Loopback0 description ** test ** ip address 10.1.1.11 255.255.255.255 -- interface FastEthernet0/0 description ** Connection to ABC ** bandwidth 3000 -- interface FastEthernet0/1 ip flow monitor all-traffic-monitor output ip address 10.0.100.1 255.255.255.0 -- interface Ethernet1/0 description ** Connection to XYZ ** ip address 10.0.13.1 255.255.255.0 [user@linux ~]$ **Attempt 3** Syntax: egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt Problem: Print IP Address only, I want interface name as well [user@linux ~]$ egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt ip address 10.1.1.11 255.255.255.255 ip address 10.0.12.1 255.255.255.0 ip address 10.0.100.1 255.255.255.0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 ip route 192.168.100.0 255.255.255.0 10.0.100.10 [user@linux ~]$ **Attempt 4** Syntax: egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt Problem: Almost work, but too much data, I just want interface line and all IP addresses under that section [user@linux ~]$ egrep '^interface|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt interface Loopback0 ip address 10.1.1.11 255.255.255.255 interface FastEthernet0/0 ip address 10.0.12.1 255.255.255.0 interface FastEthernet0/1 ip address 10.0.100.1 255.255.255.0 interface Ethernet1/0 ip address 10.0.13.1 255.255.255.0 ip helper-address 10.1.1.1 ip helper-address 10.1.1.2 network 1.1.1.1 0.0.0.0 area 0 network 10.0.12.0 0.0.0.255 area 0 network 10.0.13.0 0.0.0.255 area 0 neighbor 10.4.4.4 remote-as 14 neighbor 10.4.4.4 update-source Loopback0 network 10.0.100.0 mask 255.255.255.0 ip route 192.168.100.0 255.255.255.0 10.0.100.10 [user@linux ~]$ If there are better tools to use besides egrep, please let me know.
Asked by user11392987 (159 rep)
Jan 6, 2020, 08:08 AM
Last activity: Jan 6, 2020, 06:01 PM