question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Endless loop in "_handle_control_chars()" if server sends EOF with zero bytes and endless loop in "channel_authenticate_telnet" in "async_channel.py" with same reason

See original GitHub issue

Describe the bug Bug in asynctelnet. Code never returns from “open()” if server sends EOF. When you connect to telnet server that sends EOFs, your code gets stuck in the endless loop in “_handle_control_chars()”, because asyncio.StreamReader.read(1) will return an empty bytes object if EOF was received and the internal buffer is empty.

Here is a problem lines in “transport.py”: https://github.com/carlmontanari/scrapli/blob/062a5f4995539e2dd3b260c7e1eaf66f19b3bedb/scrapli/transport/plugins/asynctelnet/transport.py#L127-L133

This can be easly fixed with

                if not c:
                    break

after the line number 129,

but there is another problem just right after that one - with authentication in “channel_authenticate_telnet” in “async_channel.py” https://github.com/carlmontanari/scrapli/blob/062a5f4995539e2dd3b260c7e1eaf66f19b3bedb/scrapli/channel/async_channel.py#L329-L339 I see increments in “return_attempts” but there is no limit for them. And it never stops. I think we need to add some variable like “max_auth_return_retry” in “BaseChannelArgs”. And when return_attempts >= max_auth_return_retry - raise ScrapliAuthenticationFailed.

To Reproduce Steps to reproduce the behavior: pip install scrapli==2021.7.30a3

  1. Your script
        conn_dict = {
            "host": ip,
            "timeout_socket": SCRAPLI_SOCKET_TIMEOUT,
            "timeout_transport": SCRAPLI_TRANSPORT_TIMEOUT,
            "timeout_ops": SCRAPLI_OPERATION_TIMEOUT,
            "comms_prompt_pattern": SCRAPLI_PROMPT_PATTERN,
            "port": 23,
            "transport": "asynctelnet",
            "auth_username": login,
            "auth_password": password
        }
            conn = AsyncGenericDriver(**conn_dict)
            try:
                await conn.open()
            except Exception as ex:
                logging.debug(str(ex))
            else:
                await conn.close()
  1. What you’re connecting to (vendor, platform, version) D-Link DES-1210-28/ME.

Expected behavior Code returns from “open()” if server sends EOFs with ScrapliAuthenticationFailed.

Screenshots 2021-07-03

OS (please complete the following information):

  • OS: Windows 10
  • scrapli version: 2021.7.30a3

Additional context I need some guidance on how to implement limiting function for “return_attempts” in “channel_authenticate_telnet” in “async_channel.py”.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
carlmontanaricommented, Jul 3, 2021

Cool, will push that up to develop shortly. Thanks for opening this and for the help!!

Carl

1reaction
pertsevdscommented, Jul 3, 2021

I think that

c = await asyncio.wait_for(self.stdout.read(1), timeout=char_read_timeout)
if not c:
    raise ScrapliConnectionNotOpened("server returned EOF, connection not opened")

will fix all problems. Tried that with my switch and scrapli works as intended.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found