How do I get the interface IP address from of a Cisco IOS configuration?

56 views Asked by At

When using the following Cisco IOS configuration, how do you get the interface IP address of GigabitEthernet1/3 with CiscoConfParse()?

!
hostname Example
!
interface GigabitEthernet1/1
 description Example interface
 ip address 192.0.2.1 255.255.255.128
 no ip proxy-arp
!
interface GigabitEthernet1/2
 shutdown
!
interface GigabitEthernet1/3
 ip address 192.0.2.129 255.255.255.128
 no ip proxy-arp
!
end

I tried using this, but it throws an AttributeError...

from ciscoconfparse import CiscoConfParse
config = """!
hostname Example
!
interface GigabitEthernet1/1
 description Example interface
 ip address 192.0.2.1 255.255.255.128
 no ip proxy-arp
!
interface GigabitEthernet1/2
 shutdown
!
interface GigabitEthernet1/3
 ip address 192.0.2.129 255.255.255.128
 no ip proxy-arp
!
end
"""

parse = CiscoConfParse(config.splitlines(), syntax='ios', factory=False)
intf = parse.find_objects("interface GigabitEthernet1/3")[0]
print(f"GigabitEthernet1/3 address: {intf.ipv4}")

This throws an AttributeError...

AttributeError: The ipv4 attribute does not exist
1

There are 1 answers

0
Mike Pennington On BEST ANSWER

There are two techniques to do this:

  • To access the ipv4 attribute of a Cisco IOS interface, you need to parse with factory=True; this returns an IPv4Obj().
  • You can also get the IP address as a string from find_child_objects() and factory=False.

factory=True and the ipv4 attribute

Explicitly...

from ciscoconfparse import CiscoConfParse
config = """!
hostname Example
!
interface GigabitEthernet1/1
 description Example interface
 ip address 192.0.2.1 255.255.255.128
 no ip proxy-arp
!
interface GigabitEthernet1/2
 shutdown
!
interface GigabitEthernet1/3
 ip address 192.0.2.129 255.255.255.128
 no ip proxy-arp
!
end
"""

parse = CiscoConfParse(config.splitlines(), syntax='ios', factory=True)
intf = parse.find_objects("interface GigabitEthernet1/3")[0]
print(f"GigabitEthernet1/3 address: {intf.ipv4}")
$ python example.py
GigabitEthernet1/3 address: <IPv4Obj 192.0.2.129/25>
$

Note that the factory=True feature is experimental.

factory=False and find_child_objects()

You can also get the interface IP address as a string by parsing with factory=False and use find_child_objects()...

>>> from ciscoconfparse import CiscoConfParse, IPv4Obj
>>> config = """!
... hostname Example
... !
... interface GigabitEthernet1/1
...  description Example interface
...  ip address 192.0.2.1 255.255.255.128
...  no ip proxy-arp
... !
... interface GigabitEthernet1/2
...  shutdown
... !
... interface GigabitEthernet1/3
...  ip address 192.0.2.129 255.255.255.128
...  no ip proxy-arp
... !
... end"""
>>> parse = CiscoConfParse(config.splitlines(), syntax='ios', factory=False)
>>> obj = parse.find_child_objects("interface GigabitEthernet1/3", "ip address")[0]
>>> addr = obj.re_match("ip\saddress\s(\S.+)")
>>> addr
'192.0.2.129 255.255.255.128'
>>> IPv4Obj(addr)
<IPv4Obj 192.0.2.129/25>
>>>