def testGetDict(self): host = Host('foo') host.set_address('foo2') self.assertEqual(host.get_dict(), {'hostname': 'foo', 'address': 'foo2', 'protocol': 'telnet', 'port': 23})
def testGetAll(self): self.assertEqual(self.host.get_all(), {'testarg': 1}) self.testSetAll() self.assertEqual(self.host.get_all(), {'test1': 1, 'test2': 2}) host = Host('localhost') self.assertEqual(host.get_all(), {})
def testGetDict(self): host = Host('foo') host.set_address('foo2') self.assertEqual(host.get_dict(), { 'hostname': 'foo', 'address': 'foo2', 'protocol': 'telnet', 'port': 23 })
def __init__(self, name = 'fake'): self.function = lambda x: None self.name = name self.failures = 0 self.data = {'pipe': 0, 'stdout': sys.stdout, 'host': Host('foo')}
def run_command(rmq_channel, command, hosts, threads): """Execute command on remote host""" from Exscript import Account, Host from Exscript.util.start import start results = {} def do_something(thread, host, conn): #conn.execute('vtysh -c "%s"' % command) conn.execute(command) result = repr(conn.response) data = { host.address: { command: result }, } body = json.dumps(data) rmq_channel.basic_publish(exchange='measure', routing_key="result", body=body) #conn.execute("exit") accounts = [Account("root", password="******")] hosts = [Host(h, default_protocol="ssh") for h in hosts] #TODO: open multiple ssh sessions start(accounts, hosts, do_something, max_threads=threads) return results
def handle(self, *args, **options): if options['all'] and options['device_id']: print ("Error: --all and --deviceid detected. Use only one") return if options['all']: devices = Device.objects.all() if options['device_id']: try: devices = [ Device.objects.get(pk=int(options['device_id'])) ] except: raise CommandError('Device ID "%s" does not exist' % options['device_id']) for device in devices: found_credential = False for credential in Credential.objects.all(): if credential.match_ip(device.access_ip): found_credential = True host = Host(device.protocol + device.access_ip) account = (Account(credential.username, credential.password)) if credential.enable_username and credential.enable_password: account.set_authorization_password(credential.enable_password) host.set_account(account) host.set('device', device) hosts.append(host) break if not found_credential: device.backup_status = "No credentials found." % device device.save() queue = Queue(max_threads = 5, verbose = -1, stderr=open(os.devnull, 'w')) queue.run(hosts, do_backup) queue.shutdown() for log in logger.get_logs(): device = Device.objects.get(access_ip=log.get_name()) if log.has_error(): device.backup_status = log.get_error(False) else: device.backup_status = 'ok' device.backup_last_ran = now() device.save()
def testConstructor(self): host = Host('localhost') self.assertEqual(host.get_protocol(), 'telnet') host = Host('localhost', default_protocol = 'foo') self.assertEqual(host.get_protocol(), 'foo') for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_name(), uri.hostname) self.assertEqual(host.get_address(), uri.hostname) self.assertEqual(host.get_uri(), str(uri))
def testToHosts(self): from Exscript.util.cast import to_hosts self.assertRaises(TypeError, to_hosts, None) result = to_hosts([]) self.assertIsInstance(result, list) self.assertEqual(len(result), 0) result = to_hosts('localhost') self.assertIsInstance(result, list) self.assertEqual(len(result), 1) self.assertIsInstance(result[0], Host) result = to_hosts(Host('localhost')) self.assertIsInstance(result, list) self.assertEqual(len(result), 1) self.assertIsInstance(result[0], Host) hosts = ['localhost', Host('1.2.3.4')] result = to_hosts(hosts) self.assertIsInstance(result, list) self.assertEqual(len(result), 2) self.assertIsInstance(result[0], Host) self.assertIsInstance(result[1], Host)
def testToHost(self): from Exscript.util.cast import to_host self.assertIsInstance(to_host('localhost'), Host) self.assertIsInstance(to_host(Host('localhost')), Host) self.assertRaises(TypeError, to_host, None)
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)
def testGetUri(self): for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_uri().split('&').sort(), str(uri).split('&').sort())
def testConstructor(self): host = Host('localhost') self.assertEqual(host.get_protocol(), 'telnet') host = Host('localhost', default_protocol='foo') self.assertEqual(host.get_protocol(), 'foo') for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_name(), uri.hostname) self.assertEqual(host.get_address(), uri.hostname) self.assertEqual(host.get_uri().split('&').sort(), str(uri).split('&').sort())
def setUp(self): self.host = Host('localhost') self.host.set_all(dict(testarg=1))
from Exscript import Host basedir = os.path.dirname(os.path.dirname(__file__)) prov_dir = os.path.join(basedir, 'providers') xsd_file = os.path.join(basedir, 'xsl', 'model.xsd') xslt_file = os.path.join(prov_dir, 'ios', 'xsl', 'generic.xsl') xml = etree.parse(sys.argv[1]) proc_xml = xml.find('processor[@type="xslt"]') fs_dir = os.path.expandvars(xml.findtext('file-store/basedir')) proc_xml.find('xsl-dir').text = basedir print 'Assuming file store is in', fs_dir class FakeProvider(object): store = FileStore(fs_dir) prov = FakeProvider() p = XsltProcessor(basedir, proc_xml) p.debug = True host = Host('m4docirom1ea.do-a-11.de.ipmb.dtag.de') host.set('path', 'ipmb/pe/m4docirom1ea.do-a-11.de.ipmb.dtag.de') host.set('country', 'mycountry') host.set('city', 'mycity') p.start(prov, host, object, xslt=xslt_file, xsd=xsd_file, input='show_version.xml', output='not-used.xml')
class HostTest(unittest.TestCase): CORRELATE = Host def setUp(self): self.host = Host('localhost') self.host.set_all(dict(testarg=1)) def testConstructor(self): host = Host('localhost') self.assertEqual(host.get_protocol(), 'telnet') host = Host('localhost', default_protocol='foo') self.assertEqual(host.get_protocol(), 'foo') for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_name(), uri.hostname) self.assertEqual(host.get_address(), uri.hostname) self.assertEqual(host.get_uri().split('&').sort(), str(uri).split('&').sort()) def testSetUri(self): for url, result in urls: self.host.set_uri(url) uri = Url.from_string(url) self.assertEqual(self.host.get_name(), uri.hostname) self.assertEqual(self.host.get_address(), uri.hostname) def testGetUri(self): for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_uri().split('&').sort(), str(uri).split('&').sort()) def testGetDict(self): host = Host('foo') host.set_address('foo2') self.assertEqual(host.get_dict(), { 'hostname': 'foo', 'address': 'foo2', 'protocol': 'telnet', 'port': 23 }) def testSetAddress(self): self.host.set_protocol('dummy') self.host.set_address('test.org') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'localhost') self.assertEqual(self.host.get_address(), 'test.org') self.host.set_address('001.002.003.004') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'localhost') self.assertEqual(self.host.get_address(), '1.2.3.4') def testGetAddress(self): self.assertEqual(self.host.get_address(), 'localhost') # Additional tests are in testSetAddress(). def testSetName(self): self.assertEqual(self.host.get_name(), 'localhost') self.host.set_protocol('dummy') self.host.set_name('test.org') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'test.org') self.assertEqual(self.host.get_address(), 'localhost') self.host.set_name('testhost') self.assertEqual(self.host.get_name(), 'testhost') def testGetName(self): pass # Tested in testSetName(). def testSetProtocol(self): self.assertEqual(self.host.get_protocol(), 'telnet') self.host.set_protocol('dummy') self.assertEqual(self.host.get_protocol(), 'dummy') def testGetProtocol(self): pass # Tested in testSetProtocol(). def testSetOption(self): self.assertRaises(TypeError, self.host.set_option, 'test', True) self.assertEqual(self.host.get_options(), {}) self.assertEqual(self.host.get_option('verify_fingerprint'), None) self.assertEqual(self.host.get_option('verify_fingerprint', False), False) self.host.set_option('verify_fingerprint', True) self.assertEqual(self.host.get_option('verify_fingerprint'), True) self.assertEqual(self.host.get_options(), {'verify_fingerprint': True}) def testGetOption(self): pass # Tested in testSetOption(). def testGetOptions(self): pass # Tested in testSetOption(). def testSetTcpPort(self): self.assertEqual(self.host.get_tcp_port(), 23) self.host.set_protocol('ssh') self.assertEqual(self.host.get_tcp_port(), 23) self.host.set_tcp_port(123) self.assertEqual(self.host.get_tcp_port(), 123) def testGetTcpPort(self): pass # Tested in testSetTcpPort(). def testSetAccount(self): account = Account('test') self.assertEqual(self.host.get_account(), None) self.host.set_account(account) self.assertEqual(self.host.get_account(), account) def testGetAccount(self): pass # Tested in testSetAccount(). def testSet(self): self.assertEqual(self.host.get('test'), None) self.host.set('test', 3) self.assertEqual(self.host.get('test'), 3) def testSetAll(self): self.testSet() self.host.set_all({'test1': 1, 'test2': 2}) self.assertEqual(self.host.get('test'), None) self.assertEqual(self.host.get('test1'), 1) self.assertEqual(self.host.get('test2'), 2) def testGetAll(self): self.assertEqual(self.host.get_all(), {'testarg': 1}) self.testSetAll() self.assertEqual(self.host.get_all(), {'test1': 1, 'test2': 2}) host = Host('localhost') self.assertEqual(host.get_all(), {}) def testAppend(self): self.assertEqual(self.host.get('test'), None) self.host.append('test', 3) self.assertEqual(self.host.get('test'), [3]) self.host.append('test', 4) self.assertEqual(self.host.get('test'), [3, 4]) def testSetDefault(self): self.testSet() self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), None) self.host.set_default('test', 5) self.host.set_default('test2', 1) self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), 1) def testHasKey(self): self.testSet() self.assertTrue(self.host.has_key('test')) self.assertFalse(self.host.has_key('test2')) def testGet(self): self.testSet() self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), None) self.assertEqual(self.host.get('test', 1), 3) self.assertEqual(self.host.get('test2', 1), 1)
class HostTest(unittest.TestCase): CORRELATE = Host def setUp(self): self.host = Host('localhost') self.host.set_all(dict(testarg = 1)) def testConstructor(self): host = Host('localhost') self.assertEqual(host.get_protocol(), 'telnet') host = Host('localhost', default_protocol = 'foo') self.assertEqual(host.get_protocol(), 'foo') for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_name(), uri.hostname) self.assertEqual(host.get_address(), uri.hostname) self.assertEqual(host.get_uri(), str(uri)) def testSetUri(self): for url, result in urls: self.host.set_uri(url) uri = Url.from_string(url) self.assertEqual(self.host.get_name(), uri.hostname) self.assertEqual(self.host.get_address(), uri.hostname) def testGetUri(self): for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_uri(), str(uri)) def testGetDict(self): host = Host('foo') host.set_address('foo2') self.assertEqual(host.get_dict(), {'hostname': 'foo', 'address': 'foo2', 'protocol': 'telnet', 'port': 23}) def testSetAddress(self): self.host.set_protocol('dummy') self.host.set_address('test.org') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'localhost') self.assertEqual(self.host.get_address(), 'test.org') self.host.set_address('001.002.003.004') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'localhost') self.assertEqual(self.host.get_address(), '1.2.3.4') def testGetAddress(self): self.assertEqual(self.host.get_address(), 'localhost') # Additional tests are in testSetAddress(). def testSetName(self): self.assertEqual(self.host.get_name(), 'localhost') self.host.set_protocol('dummy') self.host.set_name('test.org') self.assertEqual(self.host.get_protocol(), 'dummy') self.assertEqual(self.host.get_name(), 'test.org') self.assertEqual(self.host.get_address(), 'localhost') self.host.set_name('testhost') self.assertEqual(self.host.get_name(), 'testhost') def testGetName(self): pass # Tested in testSetName(). def testSetProtocol(self): self.assertEqual(self.host.get_protocol(), 'telnet') self.host.set_protocol('dummy') self.assertEqual(self.host.get_protocol(), 'dummy') def testGetProtocol(self): pass # Tested in testSetProtocol(). def testSetOption(self): self.assertRaises(TypeError, self.host.set_option, 'test', True) self.assertEqual(self.host.get_options(), {}) self.assertEqual(self.host.get_option('verify_fingerprint'), None) self.assertEqual(self.host.get_option('verify_fingerprint', False), False) self.host.set_option('verify_fingerprint', True) self.assertEqual(self.host.get_option('verify_fingerprint'), True) self.assertEqual(self.host.get_options(), {'verify_fingerprint': True}) def testGetOption(self): pass # Tested in testSetOption(). def testGetOptions(self): pass # Tested in testSetOption(). def testSetTcpPort(self): self.assertEqual(self.host.get_tcp_port(), 23) self.host.set_protocol('ssh') self.assertEqual(self.host.get_tcp_port(), 23) self.host.set_tcp_port(123) self.assertEqual(self.host.get_tcp_port(), 123) def testGetTcpPort(self): pass # Tested in testSetTcpPort(). def testSetAccount(self): account = Account('test') self.assertEqual(self.host.get_account(), None) self.host.set_account(account) self.assertEqual(self.host.get_account(), account) def testGetAccount(self): pass # Tested in testSetAccount(). def testSet(self): self.assertEqual(self.host.get('test'), None) self.host.set('test', 3) self.assertEqual(self.host.get('test'), 3) def testSetAll(self): self.testSet() self.host.set_all({'test1': 1, 'test2': 2}) self.assertEqual(self.host.get('test'), None) self.assertEqual(self.host.get('test1'), 1) self.assertEqual(self.host.get('test2'), 2) def testGetAll(self): self.assertEqual(self.host.get_all(), {'testarg': 1}) self.testSetAll() self.assertEqual(self.host.get_all(), {'test1': 1, 'test2': 2}) host = Host('localhost') self.assertEqual(host.get_all(), {}) def testAppend(self): self.assertEqual(self.host.get('test'), None) self.host.append('test', 3) self.assertEqual(self.host.get('test'), [3]) self.host.append('test', 4) self.assertEqual(self.host.get('test'), [3, 4]) def testSetDefault(self): self.testSet() self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), None) self.host.set_default('test', 5) self.host.set_default('test2', 1) self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), 1) def testHasKey(self): self.testSet() self.assert_(self.host.has_key('test')) self.failIf(self.host.has_key('test2')) def testGet(self): self.testSet() self.assertEqual(self.host.get('test'), 3) self.assertEqual(self.host.get('test2'), None) self.assertEqual(self.host.get('test', 1), 3) self.assertEqual(self.host.get('test2', 1), 1)
# This script is not meant to provide a fully automated test, it's # merely a hack/starting point for investigating memory consumption # manually. The behavior also depends heavily on the version of meliae. from meliae import scanner, loader from Exscript import Account, Host hostlist = [Host(str(i)) for i in range(1, 10000)] #accountlist = [Account(str(i)) for i in range(1, 10000)] scanner.dump_all_objects('test.dump') om = loader.load('test.dump') print om.summarize()
def testGetUri(self): for url, result in urls: host = Host(url) uri = Url.from_string(url) self.assertEqual(host.get_uri(), str(uri))
# You can add a safehold based on the guess_os() method. if conn.guess_os() != 'ios': 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, host1, 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)
def setUp(self): self.host = Host('localhost') self.host.set_all(dict(testarg = 1))
def __get_host_from_row(self, row): assert row is not None tbl_h = self._table_map['host'] host = Host(row[tbl_h.c.name]) host.set_address(row[tbl_h.c.address]) host.set_protocol(row[tbl_h.c.protocol]) host.set_tcp_port(row[tbl_h.c.tcp_port]) host.set('path', row[tbl_h.c.path]) host.set('country', row[tbl_h.c.country]) host.set('city', row[tbl_h.c.city]) host.set('os', row[tbl_h.c.os]) host.set('duration', row[tbl_h.c.duration]) host.set('deleted', row[tbl_h.c.deleted]) return host
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(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, host1, 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)
import sys from FileStore import FileStore from Exscript import Host basedir = os.path.dirname(os.path.dirname(__file__)) prov_dir = os.path.join(basedir, 'providers') xsd_file = os.path.join(basedir, 'xsl', 'model.xsd') xslt_file = os.path.join(prov_dir, 'ios', 'xsl', 'generic.xsl') xml = etree.parse(sys.argv[1]) proc_xml = xml.find('processor[@type="xslt"]') fs_dir = os.path.expandvars(xml.findtext('file-store/basedir')) proc_xml.find('xsl-dir').text = basedir print 'Assuming file store is in', fs_dir class FakeProvider(object): store = FileStore(fs_dir) prov = FakeProvider() p = XsltProcessor(basedir, proc_xml) p.debug = True host = Host('m4docirom1ea.do-a-11.de.ipmb.dtag.de') host.set('path', 'ipmb/pe/m4docirom1ea.do-a-11.de.ipmb.dtag.de') host.set('country', 'mycountry') host.set('city', 'mycity') p.start(prov, host, object, xslt = xslt_file, xsd = xsd_file, input = 'show_version.xml', output = 'not-used.xml')