我是 TCL.I 的新手,只想通过在 TCL 中提供 ip 地址来提取接口名称,并使用正则表达式进行 Expect
I am new to TCL.I just want to extract interface name by giving ip address in TCL and Expect using regexp
我的输入数据是这样的:
Interface IP-Address OK? Method Status Protocol
Embedded-Service-Engine0/0 unassigned YES NVRAM administratively down down
GigabitEthernet0/0 unassigned YES NVRAM up up
GigabitEthernet0/0.10 10.1.1.1 YES NVRAM up up
GigabitEthernet0/0.20 20.1.1.2 YES NVRAM up up
GigabitEthernet0/1 192.168.2.1 YES NVRAM up up
GigabitEthernet0/2 192.168.1.1 YES NVRAM up up
我想要一种方法来提取给定 IP 地址的接口名称。例如,如果输入是192.168.1.1
,输出应该是GigabitEthernet0/2
有人可以帮助我吗?我试过这个:
regexp -line -- ^.*?(?=(?:\..*?)?\s$ip) $input
到目前为止,解决此类问题的最简单方法通常是解析出行以进行某种映射,可能在数组中,然后在其中进行查找。要解析数据,我们将使用 regexp -all -line -inline
;这是一个 非常 有用的组合,因为它会生成我们可以使用 foreach
处理的列表来制作我们的地图。
# You might read this data from another program or from a file; that's good too...
set data "Interface IP-Address OK? Method Status Protocol
Embedded-Service-Engine0/0 unassigned YES NVRAM administratively down down
GigabitEthernet0/0 unassigned YES NVRAM up up
GigabitEthernet0/0.10 10.1.1.1 YES NVRAM up up
GigabitEthernet0/0.20 20.1.1.2 YES NVRAM up up
GigabitEthernet0/1 192.168.2.1 YES NVRAM up up
GigabitEthernet0/2 192.168.1.1 YES NVRAM up up "
# Build the mapping; the “-” in the variable name list is for skipping some unwanted stuff
foreach {- interface ip} [regexp -all -line -inline {^(\w+/\d+)\s+([\d.]+)} $data] {
set mapToIP($interface) $ip
set mapToInterface($ip) $interface
}
然后我们可以随时轻松地进行查找:
set myIP 192.168.1.1
puts "$myIP is mapped to interface: $mapToInterface($myIP)"
FWIW,您真的 需要确保始终将 RE 放在大括号中,因为它可以避免各种问题。虽然这不是一个 100% 的规则,你应该总是支撑你的 REs,你应该这样做,直到你绝对不能避免在运行时从单独的部分构建 REs,这在实际代码中是非常罕见的事情。
我的输入数据是这样的:
Interface IP-Address OK? Method Status Protocol Embedded-Service-Engine0/0 unassigned YES NVRAM administratively down down GigabitEthernet0/0 unassigned YES NVRAM up up GigabitEthernet0/0.10 10.1.1.1 YES NVRAM up up GigabitEthernet0/0.20 20.1.1.2 YES NVRAM up up GigabitEthernet0/1 192.168.2.1 YES NVRAM up up GigabitEthernet0/2 192.168.1.1 YES NVRAM up up
我想要一种方法来提取给定 IP 地址的接口名称。例如,如果输入是192.168.1.1
,输出应该是GigabitEthernet0/2
有人可以帮助我吗?我试过这个:
regexp -line -- ^.*?(?=(?:\..*?)?\s$ip) $input
到目前为止,解决此类问题的最简单方法通常是解析出行以进行某种映射,可能在数组中,然后在其中进行查找。要解析数据,我们将使用 regexp -all -line -inline
;这是一个 非常 有用的组合,因为它会生成我们可以使用 foreach
处理的列表来制作我们的地图。
# You might read this data from another program or from a file; that's good too...
set data "Interface IP-Address OK? Method Status Protocol
Embedded-Service-Engine0/0 unassigned YES NVRAM administratively down down
GigabitEthernet0/0 unassigned YES NVRAM up up
GigabitEthernet0/0.10 10.1.1.1 YES NVRAM up up
GigabitEthernet0/0.20 20.1.1.2 YES NVRAM up up
GigabitEthernet0/1 192.168.2.1 YES NVRAM up up
GigabitEthernet0/2 192.168.1.1 YES NVRAM up up "
# Build the mapping; the “-” in the variable name list is for skipping some unwanted stuff
foreach {- interface ip} [regexp -all -line -inline {^(\w+/\d+)\s+([\d.]+)} $data] {
set mapToIP($interface) $ip
set mapToInterface($ip) $interface
}
然后我们可以随时轻松地进行查找:
set myIP 192.168.1.1
puts "$myIP is mapped to interface: $mapToInterface($myIP)"
FWIW,您真的 需要确保始终将 RE 放在大括号中,因为它可以避免各种问题。虽然这不是一个 100% 的规则,你应该总是支撑你的 REs,你应该这样做,直到你绝对不能避免在运行时从单独的部分构建 REs,这在实际代码中是非常罕见的事情。