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 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 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 __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 testGetDict(self): host = Host('foo') host.set_address('foo2') self.assertEqual(host.get_dict(), { 'hostname': 'foo', 'address': 'foo2', 'protocol': 'telnet', 'port': 23 })
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
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)
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 setUp(self): self.host = Host('localhost') self.host.set_all(dict(testarg=1))
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)
# 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()
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')