Reputation: 11
I'm your typical networking guy and I'm totally new to this type of stuff. I'll do my best to explain what it is I need help with.
I'm using Cisco Prime to find all devices that contain two separate strings.
The first is-
device-sensor filter-list dhcp list DHCP_LIST_NAME
The second is-
ip address-helper 1.1.1.1
Here's the last thing I tried.
(device-sensor filter-list dhcp list DHCP_LIST_NAME)|(ip address-helper 1.1.1.1)
Ideally I'd like to match the first string, ignore any white space and any characters in between and then match the second string.
I've ran it through Regex101 and it seems to work but it just seems like there's a better if not cleaner way. From what I've read so far the pipe character matches one thing or something else. I need it to match both so I can tell Cisco Prime to raise a violation and make a fix.
Edit: Added sample configs.
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
Upvotes: -1
Views: 167
Reputation: 44283
Using regular expressions for searching for fixed length strings may be a bit of overkill, so this I think is the better, cleaner way.
This is JavaScript, but most languages allow you to search for a substring within another string and return the index of where it found the match or some error indication if the substring was not found:
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')
}
In 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')
Upvotes: 0
Reputation: 18939
You can use the following pattern:
/(device-sensor filter-list dhcp list DHCP_LIST_NAME).*(ip helper-address 1\.1\.1\.1)/igm
This will match the first term, then any characters till the next term. We need to escape the . in the ip address. As the pattern has the terms in brackets, two capture groups will be created. These are actually not needed, but included to demo you can return the matched groups.
Snippet below:
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);
Upvotes: 0