Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import getpass
- import datetime
- import pyping
- import colorama
- from termcolor import colored
- from termcolor import cprint
- from lxml import etree
- from jnpr.junos import Device
- from jnpr.junos.utils.config import Config
- from jnpr.junos.exception import *
- from jnpr.junos.facts import *
- """ This is used to get the name of the IP list that will be used for the script """
- location = raw_input("Select location: ")
- colorama.init()
- bold_green = lambda x: cprint(x, 'green', attrs=['bold'])
- green = lambda x: cprint(x, 'green')
- yellow = lambda x: cprint(x, 'yellow', attrs=['bold'])
- red = lambda x: cprint(x, 'red', attrs=['bold'], file=sys.stderr)
- """ The username is specified here and you will be prompted for a password. """
- username = raw_input('Username:')
- password = getpass.getpass("Password:")
- """ This is the error log """
- fail_log = 'junos-config-change-error-report-%s.txt' % location
- """ This is the 'out-file' that the script writes results to """
- f = open('junos-config-change-results-%s.txt' % location, 'w')
- fail = open(fail_log, 'a')
- def update_config(host):
- """Our main function 'update_config' is defined below"""
- def date_time():
- """function used to grab the date and time in an easy to read format"""
- date = str(datetime.datetime.now())
- return date
- def snmp_location():
- """function used to grab the contents of the snmp description in the config"""
- try:
- data = dev.rpc.get_config() # use 'normalize=True' when possible
- location = data.xpath('//location')
- location = etree.tostring(location[0])
- location = location.replace("<location>", "").replace("</location>\n", "")
- return location
- except Exception as err:
- print "Error in snmp_location()!"
- f.write("Error: snmp_location(): {0} ".format(err) + '\n')
- model = dev.facts['model']
- f.write("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n")
- f.write("Working on: " + host + '\n')
- f.write(snmp_location() + '\n')
- print "Working on:", host
- print snmp_location()
- f.write(date_time() + '\n')
- print datetime.datetime.now()
- print "Model:", model
- f.write("Model: " + model + '\n')
- """ This binds Config to dev """
- conf = Config(dev)
- hostname = dev.facts['hostname']
- """ This is where the ping function is defined.
- This function is used later in the script to test connectivity to the SRX """
- def pingtest(host):
- """function is used to see if the host is still pingable"""
- tryping = pyping.ping(host.strip())
- if tryping.ret_code is 0:
- return True
- else:
- return False
- """ This is where the Juniper 'set' commands are defined.
- This command is just used as an example and is only locally significant """
- set_commands = """
- set system scripts op file sit.slax description "null"
- """
- f.write("Locking the configuration...\n")
- print "Locking the configuration..."
- """ This command locks the configuration for a 'configure exclusive' """
- try:
- conf.lock()
- f.write("Configuration locked\n")
- green("Configuration locked")
- except (KeyboardInterrupt, SystemExit):
- raise
- except LockError as err:
- f.write("Error: Unable to lock configuration: {0} ".format(err) + '\n')
- red("Error: Unable to lock configuration...")
- except Exception as err:
- print err
- f.write("Error in conf.lock(): {0} ".format(err) + '\n')
- f.write("Checking for uncommitted configuration changes...\n")
- print "Checking for uncommitted configuration changes..."
- """ This command is the equivilant of 'show | compare' """
- if conf.diff() is None:
- f.write("There are no uncommitted changes\n")
- green("There are no uncommitted changes")
- else:
- f.write("There are uncommitted changes...rolling back...\n")
- yellow("There are uncommmitted changes...rolling back...")
- try:
- conf.rollback(rb_id=0) # This command issues a rollback. 'rb_id=' is the rollabck number: 0 - 5
- f.write("Configuration has been rolled back to 0\n")
- f.write("Locking configuration...\n")
- green("Configuration has been rolled back!")
- try:
- f.write("Double checking 'show | compare'\n")
- print "Double checking 'show | compare'"
- if conf.diff() is None:
- try:
- print "Locking configuration..."
- conf.lock() # This command is used to lock the configuration in the event that the first
- f.write("Configuration locked\n") # configuiration lock failed due to
- print "Configuration locked" # uncommitted configuration changes
- except (KeyboardInterrupt, SystemExit):
- raise
- except LockError as err:
- f.write("Unable to lock configuration: {0} ".format(err) + '\n')
- red("Error: Unable to lock configuration!")
- f.write("Exiting this device immediately!\n")
- print "Exiting this device immediately!"
- return
- except Exception as err:
- print err
- f.write("Error in conf.lock(): {0} ".format(err) + '\n')
- return
- else:
- fail.write("There were problems removing the pending \
- configuration changes on " + hostname + '\n')
- f.write("There were problems removing the pending configuration changes\n")
- f.write("Exiting now...\n")
- print "There were problems removing the pending configuration changes"
- print "Exiting now"
- return
- except (KeyboardInterrupt, SystemExit):
- raise
- except Exception as err:
- fail.write("There were probelms running 'show | compare' on " + hostname + '\n')
- fail.write(err + '\n')
- f.write("There were errors running 'show | compare'\n")
- f.write("Exiting device immediately\n")
- print "There were errors running 'show | compare' "
- print err
- print "Exiting now"
- return
- except (KeyboardInterrupt, SystemExit):
- raise
- except Exception as err:
- fail.write("Failed to rollback on " + hostname + '\n')
- fail.write(err + '\n')
- f.write("Failed to rollback...\n")
- f.write("Exiting this device\n")
- print "Failed to rollback..."
- print err
- print "Exiting this device"
- dev.close()
- return
- f.write("Loading the configuration changes...\n")
- print "Loading the configuration changes..."
- """ This command loads the configuration changes.
- The 'merge=False' parameter means it will overwrite existing configurations """
- try:
- conf.load(set_commands, format='set', merge=False)
- except (KeyboardInterrupt, SystemExit):
- raise
- except ValueError as err:
- fail.write("Failed to load configuration changes {0} ".format(err) + '\n')
- f.write("Loading the configuration changes failed: {0} ".format(err) + '\n')
- f.write("Exiting the device\n")
- red(err.message)
- return
- except ConfigLoadError as err:
- fail.write("Failed to load configuration changes {0} ".format(err) + '\n')
- f.write("Loading the configuration changes failed: {0} ".format(err) + '\n')
- f.write("Exiting the device\n")
- red(err.message)
- return
- except Exception as err:
- print err
- f.write("Error: Failed to load configuration changes {0} ".format(err) + '\n')
- return
- try:
- f.write("Checking commit...\n")
- print "Checking commit..."
- conf.commit_check()
- f.write("Commit check passed\n")
- print "Commit check passed with 0 errors"
- except CommitError as err:
- fail.write("Commit check did NOT pass {0} ".format(err) + '\n')
- f.write("Commit check did NOT pass! {0} ".format(err) + '\n')
- print "Commit check did NOT pass! Check log file for more"
- return
- except RpcError as err:
- fail.write("Commit check did NOT pass {0} ".format(err) + '\n')
- f.write("Commit check did NOT pass! {0} ".format(err) + '\n')
- print "Commit check did NOT pass! Check log file for more"
- return
- except Exception as err:
- print err
- f.write("Commit check did NOT pass: {0} ".format(err) + '\n')
- return
- f.write("Committing the configuration...\n")
- print "Committing the configuration changes..."
- """ This command commits the config with a comment and 1 minute to confirm """
- try:
- conf.commit(confirm=1)
- f.write("Commit confirm 1 successful!\n")
- bold_green("Commit confirm 1 successful!")
- print "Verifying connectivity..."
- except (KeyboardInterrupt, SystemExit):
- raise
- except CommitError as err:
- fail.write("Commit failed or broke connectivity: {0} ".format(err) + '\n')
- f.write("Commit failed or broke conectivity: {0} ".format(err) + '\n')
- f.write("Pinging to verify...\n")
- red("Commit failed or broke connectivity")
- print "Pinging to verify..."
- except RpcTimeoutError as err:
- fail.write("Commit failed or broke connectivity: {0} ".format(err) + '\n')
- f.write("Commit failed or broke conectivity: {0} ".format(err) + '\n')
- f.write("Pinging to verify...\n")
- red("Commit failed or broke connectivity")
- print "Pinging to verify..."
- except Exception as err:
- print err
- f.write("Commit failed or broke connectivity: {0} ".format(err) + '\n')
- """ This command is used to ping the host to see if applying the configuration broke the connectivity """
- if pingtest(host) is True:
- f.write(host.strip() + " looks up from here\n")
- f.write("Confirming the configuration...\n")
- print host.strip(), "looks UP from here"
- print "Confirming the configuration..."
- try:
- conf.commit(comment="COMMIT MESSAGE GOES HERE") # If the ping succeeds it issues a 'commit confirm'
- f.write("Configuration was confirmed!\n")
- f.write("Unlocking the configuration...\n")
- bold_green("Configuration was confirmed!")
- print "Unlocking the configuration..."
- try:
- conf.unlock() # This command unlocks the configuration
- except (KeyboardInterrupt, SystemExit):
- raise
- except UnlockError as err:
- f.write("Unlocking the configuration failed: {0} ".format(err) + '\n')
- red("Unlocking the configuration failed")
- except Exception as err:
- print err
- f.write("Error unlocking the configuration: {0} ".format(err) + '\n')
- f.write("Closing connection to " + hostname + '\n')
- print "Closing the connection to", hostname
- try:
- dev.close() # This command closes the device connection
- except (KeyboardInterrupt, SystemExit):
- raise
- except Exception as err:
- f.write("Failed to close: " + err + hostname + '\n')
- print "Failed to close", hostname, err
- except (KeyboardInterrupt, SystemExit):
- raise
- except CommitError as err:
- fail.write("Commit failed: {0} ".format(err) + '\n')
- f.write("Failed to commit changes: {0} ".format(err) + '\n')
- print "Failed to commit the changes"
- except RpcTimeoutError as err:
- print "RPC timeout error on %s!" % host
- f.write("RPC timeout error on %s!" % host + '\n')
- fail.write("RPC timeout error on %s!" % host + '\n')
- except Exception as err:
- print err
- f.write("Error: {0} ".format(err) + '\n')
- """ This detects ping failure. If the ping fails,
- the script will exit the current host without confirming
- the commit and the configuration will rollback in 1 minute """
- else:
- f.write(host.strip() + " looks DOWN from here...\n")
- fail.write(host + "looks down from here\n")
- f.write("Moving on to the next host...\n")
- print host, "looks down from here..."
- print "Moving on to the next host..."
- f.write("Completed: " + hostname + '\n')
- f.write("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n")
- print "Completed:", hostname
- print ""
- """ This is used to loop through every host entry in the list specified """
- with open('C:\\ip_lists/%s-list.txt' % location) as infile:
- for host in infile:
- """ This is used to test connectivity to a host before attempting to connect """
- dev = Device(host=host.strip(), user=username, password=password) # change pass function
- try:
- dev.open()
- except (KeyboardInterrupt, SystemExit):
- raise
- except ConnectError as err:
- fail.write("Error: Cannot connect to device: {0} ".format(err) + '\n')
- f.write("Error: Cannot connect to device: {0} ".format(err) + '\n\n')
- yellow("Error: Cannot connect to device: {0} ".format(err))
- continue
- except Exception as err:
- print err
- f.write("Error: Cannot connect to device: {0} ".format(err) + '\n')
- continue
- update_config(host)
- f.close()
- fail.close()
- if os.path.getsize(fail_log) == 0:
- os.remove(fail_log)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement