Beispiel #1
0
    def _init_queue_from_name(self, name):
        # Create the queue first.
        element = self.cfgtree.find('queue[@name="%s"]' % name)
        max_threads = element.find('max-threads').text
        queue = Queue(verbose=0, max_threads=max_threads)

        # Assign account pools to the queue.
        def match_cb(condition, host):
            return eval(condition, host.get_dict())

        for pool_elem in element.iterfind('account-pool'):
            pname = pool_elem.text
            pool = self._init_account_pool_from_name(pname)
            cond = pool_elem.get('for')
            if cond is None:
                print 'Assigning default account pool "%s" to "%s"...' % (
                    pname, name)
                queue.add_account_pool(pool)
                continue

            print 'Assigning account pool "%s" to "%s"...' % (pname, name)
            condition = compile(cond, 'config', 'eval')
            queue.add_account_pool(pool, partial(match_cb, condition))

        return queue
Beispiel #2
0
def run(users, hosts, func, **kwargs):
    """
    Convenience function that creates an Exscript.Queue instance, adds
    the given accounts, and calls Queue.run() with the given
    hosts and function as an argument.

    If you also want to pass arguments to the given function, you may use
    util.decorator.bind() like this::

      def my_callback(job, host, conn, my_arg, **kwargs):
          print my_arg, kwargs.get('foo')

      run(account,
          host,
          bind(my_callback, 'hello', foo = 'world'),
          max_threads = 10)

    @type  users: Account|list[Account]
    @param users: The account(s) to use for logging in.
    @type  hosts: Host|list[Host]
    @param hosts: A list of Host objects.
    @type  func: function
    @param func: The callback function.
    @type  kwargs: dict
    @param kwargs: Passed to the Exscript.Queue constructor.
    """
    queue = Queue(**kwargs)
    queue.add_account(users)
    queue.run(hosts, func)
    queue.destroy()
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+"."
Beispiel #4
0
 def createQueue(self, logdir=None, **kwargs):
     if self.queue:
         self.queue.destroy()
     self.out = self.manager.Log()
     self.err = self.manager.Log()
     self.queue = Queue(mode=self.mode,
                        stdout=self.out,
                        stderr=self.err,
                        **kwargs)
     self.accm = self.queue.account_manager
     if logdir is not None:
         self.logger = FileLogger(logdir)
Beispiel #5
0
    def _init_queue_from_name(self, name):
        # Create the queue first.
        element = self.cfgtree.find('queue[@name="%s"]' % name)
        max_threads = element.find('max-threads').text
        queue = Queue(verbose=0, max_threads=max_threads)

        # Assign account pools to the queue.
        for pool_elem in element.iterfind('account-pool'):
            pool = self._init_account_pool_from_name(pool_elem.text)
            cond = pool_elem.get('for')
            if cond is None:
                queue.add_account_pool(pool)
                continue

            condition = compile(cond, 'config', 'eval')

            def match_cb(host):
                return eval(condition, host.get_dict())

            queue.add_account_pool(pool, match_cb)

        return queue
Beispiel #6
0
 def setUp(self):
     account = Account('sab', '')
     self.queue = Queue(verbose=0, max_threads=1)
     self.logger = Logger()
     self.queue.add_account(account)
Beispiel #7
0
import time, Exscript.util.sigintcatcher
from Exscript import Queue, Account
from Exscript.util.decorator import bind
from TkExscript import QueueWindow


def do_something(conn, wait):
    conn.connect()
    conn.authenticate()
    for i in range(100):
        conn.execute('test%d' % i)
        time.sleep(wait)
    conn.close()


queue = Queue(max_threads=4, verbose=0)
queue.add_account(Account('test', 'test'))
window = QueueWindow(queue)
queue.run('dummy://dummy1', bind(do_something, .02))
queue.run('dummy://dummy2', bind(do_something, .2))
queue.run('dummy://dummy3', bind(do_something, .3))
window.mainloop()
queue.shutdown()
Beispiel #8
0
    elif task == 'NO_DIVERT' and valid_ipv4(ipaddr):
        conn.execute('term len 0')
        conn.execute('conf t')
        conn.execute('ip access-list extended NORMAL')
        conn.execute('no permit ip any any')
        conn.execute(f"no deny ip {ipaddr} {wildcard}")
        conn.execute(f"permit ip any any")
        
        conn.execute('ip access-list extended DIVERT')
        conn.execute('no deny ip any any')
        conn.execute(f"no permit ip {ipaddr} {wildcard}")
        conn.execute('deny ip any any')
        conn.execute('end')
        conn.execute('clear ip bgp * soft out')
        
    else:
        RED = '\033[31m'
        print(RED + "\nWARNING: \tPLEASE INPUT A CORRECT TASK AND IP ADDRESS!")
        print(f"NOTE:\t\tNO CHANGES HAS BEEN MADE!")
        print(Style.RESET_ALL)
        

# Read input data.
accounts = get_accounts_from_file('/root/crash_course/project/accounts.cfg')
hosts = get_hosts_from_file('/root/crash_course/project/hostlist.txt',default_protocol='ssh2')

queue = Queue(max_threads=5)
queue.add_account(accounts)
queue.run(hosts, do_something,)
queue.shutdown()                
Beispiel #9
0
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)
Beispiel #10
0
from Exscript.util.decorator import bind
from Exscript import Queue, Account, Host

objnames = ('count_calls', )
follow_modules = False


def count_calls(conn, thedata, **kwargs):
    thedata['n_calls'] += 1


def foobar():
    pass


queue = Queue()
data = {'n_calls': 0}
func = bind(count_calls, data)
task = queue.run(['t1', 't2', 't3', 't4', 't5', 't6'], func)
task.wait()
queue.shutdown()
queue.destroy()

del func

# Test memory consumption.
from meliae import scanner
gc.collect()
scanner.dump_all_objects("test.dump")
from meliae import loader
om = loader.load('test.dump')
Beispiel #11
0
#!/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)