我是您典型的网络专家,对这种东西我是完全陌生的。我会尽力解释我需要什么帮助。
我正在使用Cisco Prime查找包含两个单独字符串的所有设备。
第一个是-
device-sensor filter-list dhcp list DHCP_LIST_NAME
第二个是-
ip address-helper 1.1.1.1
这是我最后尝试的方法。
(device-sensor filter-list dhcp list DHCP_LIST_NAME)|(ip address-helper 1.1.1.1)
理想情况下,我想匹配第一个字符串,忽略任何空格和之间的任何字符,然后匹配第二个字符串。
我已经通过Regex101运行了它,它似乎可以工作,但是似乎有一种更好甚至更干净的方法。从目前为止我所读的内容中,竖线字符匹配一件事或另一件事。我需要将其匹配,以便我可以告诉Cisco Prime提出违规并进行修复。
编辑:添加了示例配置。
device-sensor filter-list cdp list CDP_LIST_NAME
*device-sensor filter-list dhcp list DHCP_LIST_NAME*
device-sensor filter-list lldp list LLDP_LIST_NAME
device-sensor filter-spec dhcp include list DHCP_LIST_NAME
device-sensor filter-spec lldp include list LLDP_LIST_NAME
device-sensor filter-spec cdp include list CDP_LIST_NAME
device-sensor accounting
device-sensor notify all-changes
!
!
interface Vlan1
description PRINTERs
ip address 2.2.2.2 255.255.255.0
ip helper-address 10.10.10.1
ip helper-address 10.10.10.2
ip helper-address 1.1.1.1
no ip redirects
no ip unreachables
您可以使用以下模式:
/(device-sensor filter-list dhcp list DHCP_LIST_NAME).*(ip helper-address 1\.1\.1\.1)/igm
这将与第一个词匹配,然后与下一个词匹配。我们需要逃脱。在IP地址中。由于该模式在方括号中包含术语,因此将创建两个捕获组。这些实际上不是必需的,但是包含在演示中,您可以返回匹配的组。
下面的代码段:
const data = 'device-sensor filter-list cdp list CDP_LIST_NAME device-sensor filter-list dhcp list DHCP_LIST_NAME device-sensor filter-list lldp list LLDP_LIST_NAME device-sensor filter-spec dhcp include list DHCP_LIST_NAME device-sensor filter-spec lldp include list LLDP_LIST_NAME device-sensor filter-spec cdp include list CDP_LIST_NAME device-sensor accounting device-sensor notify all-changes ! ! interface Vlan1 description PRINTERs ip address 2.2.2.2 255.255.255.0 ip helper-address 10.10.10.1 ip helper-address 10.10.10.2 ip helper-address 1.1.1.1 no ip redirects no ip unreachables';
let match = /(device-sensor filter-list dhcp list DHCP_LIST_NAME).*(ip helper-address 1\.1\.1\.1)/igm.test(data);
console.log(match);
使用正则表达式搜索固定长度的字符串可能有点过分。这是JavaScript,但是大多数语言都允许您在另一个字符串中搜索子字符串,并返回找到匹配项的索引,或者如果找不到子字符串则返回一些错误指示:
const text = 'device-sensor filter-list cdp list CDP_LIST_NAME device-sensor filter-list dhcp list DHCP_LIST_NAME\ndevice-sensor filter-list lldp list LLDP_LIST_NAME device-sensor filter-spec dhcp include list DHCP_LIST_NAME device-sensor filter-spec lldp include list LLDP_LIST_NAME device-sensor filter-spec cdp include list CDP_LIST_NAME device-sensor accounting device-sensor notify all-changes ! ! interface Vlan1 description PRINTERs ip address 2.2.2.2 255.255.255.0 ip helper-address 10.10.10.1 ip helper-address 10.10.10.2 ip helper-address 1.1.1.1 no ip redirects no ip unreachables';
if (text.indexOf('device-sensor filter-list dhcp list DHCP_LIST_NAME') != -1 &&
text.indexOf('ip helper-address 1.1.1.1') != -1) {
console.log('Found it')
}
在Python中:
text = """device-sensor filter-list cdp list CDP_LIST_NAME
*device-sensor filter-list dhcp list DHCP_LIST_NAME*
device-sensor filter-list lldp list LLDP_LIST_NAME
device-sensor filter-spec dhcp include list DHCP_LIST_NAME
device-sensor filter-spec lldp include list LLDP_LIST_NAME
device-sensor filter-spec cdp include list CDP_LIST_NAME
device-sensor accounting
device-sensor notify all-changes
!
!
interface Vlan1
description PRINTERs
ip address 2.2.2.2 255.255.255.0
ip helper-address 10.10.10.1
ip helper-address 10.10.10.2
ip helper-address 1.1.1.1
no ip redirects
no ip unreachables"""
if ('device-sensor filter-list dhcp list DHCP_LIST_NAME' in text
and 'ip helper-address 1.1.1.1' in text):
print('Found it')