Example #1
0
	def onPasswordPromptNotDetected(self):
		"""
		"""
		self.debug( 'password prompt not detected' )
		
		# log event		
		errstr = 'password prompt not detected'
		tpl = templates.ssh(host=self.host, port=self.port, more=templates.error(errstr) )
		self.logRecvEvent( shortEvt = "password prompt error", tplEvt = tpl )
			
		# cleanup
		self.cleanProcess()
Example #2
0
	def isError(self, timeout=1.0, errReason=None):
		"""
		Waits to receive "error" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			

		@param data: error reason expected
		@type data:	string/operators/none	
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
		if not ( isinstance(timeout, int) or isinstance(timeout, float) ) or isinstance(timeout,bool): 
			raise TestAdapterLib.ValueException(TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout) )
		
		expected = templates.ssh(host=self.host, port=self.port, more=templates.error(errReason) )
		evt = self.received( expected = expected, timeout = timeout )
		return evt
Example #3
0
    def isError(self, timeout=1.0, errReason=None):
        """
		Waits to receive "error" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			

		@param data: error reason expected
		@type data:	string/operators/none	
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        expected = templates.ssh(host=self.host,
                                 port=self.port,
                                 more=templates.error(errReason))
        evt = self.received(expected=expected, timeout=timeout)
        return evt
Example #4
0
    def login(self):
        """
		Login to the server
		"""
        # log event
        tpl = templates.ssh(host=self.host,
                            port=self.port,
                            more=templates.login(username=self.username,
                                                 password=self.password))
        self.logSentEvent(shortEvt="login", tplEvt=tpl)

        SSH_NEWKEY = r'Are you sure you want to continue connecting'
        SSH_KEYCHANGED = r'REMOTE HOST IDENTIFICATION HAS CHANGED'

        # connect
        cmdSSH = 'ssh -l %s %s -p %s -o "UserKnownHostsFile /dev/null"' % (
            self.username, self.host, self.port)
        self.debug(cmdSSH)
        self.child = pexpect.spawn(cmdSSH, timeout=self.timeout)
        r = self.child.expect([
            pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: ', SSH_KEYCHANGED,
            pexpect.EOF
        ])

        if r == 0:  # Timeout
            self.debug(self.child.before)
            self.onPasswordPromptNotDetected()

        elif r == 1:  # accept public key.
            # public key stored in the 'known_hosts' cache.
            self.debug('public key prompt detected')

            # send yes to accept
            self.child.sendline('yes')
            r = self.child.expect([pexpect.TIMEOUT, '[Pp]assword: '])

            if r == 0:  # Timeout
                self.debug(self.child.before)
                self.onPasswordPromptNotDetected()

            elif r == 1:  # ok
                self.debug(self.child.before)
                self.__type_pass()

            else:  # unknown error
                self.debug(self.child.before)
                self.error('login: sub unknown error')

        elif r == 2:
            self.debug(self.child.before)
            self.__type_pass()

        elif r == 3:
            self.debug('rsa finger has changed detected')
            self.debug(self.child.before)

            # log event
            tpl = templates.ssh(host=self.host,
                                port=self.port,
                                more=templates.error(self.child.before))
            self.logRecvEvent(
                shortEvt="connection error: rsa fingerprint has changed",
                tplEvt=tpl)

            # cleanup
            self.cleanProcess()

        elif r == 4:
            # ssh: connect to host 227.0.0.2 port 22: Network is unreachable
            # ssh: connect to host 127.0.0.1 port 34: Connection refused
            self.debug(self.child.before)
            errstr = self.child.before.split(':')[2].strip()

            # log event
            tpl = templates.ssh(host=self.host,
                                port=self.port,
                                more=templates.error(errstr.lower()))
            self.logRecvEvent(shortEvt="connection error", tplEvt=tpl)

            # cleanup
            self.cleanProcess()
        else:
            self.error('login: unknown error')