Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ios utilities #146

Merged
merged 6 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions netutils/config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,83 @@ def build_config_relationship(self) -> t.List[ConfigLine]:
self._update_config_lines(line)
return self.config_lines

@staticmethod
def _match_type_check(line: str, pattern: str, match_type: str) -> bool:
"""Checks pattern for exact match or regex."""
if match_type == "exact" and line == pattern:
return True
if match_type == "startswith" and line.startswith(pattern):
return True
if match_type == "endswith" and line.endswith(pattern):
return True
if match_type == "regex" and re.match(pattern, line):
return True
return False

def find_all_children(self, pattern: str, match_type: str = "exact") -> t.List[str]:
"""Returns configuration part for a specific pattern not including parents.

Args:
pattern: pattern that describes parent.
match_type (optional): Exact or regex. Defaults to "exact".

Returns:
configuration under that parent pattern.
Example:
>>> config = '''
... router bgp 45000
... address-family ipv4 unicast
... neighbor 192.168.1.2 activate
... network 172.17.1.0 mask'''
>>> bgp_conf = BaseSpaceConfigParser(str(config)).find_all_children(pattern="router bgp", match_type="startswith")
>>> print(bgp_conf)
['router bgp 45000', ' address-family ipv4 unicast', ' neighbor 192.168.1.2 activate', ' network 172.17.1.0 mask']
"""
config = []
for cfg_line in self.build_config_relationship():
parents = cfg_line.parents[0] if cfg_line.parents else None
if (
parents
and self._match_type_check(parents, pattern, match_type)
or self._match_type_check(cfg_line.config_line, pattern, match_type)
):
config.append(cfg_line.config_line)
return config

def find_children_w_parents(
self, parent_pattern: str, child_pattern: str, match_type: str = "exact"
) -> t.List[str]:
"""Returns configuration part for a specific pattern including parents and children.

Args:
parent_pattern: pattern that describes parent.
child_pattern: pattern that describes child.
match_type (optional): Exact or regex. Defaults to "exact".

Returns:
configuration under that parent pattern.
Example:
>>> config = '''
... router bgp 45000
... address-family ipv4 unicast
... neighbor 192.168.1.2 activate
... network 172.17.1.0 mask'''
>>> bgp_conf = BaseSpaceConfigParser(str(config)).find_children_w_parents(parent_pattern="router bgp", child_pattern=" address-family", match_type="regex")
>>> print(bgp_conf)
[' address-family ipv4 unicast', ' neighbor 192.168.1.2 activate', ' network 172.17.1.0 mask']
"""
config = []
potential_parents = [
elem.parents[0]
for elem in self.build_config_relationship()
if self._match_type_check(elem.config_line, child_pattern, match_type)
]
for cfg_line in self.build_config_relationship():
parents = cfg_line.parents[0] if cfg_line.parents else None
if parents in potential_parents and self._match_type_check(parents, parent_pattern, match_type):
config.append(cfg_line.config_line)
return config


class BaseBraceConfigParser(BaseConfigParser):
"""Base parser class for config syntax that demarcates using braces."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
crypto pki trustpoint TP-self-signed-1088426642
enrollment selfsigned
subject-name cn=IOS-Self-Signed-Certificate-1088426642
revocation-check none
rsakeypair TP-self-signed-1088426642
crypto pki trustpoint SLA-TrustPoint
enrollment pkcs12
revocation-check crl
crypto pki certificate chain TP-self-signed-1088426642
certificate self-signed 01
quit
crypto pki certificate chain SLA-TrustPoint
certificate ca 01
quit
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
!
version 17.1
service timestamps debug datetime msec
service timestamps log datetime msec
! Call-home is enabled by Smart-Licensing.
service call-home
platform qfp utilization monitor load 80
platform punt-keepalive disable-kernel-core
platform console serial
!
hostname jcy-bb-01
!
boot-start-marker
boot-end-marker
!
!
vrf definition MANAGEMENT
!
address-family ipv4
exit-address-family
!
address-family ipv6
exit-address-family
!
logging userinfo
!
no aaa new-model
call-home
! If contact email address in call-home is configured as [email protected]
! the email address configured in Cisco Smart License Portal will be used as contact email address to send SCH notifications.
contact-email-addr [email protected]
profile "CiscoTAC-1"
active
destination transport-method http
!
!
!
!
!
!
!
no ip domain lookup
ip domain name infra.ntc.com
!
!
!
login on-success log
!
!
!
!
!
!
!
subscriber templating
!
!
!
!
!
!
multilink bundle-name authenticated
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
crypto pki trustpoint TP-self-signed-1088426642
enrollment selfsigned
subject-name cn=IOS-Self-Signed-Certificate-1088426642
revocation-check none
rsakeypair TP-self-signed-1088426642
!
crypto pki trustpoint SLA-TrustPoint
enrollment pkcs12
revocation-check crl
!
!
crypto pki certificate chain TP-self-signed-1088426642
certificate self-signed 01
quit
crypto pki certificate chain SLA-TrustPoint
certificate ca 01
quit
!
license udi pid CSR1000V sn 9SAGBHTUEE9
diagnostic bootup level minimal
archive
path bootflash:archive
memory free low-watermark processor 72107
!
!
spanning-tree extend system-id
!
username ntc privilege 15 password 0 ntc123
!
redundancy
!
!
!
!
!
lldp run
cdp run
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
interface Loopback0
ip address 10.0.10.3 255.255.255.255
!
interface GigabitEthernet1
description MANAGEMENT_DO_NOT_CHANGE
ip address 10.0.0.15 255.255.255.0
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet2
ip address 10.10.0.6 255.255.255.252
ip access-group BLOCK_TRANSIT_LINKS in
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet3
ip address 10.10.0.14 255.255.255.252
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet4
description backbone-to-vmx3-ge0/0/3
ip address 10.10.0.17 255.255.255.252
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet5
no ip address
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet6
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet7
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet8
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet9
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
router bgp 65251
bgp router-id 10.0.10.3
bgp log-neighbor-changes
redistribute connected
neighbor 10.10.0.5 remote-as 65251
neighbor 10.10.0.13 remote-as 65251
neighbor 10.10.0.18 remote-as 65252
!
!
virtual-service csr_mgmt
!
ip forward-protocol nd
ip http server
ip http authentication local
ip http secure-server
!
ip route 0.0.0.0 0.0.0.0 10.0.0.2
ip scp server enable
!
!
logging origin-id hostname
logging host 10.125.1.171 transport udp port 7004
!
!
snmp-server community ntc-public RO
snmp-server community ntc-private RW
snmp-server community networktocode RO
snmp-server community secure RW
snmp-server location Network to Code - NYC | NY
snmp-server contact John Smith
snmp-server host 10.1.1.1 version 2c networktocode
!
!
!
control-plane
!
!
!
!
banner exec ^C
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS *
* education. IOSv is provided as-is and is not supported by Cisco's *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any *
* purposes is expressly prohibited except as otherwise authorized by *
* Cisco in writing. *
**************************************************************************^C
banner incoming ^C
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS *
* education. IOSv is provided as-is and is not supported by Cisco's *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any *
* purposes is expressly prohibited except as otherwise authorized by *
* Cisco in writing. *
**************************************************************************^C
!
alias exec ntcclear clear platform software vnic-if nv
!
line con 0
stopbits 1
line vty 0 4
privilege level 15
login local
transport preferred ssh
transport input all
line vty 5 15
privilege level 15
login local
transport preferred ssh
transport input all
!
ntp server 10.1.1.1
ntp server 10.2.2.2 prefer
!
!
!
!
!
netconf-yang
restconf
end
Loading