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
There are two techniques to do this:
ipv4attribute of a Cisco IOS interface, you need to parse withfactory=True; this returns anIPv4Obj().find_child_objects()andfactory=False.factory=True and the
ipv4attributeExplicitly...
Note that the
factory=Truefeature is experimental.factory=False and
find_child_objects()You can also get the interface IP address as a string by parsing with
factory=Falseand usefind_child_objects()...