class TemplateTest(unittest.TestCase): def setUp(self): account = Account('sab', '') self.queue = Queue(verbose = 0, max_threads = 1) self.logger = Logger() self.queue.add_account(account) def tearDown(self): self.queue.destroy() def testTemplates(self): callback = bind(log_to(self.logger)(dummy_cb), self) for test in os.listdir(test_dir): pseudo = os.path.join(test_dir, test, 'pseudodev.py') if os.path.exists(pseudo): self.queue.run('pseudo://' + pseudo, callback) else: self.queue.run('ios://' + test, callback) self.queue.shutdown() # Unfortunately, unittest.TestCase does not fail if self.assert() # was called from a subthread, so this is our workaround... failed = self.logger.get_aborted_logs() report = format(self.logger, show_successful = False) self.assert_(not failed, report)
class TemplateTest(unittest.TestCase): def setUp(self): account = Account('sab', '') self.queue = Queue(verbose=0, max_threads=1) self.logger = Logger() self.queue.add_account(account) def tearDown(self): self.queue.destroy() def testTemplates(self): callback = bind(log_to(self.logger)(dummy_cb), self) for test in os.listdir(test_dir): pseudo = os.path.join(test_dir, test, 'pseudodev.py') if os.path.exists(pseudo): self.queue.run('pseudo://' + pseudo, callback) else: self.queue.run('ios://' + test, callback) self.queue.shutdown() # Unfortunately, unittest.TestCase does not fail if self.assert() # was called from a subthread, so this is our workaround... failed = self.logger.get_aborted_logs() report = format(self.logger, show_successful=False) self.assert_(not failed, report)
def setUp(self): account = Account('sab', '') self.queue = Queue(verbose = 0, max_threads = 1) self.logger = Logger() self.queue.add_account(account)
def setUp(self): self.logger = Logger() self.n_actions = 0
class reportTest(unittest.TestCase): CORRELATE = Exscript.util.report def setUp(self): self.logger = Logger() self.n_actions = 0 def createLog(self): self.n_actions += 1 name = 'fake' + str(self.n_actions) job = FakeJob(name) self.logger.add_log(id(job), job.name, 1) self.logger.log(id(job), 'hello world') return job def createAbortedLog(self): job = self.createLog() try: raise FakeError() except Exception: thetype, exc, tb = sys.exc_info() tb = ''.join(traceback.format_exception(thetype, exc, tb)) self.logger.log_aborted(id(job), (thetype, exc, tb)) return job def createSucceededLog(self): job = self.createLog() self.logger.log_succeeded(id(job)) return job def testStatus(self): from Exscript.util.report import status self.createSucceededLog() expect = 'One action done (succeeded)' self.assertEqual(status(self.logger), expect) self.createSucceededLog() expect = '2 actions total (all succeeded)' self.assertEqual(status(self.logger), expect) self.createAbortedLog() expect = '3 actions total (1 failed, 2 succeeded)' self.assertEqual(status(self.logger), expect) def testSummarize(self): from Exscript.util.report import summarize self.createSucceededLog() self.createAbortedLog() expected = 'fake1: ok\nfake2: FakeError' self.assertEqual(summarize(self.logger), expected) def testFormat(self): from Exscript.util.report import format self.createSucceededLog() self.createAbortedLog() self.createSucceededLog() file = os.path.splitext(__file__)[0] expected = ''' Failed actions: --------------- fake2: Traceback (most recent call last): File "%s.py", line 54, in createAbortedLog raise FakeError() FakeError Successful actions: ------------------- fake1 fake3'''.strip() % file expected_py3 = expected.replace('FakeError\n', 'util.reportTest.FakeError\n') if sys.version_info[0] < 3: self.assertEqual(format(self.logger), expected) else: self.assertEqual(format(self.logger), expected_py3)
def setUp(self): account = Account('sab', '') self.queue = Queue(verbose=0, max_threads=1) self.logger = Logger() self.queue.add_account(account)
# Send command to router to retrieve second part of VRF configuration socket.execute("show running-config | section SMVPN "+routeDistinguisher+" ") outputFile.write(socket.response) # Write contents of running config to output file socket.send("exit\r") # Send the "exit" command to log out of router gracefully socket.close() # Close SSH connection # Exception: outputFile file could not be opened except IOError: print print "--> An error occurred opening "+outputFile+"." print "--> "+vrfName+" backed up to "+outputFilename+"." logger = Logger() # Log stuff @log_to(logger) # Logging decorator; Must precede buildIndex! @autologin() # Exscript login decorator; Must precede buildIndex! def buildIndex(job, host, socket): # This function builds the index file by connecting to the router and extracting all # matching sections. I chose to search for "crypto keyring" because it is the only # portion of a VPN config that contains the VRF name AND Peer IP. Caveat is that # the program temporarily captures the pre-shared key. "crypto isakmp profile" was not # a suitable query due to the possibility of multiple "match identity address" statements stdout.write(".") # Write period without trailing newline socket.execute("terminal length 0") # Disable user-prompt to page through config # Exscript doesn't always recognize Cisco IOS # for socket.autoinit() to work correctly # Send command to router to capture results
#!/usr/bin/env python 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)
#!/usr/bin/env python from Exscript import Queue, Logger 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 @autologin def do_something(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. Using "logdir = ..." is equivalent to the following: # logger = FileLogger(queue, 'my/logdir') # It instructs the queue to automatically log everything to the filesystem; # one file is created per host. queue = Queue(verbose = 0, max_threads = 5, logdir = 'my/logdir/') logger = Logger(queue) # Logs everything to memory. 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)