def routerLogin(): # This function prompts the user to provide their login credentials and logs into each # of the routers before calling the buildIndex function to extract relevant portions of # the router config. As designed, this function actually has the capability to login to # multiple routers simultaneously. I chose to not allow it to multi-thread given possibility # of undesirable results from multiple threads writing to the same index file simultaneously try:# Check for existence of routerFile; If exists, continue with program with open(routerFile, "r"): pass # Read hosts from specified file & remove duplicate entries, set protocol to SSH2 hosts = get_hosts_from_file(routerFile,default_protocol="ssh2",remove_duplicates=True) if username == "": # If username is blank print account = read_login() # Prompt the user for login credentials elif password == "": # If password is blank print account = read_login() # Prompt the user for login credentials else: # Else use username/password from configFile account = Account(name=username, password=b64decode(password)) # Minimal message from queue, 1 threads, redirect errors to null queue = Queue(verbose=0, max_threads=1, stderr=(open(os.devnull, "w"))) queue.add_account(account) # Use supplied user credentials print stdout.write("--> Building index...") # Print without trailing newline queue.run(hosts, buildIndex) # Create queue using provided hosts queue.shutdown() # End all running threads and close queue # If logFileDirectory does not exist, create it. if not path.exists(logFileDirectory): makedirs(logFileDirectory) # Define log filename based on date logFilename = logFileDirectory+"VRFSearchAndBackup_"+date+".log" # Check to see if logFilename currently exists. If it does, append an # integer onto the end of the filename until logFilename no longer exists incrementLogFilename = 1 while fileExist(logFilename): logFilename = logFileDirectory+"VRFSearchAndBackup_"+date+"_"+str(incrementLogFilename)+".log" incrementLogFilename = incrementLogFilename + 1 # Write log results to logFile with open(logFilename, "w") as outputLogFile: try: outputLogFile.write(summarize(logger)) # Exception: router file was not able to be opened except IOError: print print "--> An error occurred opening "+logFileDirectory+logFile+"." # Exception: router file could not be opened except IOError: print print "--> An error occurred opening "+routerFile+"."
f.write(description + "\t\t\t" + intf + "\t\t\t" + "Serial: " + serial + "\n") f.close() # Read input data. #accounts = get_accounts_from_file('accounts.cfg') accounts = read_login() try: hosts = get_hosts_from_file('hostlist.txt') except: print """ Devi creare un file che si chiama hostlist.txt e dentro ci sono linee di questo tipo: ssh://r.rm2.garr.net """ sys.exit(0) # Run do_something on each of the hosts. The given accounts are used # round-robin. "verbose = 0" instructs the queue to not generate any # output on stdout. queue = Queue(verbose = 3, max_threads = 5) queue.add_account(accounts) # Adds one or more accounts. queue.run(hosts, do_something) # Asynchronously enqueues all hosts. queue.shutdown() # Waits until all hosts are completed. # Print a short report. print status(logger) print summarize(logger)
def testSummarize(self): from Exscript.util.report import summarize self.createSucceededLog() self.createAbortedLog() expected = 'fake1: ok\nfake2: FakeError' self.assertEqual(summarize(self.logger), expected)
if not path.exists(logFileDirectory): makedirs(logFileDirectory) # Define log filename based on date logFilename = logFileDirectory+"DownloadRouterConfig_"+date+".log" # Check to see if logFilename currently exists. If it does, append an # integer onto the end of the filename until logFilename no longer exists incrementLogFilename = 1 while fileExist(logFilename): logFilename = logFileDirectory+"DownloadRouterConfig_"+date+"_"+str(incrementLogFilename)+".log" incrementLogFilename = incrementLogFilename + 1 # Write log results to logFile with open(logFilename, "w") as outputLogFile: try: outputLogFile.write(summarize(logger)) # Exception: router file was not able to be opened except IOError: print print "--> An error occurred opening "+logFileDirectory+logFile+"." else: # if fileExist(routerFile): # Else if no routerFile exists, create a sample one and quit the program. try: with open (routerFile, "w") as exampleFile: print print "--> Router file not found; Creating "+routerFile+"." print " Edit this file and restart the application." exampleFile.write("## DownloadRouterConfig.py ROUTER FILE ##\n#\n") exampleFile.write("#Enter a list of hostnames or IP Addresses, one per line.\n#For example:\n")
from Exscript import Queue, Logger from Exscript.util.log import log_to from Exscript.util.decorator import autologin from Exscript.util.file import get_hosts_from_file, get_accounts_from_file from Exscript.util.report import status, summarize logger = Logger() # Logs everything to memory. @log_to(logger) @autologin() def do_something(job, host, conn): conn.execute('show ip int brie') # Read input data. accounts = get_accounts_from_file('accounts.cfg') hosts = get_hosts_from_file('hostlist.txt') # Run do_something on each of the hosts. The given accounts are used # round-robin. "verbose=0" instructs the queue to not generate any # output on stdout. queue = Queue(verbose=5, max_threads=5) queue.add_account(accounts) # Adds one or more accounts. queue.run(hosts, do_something) # Asynchronously enqueues all hosts. queue.shutdown() # Waits until all hosts are completed. # Print a short report. print status(logger) print summarize(logger)