mpenning / ciscoconfparse

Parse, Audit, Query, Build, and Modify Arista / Cisco / Juniper / Palo Alto / F5 configurations.
http://www.pennington.net/py/ciscoconfparse/
GNU General Public License v3.0
793 stars 220 forks source link

ip address of an interface with CIDR notation #167

Closed x0341 closed 4 years ago

x0341 commented 4 years ago

trying to get the IP address and netmask when using a CIDR notation, but can't get this working:

IPv4_REGEX = r"\s*(?i)ip\saddress\s(\S+\s+\S+)" for cmd in interface_cmd.re_search_children(IPv4_REGEX): ipv4_addr = interface_cmd.re_match_iter_typed(IPv4_REGEX, result_type=IPv4Obj) result["network_interfaces"][num_interfaces]["ipv4_address"] = ipv4_addr.ip.exploded result["network_interfaces"][num_interfaces]["ipv4_netmask"] = ipv4_addr.netmask.exploded

mpenning commented 4 years ago

The way to get the address and netmask in CIDR notation is possible by doing this:

>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> ipv4_addr = IPv4Obj('172.16.1.5 255.255.255.0')
>>> print(str(ipv4_addr.ip)+'/'+str(ipv4_addr.prefixlen))
172.16.1.5/24
>>>

However, I just built an explicit as_cidr_addr property for this use case directly onto IPv4Obj in version 1.4.9...

>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> ipv4_addr = IPv4Obj('172.16.1.5 255.255.255.0')
>>> print(ipv4_addr.as_cidr_addr)
172.16.1.5/24
>>>