pythonciscociscoconfparse

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


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 ciscoconfparse2 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

Solution

  • There are two techniques to do this:

    factory=True and the ipv4 attribute

    Explicitly...

    from ciscoconfparse2 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 ciscoconfparse2 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>
    >>>