Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EOF control in telnet transport with old devices #351

Closed
lorenzo-d-m opened this issue Oct 10, 2024 · 4 comments
Closed

EOF control in telnet transport with old devices #351

lorenzo-d-m opened this issue Oct 10, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@lorenzo-d-m
Copy link
Contributor

Hi Carl,
I'm facing an issue with telnet transport trying to gather configurations from Cisco PIX devices and also from some old ASA.
Such devices put an EOF sequence after the login and before getting the prompt. E.g. the read() method assigns self._raw_buf the value:

b"****\r\nType help or '?' for a list of available commands.\r\n\r\x00SEG-TTS-VDF/MONITOR> "

It contains the redacted password, the hint proposed by the device, and unfortunately, in front of the prompt it contains an EOF sequence \x00.

As self._raw_buf is processed by the method _handle_control_chars, it throws a
scrapli.exceptions.ScrapliConnectionNotOpened: server returned EOF, connection not opened
then the connection is closed.

To reproduce:

import asyncio
from scrapli.transport.plugins.asynctelnet.transport import AsynctelnetTransport
from scrapli.transport.plugins.asynctelnet.transport import PluginTransportArgs
from scrapli.transport.base.base_transport import BaseTransportArgs


base_transport_args = BaseTransportArgs(
    transport_options={
        'asynctelnet2': {'socks5_address': ('192.168.35.180', 20022), 'socks5_auth': ('test', '************')}
    },
    host="10.3.2.155",
)

asynctelnet_transport = AsynctelnetTransport(
    base_transport_args=base_transport_args,
    plugin_transport_args=PluginTransportArgs(),
)

asynctelnet_transport.stdout = asyncio.StreamReader()

asynctelnet_transport._raw_buf = b"****\r\nType help or '?' for a list of available commands.\r\n\r\x00SEG-TTS-VDF/MONITOR> "  # read from device
			
asynctelnet_transport._handle_control_chars()


Expected behavior:
No exceptions



Stack Trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\MYDEV\Repos\netconfig\venv\Lib\site-packages\scrapli\transport\plugins\asynctelnet\transport.py", line 121, in _handle_control_chars
    raise ScrapliConnectionNotOpened("server returned EOF, connection not opened")
scrapli.exceptions.ScrapliConnectionNotOpened: server returned EOF, connection not opened


OS:
Windows 11 Pro
Scrapli Version: 2024.07.30



Proposed solution:
With some devices the EOF sequence occurs in the middle of _raw_buf, however, with others (ASA) EOF occurs at the end of _raw_buf.

Since _handle_control_chars() is called from read(), which in turn is wrapped in the timeout decorator, I suggest to remove the control on EOF inside _handle_control_chars().

scrapli/scrapli/transport/plugins/asynctelnet/transport.py

120 >>> # if self._raw_buf.find(NULL) != -1:
121 >>>     # raise ScrapliConnectionNotOpened("server returned EOF, connection not opened")

Re-running the above code commenting out the EOF control matches the expected behavior.

The same goes for the sync module.

@lorenzo-d-m lorenzo-d-m added the bug Something isn't working label Oct 10, 2024
@carlmontanari
Copy link
Owner

thanks for this @lorenzo-d-m ill try to take a peak this weekend. will have to re-remember how all the telnet thing work 😄

@carlmontanari
Copy link
Owner

hey @lorenzo-d-m sorry im just getting back to this.

when I sat down to look at this this rang some bells in my memory :D looks like this area was tweaked a bit not too long ago here and from Matt's digging into this it shouldn't be necessary anyway. since I have no devices to really test against I'm always a little spooked to make changes to core bits like this -- have you been able to test your update on other devices as well by any chance? If yeah, I'd say let's do it hah, if you're up for a minor pr to nuke those two lines in sync and async that would be great.

thanks a bunch for a great issue with the repro and all the back story, very much appreciated!

@carlmontanari
Copy link
Owner

awesome thank you @lorenzo-d-m -- wrapping this up over in #355!

@lorenzo-d-m
Copy link
Contributor Author

Awesome, thank you @carlmontanari

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants