def testCommandTimeout(self, MockExpect): ''' If after invoking a cli command the expected prompt is not found in the response a timeout event is triggered. ''' responses = [ 'pyco@localhost password: '******'''Linux cencenighe 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011 x86_64 GNU/Linux\r Ubuntu 10.04.2 LTS\r \r Welcome to Ubuntu!\r * Documentation: https://help.ubuntu.com/\r \r Last login: Thu Feb 24 09:05:39 2011 from localhost\r\n$ ''', 'to be cleared by clear_buffer', '$ ', 'to be cleared by clear_buffer', 'no prompt' ] log.info("testLoginTimeout ...") h = device('telnet://*****:*****@h') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses h.login() self.failUnlessRaises(ConnectionTimedOut, h.send, 'id')
def testPromptDiscoveryDisabled(self, MockExpect): ''' Disable the prompt discovery. ''' responses = [ 'pyco@localhost password: '******'''Linux cencenighe 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011 x86_64 GNU/Linux\r Ubuntu 10.04.2 LTS\r \r Welcome to Ubuntu!\r * Documentation: https://help.ubuntu.com/\r \r Last login: Thu Feb 24 09:05:39 2011 from localhost\r\n$ some output''', 'i am pyco' ] log.info("testLoginTimeout ...") h = device('telnet://*****:*****@h') h.discoverPrompt = False MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses h.login() h.send('id')
def testTC1(self, MockExpect): log.info("testTC1 ...") h = device('ssh://*****:*****@host') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses self.failUnlessRaises(PermissionDenied, h.login)
def testFakeOk(self): log.debug("testFakeOk ...") h = device("telnet://%s:%s@%s:%d" % ("username", "secret", "localhost", TELNET_PORT)) h.maxWait = 2 out = h("id") print("--> %s" % out) self.assertRegex(out, "uid=[0-9]+\(pyco\).*")
def testNativeTelnet(self): log.info("testNative ...") user = environ["USER"] log.info("USER: [%s]" % user) h = device("telnet://%s:%s@%s" % (user, user, "localhost")) h.maxWait = 10 self.assertRaises(PermissionDenied, h, "id")
def testSimpleCommand(self): log.debug("starting testSimpleCommand ...") target = device('telnet://%s:%s@%s:%d' % ('kenobi', 'secret', 'localhost', TELNET_PORT)) target.maxWait = 2 out = target('id') target.close() self.assertRegex(out, 'uid=[0-9]+\(pyco\).*')
def _testLoginTimeout(self, MockExpect): responses = ['unexpected response (expected password prompt)', 'to be cleared by clear_buffer'] log.info("testLoginTimeout ...") h = device('telnet://*****:*****@h') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses self.failUnlessRaises(LoginFailed, h.login)
def _testLoginTimeout(self, MockExpect): responses = [ 'unexpected response (expected password prompt)', 'to be cleared by clear_buffer' ] log.info("testLoginTimeout ...") h = device('telnet://*****:*****@h') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses self.failUnlessRaises(LoginFailed, h.login)
def testTC1(self, MockExpect): log.info("testTC1 ...") h = device('telnet://*****:*****@h') h.maxWait = 20 print(h.patternMap['GROUND']) MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses out = h.login() h.send('id')
def testNoPassword(self, MockExpect): from pyco.device import cliIsConnected log.info("testNoPassword ...") #h = device('telnet://%s:%s@%s' % (hop1['username'], hop1['password'], hop1['name'])) h = device('telnet://*****:*****@h') h.remove_event('username_event', 'GROUND') h.add_event_action('timeout', action=cliIsConnected, endState='USER_PROMPT') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = self.responses out = h.login()
def testNoPromptAfterResponse(self, MockExpect): from pyco.device import cliIsConnected log.info("testNoPromptAfterResponse ...") cisco = device('telnet://*****:*****@h') from pyco.actions import sendUsername cisco.add_event_action('username-event', pattern='Username: '******'GROUND') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = self.responses out = cisco.login() self.failUnlessRaises(ConnectionTimedOut, cisco.send, 'show version')
def testMulti(self): log.debug("testMulti ...") setface("ciscoios") h = device('telnet://%s:%s@%s:%d/ciscoios' % ('obi-wan-kenobi', 'secret', 'localhost',TELNET_PORT)) h.maxWait = 2 #p = Client()[:] hosts = [h] * 10 with Pool(5) as p: results = p.map(command, hosts) self.assertEqual(len(results), 10, "expected 10 command outputs") self.assertNotIn(None, results, "some commands failed unexpectely")
def testMulti(self): log.debug("testMulti ...") setface("ciscoios") h = device('telnet://%s:%s@%s:%d/ciscoios' % ('obi-wan-kenobi', 'secret', 'localhost', TELNET_PORT)) h.maxWait = 2 #p = Client()[:] hosts = [h] * 10 with Pool(5) as p: results = p.map(command, hosts) self.assertEqual(len(results), 10, "expected 10 command outputs") self.assertNotIn(None, results, "some commands failed unexpectely")
def testCommandTimeout(self, MockExpect): ''' If after invoking a cli command the expected prompt is not found in the response a timeout event is triggered. ''' responses = ['pyco@localhost password: '******'''Linux cencenighe 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011 x86_64 GNU/Linux\r Ubuntu 10.04.2 LTS\r \r Welcome to Ubuntu!\r * Documentation: https://help.ubuntu.com/\r \r Last login: Thu Feb 24 09:05:39 2011 from localhost\r\n$ ''', 'to be cleared by clear_buffer', '$ ','to be cleared by clear_buffer', 'no prompt'] log.info("testLoginTimeout ...") h = device('telnet://*****:*****@h') MockExpect.side_effect = simulator.side_effect simulator.side_effect.responses = responses h.login() self.failUnlessRaises(ConnectionTimedOut, h.send, 'id')
if target.has_event_handlers(target.currentEvent): log.debug("[%s] got [%s] event; invoking handlers: [%s]" % (target.name, target.currentEvent.name, target.get_event_handlers(target.currentEvent))) for eh in target.get_event_handlers(target.currentEvent): eh(target) stateChanged = target.process(target.currentEvent) response += self.pipe.before if isinstance(self.pipe.after, str) and not target.currentEvent.isPromptMatch(): response += self.pipe.after return response # def processResponseWithTimeout(self, target, checkPoint): # patterns = [pexpect.TIMEOUT] # return self.patternMatch(target, checkPoint, patterns, target.maxWait, exactMatch=True) def processResponse(self, target, checkPoint): ''' ''' return self.patternMatch(target, checkPoint, [TIMEOUT], target.maxWait) # The source point of all paths SOURCE_HOST = device('__source_host__') # the source is connected for definition SOURCE_HOST.is_connected = lambda : True
def testWrongPassword(self): log.debug("testWrongPassword ...") h = device("telnet://%s:%s@%s:%d" % ("username", "wrong_pwd", "localhost", TELNET_PORT)) self.assertRaises(PermissionDenied, h, "id")
def testWrongPassword(self): log.debug("testWrongPassword ...") target = device('telnet://%s:%s@%s:%d' % ('kenobi', 'wrong_pwd', 'localhost', TELNET_PORT)) target.close() self.assertRaises(PermissionDenied, target, 'id')
def doUname(hostname, user, pwd, protocol): print("doUname") h = device("%s://%s:%s@%s:7777" % (protocol, user, pwd, hostname)) out = h('uname -a')
def testWrongUsername(self): log.debug("testWrongUsername ...") target = device('telnet://%s:%s@%s:%d' % ('fake_username', 'secret', 'localhost', TELNET_PORT)) target.close() self.assertRaises(PermissionDenied, target, 'id')
def testConnectionRefused(self): log.debug("testConnectionRefused ...") target = device('telnet://%s:%s@%s:%d' % ('fake_username', 'secret', 'localhost', 9999)) target.close() self.assertRaises(ConnectionRefused, target, 'id')
def doUname(hostname,user,pwd,protocol): print("doUname") h=device("%s://%s:%s@%s:7777" % (protocol,user,pwd,hostname)) out=h('uname -a')
def command(host): try: print("----> [%s]" % host.driver) if (host.driver.name == "ciscoios"): cioscoios_show_ip_local_pool(host) elif(host.driver.name == 'juniper'): print("Thommy lets happen!") else: print("unknown device type %s" % host.driver) except Exception: #print("%s: interrogazione fallita, vedere file di log per i dettagli" % host.name) #traceback.print_exc(file=sys.stdout) logging.exception("interrogazione fallita") finally: pass if __name__ == '__main__': indb = Database() with Pool(4) as pool: for n in indb.getall(): print("telnetting %s" % n.name) host = device('telnet://%s:%s@%s:%d/ciscoios' % (n.username, n.password, 'localhost', TELNET_PORT)) pool.apply(command, (host,))
if target.has_event_handlers(target.currentEvent): log.debug("[%s] got [%s] event; invoking handlers: [%s]" % (target.name, target.currentEvent.name, target.get_event_handlers(target.currentEvent))) for eh in target.get_event_handlers(target.currentEvent): eh(target) stateChanged = target.process(target.currentEvent) response += self.pipe.before if isinstance(self.pipe.after, str) and not target.currentEvent.isPromptMatch(): response += self.pipe.after return response # def processResponseWithTimeout(self, target, checkPoint): # patterns = [pexpect.TIMEOUT] # return self.patternMatch(target, checkPoint, patterns, target.maxWait, exactMatch=True) def processResponse(self, target, checkPoint): ''' ''' return self.patternMatch(target, checkPoint, [TIMEOUT], target.maxWait) # The source point of all paths SOURCE_HOST = device('__source_host__') # the source is connected for definition SOURCE_HOST.is_connected = lambda: True