def testAcquireAccount(self): self.testAddAccount() self.accm.acquire_account(self.account1) self.account1.release() self.accm.acquire_account(self.account1) self.account1.release() # Add three more accounts. filename = os.path.join(os.path.dirname(__file__), 'account_pool.cfg') self.accm.add_account(get_accounts_from_file(filename)) self.assertEqual(self.accm.n_accounts(), 5) for i in range(0, 2000): # Each time an account is acquired a different one should be # returned. acquired = {} for n in range(0, 5): account = self.accm.acquire_account() self.assertTrue(account is not None) self.assertNotIn(account.get_name(), acquired) acquired[account.get_name()] = account # Release one account. acquired['abc'].release() # Acquire one account. account = self.accm.acquire_account() self.assertEqual(account.get_name(), 'abc') # Release all accounts. for account in list(acquired.values()): account.release()
def testAcquireAccount(self): self.testAddAccount() self.accm.acquire_account(self.account1) self.account1.release() self.accm.acquire_account(self.account1) self.account1.release() # Add three more accounts. filename = os.path.join(os.path.dirname(__file__), 'account_pool.cfg') self.accm.add_account(get_accounts_from_file(filename)) self.assertEqual(self.accm.n_accounts(), 5) for i in range(0, 2000): # Each time an account is acquired a different one should be # returned. acquired = {} for n in range(0, 5): account = self.accm.acquire_account() self.assertTrue(account is not None) self.assertNotIn(account.get_name(), acquired) acquired[account.get_name()] = account # Release one account. acquired['abc'].release() # Acquire one account. account = self.accm.acquire_account() self.assertEqual(account.get_name(), 'abc') # Release all accounts. for account in list(acquired.values()): account.release()
def add_account_pool_from_file(self, name, filename): # Remove the pool if it exists. xml = self.cfgtree.getroot() pool_elem = xml.find('account-pool[@name="%s"]' % name) if pool_elem is not None: xml.remove(pool_elem) # Import the new pool from the given file. pool_elem = etree.SubElement(xml, 'account-pool', name=name) accounts = get_accounts_from_file(filename) add_accounts_to_etree(pool_elem, accounts) self.save()
def add_account_pool_from_file(self, name, filename): # Remove the pool if it exists. xml = self.cfgtree.getroot() pool_elem = xml.find('account-pool[@name="%s"]' % name) if pool_elem is not None: xml.remove(pool_elem) # Import the new pool from the given file. pool_elem = etree.SubElement(xml, 'account-pool', name = name) accounts = get_accounts_from_file(filename) add_accounts_to_etree(pool_elem, accounts) self.save()
def __init__(self, io): self.hosts = [] self.accounts = [] self.io = io self.hosts = get_hosts_from_file(self.io.rootDir + "/" + "hosts") logging.info("read in hosts" + self.io.rootDir + "/" + "hosts") if not self.hosts: logging.info("EMPTY HOST FILE, exit ...") exit(1) for h in self.hosts: logging.info("host:" + h.get_address()) self.accounts = get_accounts_from_file(io.rootDir + "/" + "accounts") logging.info("read in accounts" + self.io.rootDir + "/" + "accounts") if not self.accounts: logging.info("EMPTY ACCOUNT FILE, exit ...") exit(1) for a in self.accounts: logging.info("account:" + a.get_name())
def testGetAccountsFromFile(self): from Exscript.util.file import get_accounts_from_file accounts = get_accounts_from_file(self.account_file.name) result = [(a.get_name(), a.get_password()) for a in accounts] result.sort() self.assertEqual(account_pool, result)
def testGetAccountsFromFile(self): from Exscript.util.file import get_accounts_from_file accounts = get_accounts_from_file(self.account_file.name) result = [(a.get_name(), a.get_password()) for a in accounts] result.sort() self.assertEqual(account_pool, result)
from Exscript.util.start import quickstart from Exscript.util.file import get_hosts_from_file, get_accounts_from_file from Exscript import Account command = input("enter command: ") def do_something(job, host, conn): conn.execute('term len 0') conn.execute(command) accounts = get_accounts_from_file('accounts.cfg') hosts = get_hosts_from_file('hostlist.txt', default_protocol='ssh2') quickstart(hosts, do_something, max_threads=15, verbose=1)
import Exscript.util.file as euf import Exscript.util.start as eus import Exscript.util.match as eum import pysvn import os import time import datetime hosts = euf.get_hosts_from_file('c:\\network_configs\\hosts.txt') accounts = euf.get_accounts_from_file('c:\\network_configs\\accounts.cfg') def dump_config(job, host, conn): """Connect to device, trim config file a bit, write to file""" conn.execute('term len 0') conn.execute('show run') #get the actual hostname of the device hostname = eum.first_match(conn, r'^hostname\s(.+)$') cfg_file = 'c:\\network_configs\\' + hostname.strip() + '.cfg' config = conn.response.splitlines() # a little cleanup for i in range(3): config.pop(i) config.pop(-0) config.pop(-1) # write config to file with open(cfg_file, 'w') as f: for line in config: f.write(line +'\n') eus.start(accounts, hosts, dump_config, max_threads=2)
#!/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)
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()
raise Exception("unsupported os: " + repr(conn.guess_os())) # autoinit() automatically executes commands to make the remote # system behave more script-friendly. The specific commands depend # on the detected operating system, i.e. on what guess_os() returns. conn.autoinit() # Execute a simple command. conn.execute("show ip int brie") print "myvariable is", conn.get_host().get("myvariable") def two(job, host, conn): conn.autoinit() conn.execute("show interface POS1/0") accounts = get_accounts_from_file("accounts.cfg") # Start on one host. host = Host("localhost") host.set("myvariable", "foobar") start(accounts, host, one) # Start on many hosts. In this case, the accounts from accounts.cfg # are only used if the host url does not contain a username and password. # The "max_threads" keyword indicates the maximum number of concurrent # connections. hosts = get_hosts_from_file("hostlist.txt") start(accounts, hosts, two, max_threads=2)