如何 grep Cisco 接口名称和该部分下的所有 IP 地址

如何 grep Cisco 接口名称和该部分下的所有 IP 地址

这是我从中获得的思科配置文件示例https://resources.intenseschool.com/mpls-basic-configuration/并对其进行了一些修改,使其与这个问题相关。

我将其保存到file.txt并尝试interface使用egrep 命名该部分下的所有IP 地址。

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
!

所需输出

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

这里有一些尝试,但没有一个产生我想要的输出。

尝试1

句法:egrep ^interface file.txt

问题:未捕获IP

[user@linux ~]$ egrep ^interface file.txt
interface Loopback0
interface FastEthernet0/0
interface FastEthernet0/1
interface Ethernet1/0
[user@linux ~]$

尝试2

句法:egrep -A2 ^interface file.txt

问题:数据太多,我只想要interface该部分下的线路和所有 IP 地址

[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 ~]$

尝试3

句法:egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt

问题:仅打印 IP 地址,我还想要接口名称

[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 ~]$

尝试4

句法:egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt

问题:几乎可以工作,但数据太多,我只想要interface该部分下的线路和所有 IP 地址

[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 ~]$

如果除此之外还有更好的工具可以使用egrep,请告诉我。

答案1

使用:

perl -lne 'print "\n$_" if /interface/; print if / ip ./' file

使用:

awk '/interface/{print "\n"$0} / ip ./' file

 输出

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 flow monitor all-traffic-monitor output
 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

相关内容