Created
May 19, 2020 21:30
-
-
Save h4ndzdatm0ld/cd4bf5439acfae2133946632a10b1685 to your computer and use it in GitHub Desktop.
Establish a netconf connection and create a backup/xml dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def netcbackup(ip, NETCONF_USER, NETCONF_PASS): | |
''' This function will establish a netconf connection and pull the running config. It will write a temp file, | |
read it and convert the XML to a python dictionary. Once parsed, we'll pull the system name of the device | |
and create a folder structure by hostname and backup the running config. | |
''' | |
try: | |
# Now let's connect to the device via NETCONF and pull the config to validate | |
nc = netconfconn(ip, NETCONF_USER, NETCONF_PASS) | |
# Grab the running configuration on our device, as an NCElement. | |
config = nc.get_config(source='running') | |
# XML elemnent as a str. | |
xmlconfig = to_xml(config.xpath('data')[0]) | |
# Write the running configuration to a temp-file (from the data/configure xpath). | |
saveFile('temp-config.xml', xmlconfig) | |
# Lets open the XML file, read it, and convert to a python dictionary and extract some info. | |
with open('temp-config.xml', 'r') as temp: | |
content = temp.read() | |
xml = xmltodict.parse(content) | |
sys_name = xml['data']['configure']['system']['name'] | |
createFolder(f"Configs/{sys_name}") | |
saveFile( | |
f"Configs/{sys_name}/{sys_name}.txt", xmlconfig) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment