Exemplo n.º 1
0
	def __type_pass(self):
		"""
		"""
		SSH_DENIED = '[Pp]ermission denied, please try again'
		
		self.debug( 'password prompt detected' )
		# send password 
		self.child.sendline(self.password)
		r = self.child.expect([pexpect.TIMEOUT, SSH_DENIED, self.PROMPT])
		
		if r == 0:  # Timeout
			self.debug( self.child.before )
			self.onPromptNotDetected()
		
		elif r == 1:# denied
			self.debug( 'permission denied detected' )
			self.debug( self.child.before )
			
			# log event
			tpl = templates.ssh(host=self.host, port=self.port, more=templates.login_failed() )
			self.logRecvEvent( shortEvt = "login failed", tplEvt = tpl )
			
			# cleanup
			self.cleanProcess()
		elif r == 2: # ok 
			self.debug( 'prompt %s detected' % self.PROMPT)
			self.debug( self.child.before )
			self.connected = True

			# log event
			tpl = templates.ssh(host=self.host, port=self.port, more=templates.connected() )
			self.logRecvEvent( shortEvt = "connected", tplEvt = tpl )

		else:
			self.error( 'prompt: unknown error' )		
Exemplo n.º 2
0
    def sendData(self, data, dataExpected=''):
        """
		Send data over SSH

		@param data: data to send over ssh
		@type data: string
		
		@param dataExpected: expected data on response
		@type dataExpected: string
		"""
        if not self.connected:
            self.debug('not connected')
            return

        # log event
        tpl = templates.ssh(host=self.host,
                            port=self.port,
                            more=templates.data_sent(data=data))
        self.logSentEvent(shortEvt="data", tplEvt=tpl)

        # send command line
        self.child.send(data)
        if len(dataExpected):
            r = self.child.expect([pexpect.TIMEOUT, dataExpected])
        else:
            r = self.child.expect([pexpect.TIMEOUT, self.PROMPT])

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

        elif r == 1:  # ok, expected condition
            # split cmd
            data_received = "%s%s%s" % (self.child.after, self.child.before,
                                        self.child.after)

            # log received event
            tpl = templates.ssh(
                host=self.host,
                port=self.port,
                more=templates.data_received(data=data_received))
            self.logRecvEvent(shortEvt="data", tplEvt=tpl)

        else:
            self.error('send data: unknown error')
Exemplo n.º 3
0
	def logout(self):
		"""
		Disconnect from the server
		"""
		if self.connected:
			# log event
			tpl = templates.ssh(host=self.host, port=self.port, more=templates.logout() )
			self.logSentEvent( shortEvt = "logout", tplEvt = tpl )
			self.connected = False
			
			# optional step, just to be clean
			self.child.sendline('exit')
			
			# log event
			tpl = templates.ssh(host=self.host, port=self.port, more=templates.disconnected() )
			self.logRecvEvent( shortEvt = "disconnected", tplEvt = tpl )
			
		# cleanup
		self.cleanProcess()
Exemplo n.º 4
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()
Exemplo n.º 5
0
	def isLoginFailed(self, timeout=1.0):
		"""
		Waits to receive "login failed" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			
		
		@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.login_failed() )
		evt = self.received( expected = expected, timeout = timeout )
		return evt
Exemplo n.º 6
0
    def isLoginFailed(self, timeout=1.0):
        """
		Waits to receive "login failed" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float			
		
		@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.login_failed())
        evt = self.received(expected=expected, timeout=timeout)
        return evt
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
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')