-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathremove-configs.py
executable file
·58 lines (43 loc) · 2.01 KB
/
remove-configs.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
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=0 noexpandtab
"""
Copyright (c) 2014, Cisco
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Version 0.2, written 2016-01-15 by [email protected]
"""
import xml.etree.ElementTree as ET
import errno, sys
def main():
ET.register_namespace('', "http://www.cisco.com/VIRL")
virl_ns = "{http://www.cisco.com/VIRL}"
if len(sys.argv) == 2:
with open(str(sys.argv[1]).strip(), "r") as fh:
root = ET.parse(fh)
else:
root = ET.parse(sys.stdin)
nodes = root.findall("{0}node".format(virl_ns))
for node in nodes:
# delete all config extensions
extensions = node.findall("{0}extensions".format(virl_ns))
for extension in extensions:
config_elem = extension.find("{0}entry[@key='config']".format(virl_ns))
if config_elem is not None:
extension.remove(config_elem)
# remove all address attributes from interfaces
# so ANK can start from a clean slate
interfaces = node.findall("{0}interface".format(virl_ns))
for attr in [ "ipv4", "ipv6", "netPrefixLenV4", "netPrefixLenV6" ]:
for interface in interfaces:
if interface.get(attr) is not None:
del interface.attrib[attr]
# write result to stdout
with sys.stdout as fh:
try:
root.write(fh, xml_declaration=True, encoding='UTF-8')
except IOError, e:
if e.errno == errno.EPIPE:
pass
return 0
if __name__ == '__main__':
sys.exit(main())