Skip to content

Telnet silently fails when timeout is exceeded #1

Description

@brunoloureiro

The Telnet connection silently fails when the configured timeout is exceeded.

file: server/machine.py
Machine.__telnet_login() may return a telnetlib.Telnet that is still stuck on login.

Causes: Telnet.read_until(expected, timeout[opt]) returns normally after timeout. The rest of the code executes under the assumption that it returned after encountering the expected token.

Solution: Determine whether read_until returned due to encountering the expected token or if it timed out.

Draft solution:

    def __read_until(self, tn, expected_string, max_tries=1, sensitive_info=False):
        login_successful = False
        for i in range(max_tries):
            output = tn.read_until(expected_string, timeout=self.__max_timeout_time)
            if output.endswith(expected_string):
                login_successful = True
                break
            else:
                if not sensitive_info:
                    self.__logger.debug(f"Attempt {i+1} to login failed. Expected {expected_string}, got {output}.")

        return login_successful

    def __telnet_login(self) -> telnetlib.Telnet:
        """ Return a telnet session
        :return:
        """
        tn = telnetlib.Telnet(self.__dut_ip, timeout=self.__max_timeout_time)
        success = self.__read_until(tn, b'ogin: ')
        if not success:
            raise RuntimeError("Failed to login into Telnet. Could not input username.")
        tn.write((self.__dut_username + '\n').encode('ascii'))

        #self.__logger.debug(f"Username: {self.__dut_username}")
        #tn.read_very_eager()
        success = self.__read_until(tn, b'assword: ', sensitive_info=True)
        if not success:
            raise RuntimeError("Could not login into Telnet. Could not input password.")
        tn.write((self.__dut_password + '\n').encode('ascii'))

        success = self.__read_until(tn, b'$ ', sensitive_info=True)
        if not success:
            raise RuntimeError("Could not login into Telnet. Failed after trying to enter inputs.")
        self.__logger.debug("Successfully logged into Telnet.")
        return tn

Machine.__read_until() can simulate the (probably) expected behaviour of Telnet.read_until() of failing upon timeout by setting max_tries to 1.
The sensitive_info argument can be used to suppress logging of potentially sensitive information (e.g., passwords), or even disable logging inside that function altogether.

edit: RuntimeException -> RuntimeError

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions