-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios-xe-native-acl-standard-lxml.py
65 lines (60 loc) · 1.72 KB
/
ios-xe-native-acl-standard-lxml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from lxml import etree
import c8000v
def iosXEStandardAcl(name, entries=[]):
config = etree.Element("config",
nsmap = {None: 'urn:ietf:params:xml:ns:netconf:base:1.0'}
)
# <native><interfaces><GigabitEthernet>
native = etree.SubElement(config, "native",
nsmap = {None: 'http://cisco.com/ns/yang/Cisco-IOS-XE-native'}
)
ip = etree.SubElement(native, "ip")
acl = etree.SubElement(ip, "access-list")
if len(entries) > 0:
standard_acl = etree.SubElement(acl, "standard",
nsmap = {None: 'http://cisco.com/ns/yang/Cisco-IOS-XE-acl'}
)
# Configure name and description
etree.SubElement(standard_acl, "name").text = name
for entry in entries:
acl_seq = "{seq}".format(seq=10*entries.index(entry)+10)
rule = etree.SubElement(standard_acl, "access-list-seq-rule")
etree.SubElement(rule, "sequence").text = acl_seq
action = etree.SubElement(rule, entry['action'])
line = etree.SubElement(action, "std-ace")
if entry['addr'] == 'any':
etree.SubElement(line, "any")
else:
etree.SubElement(
line,
"ipv4-address-prefix").text = entry['addr']
etree.SubElement(line, "ipv4-prefix").text = entry['addr']
if 'mask' in entry:
etree.SubElement(line, "mask").text = entry['mask']
else:
standard_acl = etree.SubElement(acl, "standard",
nsmap = {None: 'http://cisco.com/ns/yang/Cisco-IOS-XE-acl'},
operation="remove"
)
return config
config = iosXEStandardAcl(
name = "10",
entries=[
{
"action": "permit",
"addr": "10.10.10.0",
"mask": "0.0.0.255",
},
{
"action": "permit",
"addr": "10.10.20.5",
},
{
"action": "deny",
"addr": "any",
},
]
)
print(etree.tostring(config, pretty_print=True).decode())
#import c8000v
#c8000v.ncclient_configure([config])