1

I wish match some line in Cisco configuration based on the destination. To make some test I made the following:

acl = 'ip route 10.5.48.0 255.255.255.0 10.242.245.65'
firewall = '10.242.245.65'
ip = r"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)"
ip_route = (f"ip route {ip} {ip} {firewall}")

if re.search(ip_route, acl):
    print(acl)

No result. I guess the problem is withe the variable ip_route.

0

2 Answers 2

1

Your problem is with the anchors ^ and $ in ip:

acl = 'ip route 10.5.48.0 255.255.255.0 10.242.245.65'
firewall = '10\.242\.245\.65'

# ip = r"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)"
#         ^                                  ^ there is your problem
# Should be:
ip = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"

ip_route = (f"ip route {ip} {ip} {firewall}")

if re.search(ip_route, acl):
    print(acl)

Those anchors assert beginning and end of the string respectively; you are using them in the middle of your constructed match.

Also (but not affecting this match) you should escape the . in firewall -- otherwise that regex metacharacter matches any character -- not just a dot.

Sign up to request clarification or add additional context in comments.

Comments

0

The ip regex contains ^ and $, which mean beginning and end of string.

But then ip is inserted in the middle of ip_route which is then used in re.search.

So you search for "something, beginning of string, something, end of string, something", which doesn't match acl because it doesn't contain "beginning of string" and "end of string" in the middle.

So: remove ^ and $ from ip.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.