def testGetAccountFromHash(self): pool1 = AccountPool() acc1 = Account('user1') pool1.add_account(acc1) self.am.add_pool(pool1) acc2 = Account('user2') self.am.add_account(acc2) self.assertEqual(self.am.get_account_from_hash(acc1.__hash__()), acc1) self.assertEqual(self.am.get_account_from_hash(acc2.__hash__()), acc2)
def __init__(self, ip, user_id, password, version): self.os_version = version self.conn.connect(ip, port='2222') account = Account(user_id, password) self.conn.login(account) self.conn.set_prompt(self.conn.get_prompt()) self.conn.execute('set cli screen-length 0')
def testReleaseAccounts(self): account1 = Account('foo') pool = AccountPool() pool.add_account(account1) pool.acquire_account(account1, 'one') self.am.add_pool(pool, lambda x: None) account2 = Account('bar') self.am.add_account(account2) self.am.acquire_account(account2, 'two') self.assert_(account1 not in pool.unlocked_accounts) self.assert_(account2 not in self.am.default_pool.unlocked_accounts) self.am.release_accounts('two') self.assert_(account1 not in pool.unlocked_accounts) self.assert_(account2 in self.am.default_pool.unlocked_accounts) self.am.release_accounts('one') self.assert_(account1 in pool.unlocked_accounts) self.assert_(account2 in self.am.default_pool.unlocked_accounts)
def set_uri(self, uri): """ Defines the protocol, hostname/address, TCP port number, username, and password from the given URL. The hostname may be URL formatted, so the following formats are all valid: - myhostname - myhostname.domain - ssh:hostname - ssh:hostname.domain - ssh://hostname - ssh://user@hostname - ssh://user:password@hostname - ssh://user:password@hostname:21 For a list of supported protocols please see set_protocol(). @type uri: string @param uri: An URL formatted hostname. """ try: uri = Url.from_string(uri, self.protocol) except ValueError: raise ValueError('Hostname parse error: ' + repr(uri)) hostname = uri.hostname or '' name = uri.path and hostname + uri.path or hostname self.set_protocol(uri.protocol) self.set_tcp_port(uri.port) self.set_name(name) self.set_address(name) if uri.username is not None \ or uri.password1 is not None \ or uri.password2: account = Account(uri.username, uri.password1, uri.password2) self.set_account(account) for key, val in uri.vars.iteritems(): self.set(key, val)
def get_configs_with_password(proto, host, username, password, enablePassword, timeout, confs): account = Account(name=username, password=password, password2=enablePassword) if proto == "ssh": configs = get_configs("ssh", account, host, timeout) elif proto == "telnet": configs = get_configs("telnet", account, host, timeout) if configs['error']: print "Error retrieving information from the router. Please check credentials and connectivity." sys.exit(-1) if confs == 'version': for line in configs['version']: print line elif confs == 'startup-config': for line in configs['startup-config']: print line elif confs == 'running-config': for line in configs['running-config']: print line elif confs == 'all-config': for line in configs['startup-config']: print line print "*"*40 for line in configs['running-config']: print line elif confs == 'all': for line in configs['version']: print line print "*"*40 for line in configs['startup-config']: print line print "*"*40 for line in configs['running-config']: print line else: for line in configs['running-config']: print line
def testAddPool(self): self.assertEqual(0, self.am.default_pool.n_accounts()) account = Account('user', 'test') self.am.add_account(account) self.assertEqual(1, self.am.default_pool.n_accounts()) def match_cb(host): self.data['match-called'] = True self.data['host'] = host return True # Replace the default pool. pool1 = AccountPool() self.am.add_pool(pool1) self.assertEqual(self.am.default_pool, pool1) # Add another pool, making sure that it does not replace # the default pool. pool2 = AccountPool() pool2.add_account(self.account) self.am.add_pool(pool2, match_cb) self.assertEqual(self.am.default_pool, pool1)
def testAcquireAccount(self): account1 = Account('user1', 'test') self.assertRaises(ValueError, self.am.acquire_account) self.am.add_account(account1) self.assertEqual(self.am.acquire_account(), account1) account1.release() account2 = Account('user2', 'test') self.am.add_account(account2) self.assertEqual(self.am.acquire_account(account2), account2) account2.release() account = self.am.acquire_account() self.assertNotEqual(account, None) account.release() account3 = Account('user3', 'test') pool = AccountPool() pool.add_account(account3) self.am.add_pool(pool) self.assertEqual(self.am.acquire_account(account2), account2) account2.release() self.assertEqual(self.am.acquire_account(account3), account3) account3.release() account = self.am.acquire_account() self.assertNotEqual(account, None) account.release()
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys from Exscript.protocols import SSH2 from Exscript.Account import Account username = '******' password = '******' ipv4 = '192.168.0.1' session = SSH2() session.connect(ipv4) session.login(Account(name=username, password=password)) print '='*40 print 'Step 1. run show command' print '='*40 session.execute('show configuration interfaces xe-0/0/0 | no-more') print session.response print '='*40 print 'Step 2. read config file' print '='*40 config_filename = sys.argv[1] with open(config_filename, 'r') as config_file : config_lines = config_file.readlines() print config_filename print config_lines
def __init__(self, ip, user_id, password, version): self.conn.connect(ip) self.os_version = version account = Account(user_id, password) self.conn.login(account) self.conn.execute('terminal length 0')
def login(self): """login.""" self.session = SSH2() self.session.connect(self.ipv4) self.session.login(Account(name=self.username, password=self.password))
def testAddAccount(self): self.assertEqual(0, self.am.default_pool.n_accounts()) account = Account('user', 'test') self.am.add_account(account) self.assertEqual(1, self.am.default_pool.n_accounts())
def setUp(self): self.am = AccountManager() self.data = {} self.account = Account('user', 'test')