pip
installedpip install -U ciscoconfparse
pip3 install -U ciscoconfparse
The following examples will use this configuration:
! filename:exampleswitch.conf
!
hostname ExampleSwitch
!
interface GigabitEthernet 1/1
switchport mode trunk
shutdown
!
interface GigabitEthernet 1/2
switchport mode access
switchport access vlan 20
switchport nonegotiate
no cdp enable
!
interface GigabitEthernet 1/3
no switchport
ip address 192.0.2.1 255.255.255.0
Use find_objects()
to give us a list of interface objects:
from ciscoconfparse import CiscoConfParse
# Parse the config into objects
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
# Iterate over all the interface objects
for intf_obj in parse.find_objects('^interface'):
print("ciscoconfparse object: " + intf_obj)
Output:
ciscoconfparse object: <IOSCfgLine # 4 'interface GigabitEthernet 1/1'>
ciscoconfparse object: <IOSCfgLine # 8 'interface GigabitEthernet 1/2'>
ciscoconfparse object: <IOSCfgLine # 14 'interface GigabitEthernet 1/3'>
Use intf_obj.children
to iterate over an object's children:
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', sytax='ios')
# Choose the first interface (parent) object
for intf_obj in parse.find_objects('^interface')[0:1]:
print("Parent obj: " + str(intf_obj))
# Iterate over all the child objects of that parent object
for c_obj in intf_obj.children:
print("Child obj : " + str(c_obj))
Output:
Parent obj: <IOSCfgLine # 4 'interface GigabitEthernet 1/1'>
Child obj : <IOSCfgLine # 5 ' switchport mode trunk' (parent is # 4)>
Child obj : <IOSCfgLine # 6 ' shutdown' (parent is # 4)>
Use intf_obj.text
to get the object's configuration text
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
# Choose the first interface (parent) object
for intf_obj in parse.find_objects('^interface')[0:1]:
print(intf_obj.text)
Output:
interface GigabitEthernet 1/1
Use find_objects_w_child()
to give us a list of shutdown interfaces:
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
for intf_obj in parse.find_objects_w_child('^interface', '^\s+shutdown'):
print("Shutdown: " + intf_obj.text)
Output:
Shutdown: interface GigabitEthernet1/1
re_match_iter_typed()
to extract values with a regex match groupfrom ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('exampleswitch.conf', syntax='ios')
intf_obj = parse.find_objects('interface\s+GigabitEthernet1\/3$')
# Search children of GigabitEthernet1/3 for a regex match and return
# the value matched in regex match group 1. If there is no match, return a
# default value: ''
intf_ip_addr = intf_obj.re_match_iter_typed(
r'ip\saddress\s(\d+\.\d+\.\d+\.\d+)\s', result_type=str,
group=1, default='')
print("ip addr: " + intf_ip_addr)
Output:
ip addr: 192.0.2.1
factory=True
CiscoConfParse('exampleswitch.conf', syntax='ios', factory=True)
gives you more informationfactory=True
factory=True
automatically extracts interface values>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('exampleswitch', syntax='ios', factory=True)
>>> intf = parse.find_objects('interface\sGigabitEthernet0/3$')[0]
>>> intf
<IOSIntfLine # 14 'GigabitEthernet1/3' info: '192.0.2.1/24'>
>>> intf.name
'GigabitEthernet1/3'
>>> intf.ipv4_addr
'192.0.2.1'
>>> intf.ipv4_netmask
'255.255.255.0
>>>
factory=True
: Important caveatsfactory=True
is BETA functionalityfactory=True
[mpenning@localhost]$ python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('/path/to/the/config', syntax='ios')
>>>
>>> # Standardize switchport configs with 0.5% broadcast storm control
>>> parse.replace_children(r'^interface\s\S+?thernet',
... r'broadcast\slevel\s\S+', 'broadcast level 0.5')
...
[' storm-control broadcast level 0.5']
>>>
>>> # Now save the new version...
>>> parse.save_as('/path/to/the/newconfig')
>>> from ciscoconfparse import CiscoConfParse
>>> BASELINE = """!
... interface GigabitEthernet0/1
... ip address 10.0.0.1 255.255.255.0
... !""".splitlines()
>>> REQUIRED_CONFIG = """!
... interface GigabitEthernet0/1
... ip address 172.16.1.1 255.255.255.0
... no ip proxy-arp
... !""".splitlines()
>>> parse = CiscoConfParse(BASELINE)
>>>
>>> # Build diffs to convert the BASELINE to the REQUIRED config
>>> print '\n'.join(parse.sync_diff(REQUIRED_CONFIG, ''))
interface GigabitEthernet0/1
no ip address 10.0.0.1 255.255.255.0
interface GigabitEthernet0/1
ip address 172.16.1.1 255.255.255.0
no ip proxy-arp
>>>
interface GigabitEthernet0/1
twice.CiscoRange()
can help you iterate over ranges of values
>>> from ciscoconfparse.ccp_util import CiscoRange
>>> intfs = CiscoRange('Gi1/0/1-5')
>>> for ii in intfs:
... print ii
...
Gi1/0/1
Gi1/0/2
Gi1/0/3
Gi1/0/4
Gi1/0/5
>>> 'Gi1/0/2' in intfs
True
>>> 'Gi1/0/8' in intfs
False
>>>
dns_query()
>>> from ciscoconfparse.ccp_util import dns_query
>>> dns_query('www.pennington.net', 'A', '4.2.2.2')
set([<DNSResponse 'A' result_str='65.19.187.2'>])
>>> answer = dns_query('www.pennington.net', 'A', '4.2.2.2')
>>> str(answer.pop())
'65.19.187.2'
>>>
>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('configs/sample_01.junos', syntax='junos',
... comment='#!')
>>> print '\n'.join(parse.ioscfg[0:5])
!# Last commit: 2015-06-28 13:00:59 CST by mpenning
system
host-name TEST01_EX
domain-name pennington.net
domain-search [ pennington.net lab.pennington.net ]
>>>
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |