carlmontanari / scrapli

Fast, flexible, sync/async, Python 3.7+ screen scraping client specifically for network devices
https://carlmontanari.github.io/scrapli/
MIT License
575 stars 59 forks source link

Timeout with telnet transport #252

Closed egreenspan2 closed 2 years ago

egreenspan2 commented 2 years ago

Describe the bug Unable to open a telnet session using nornir-scrapli, getting socket.timeout: timed out

To Reproduce Steps to reproduce the behavior:

  1. Configure scrapli transport parameters as follows: host.connection_options["scrapli"] = ConnectionOptions( extras={ "auth_secondary": "XXXX", "port": 23, "transport": "telnet", } ) Execute a nornir tasks to connect and it immediately thows socket.timeout (from telnetlib library?)

  2. What you're connecting to (vendor, platform, version)

    Cisco switch, WS-C3750G-24T, (old code: 12.2(35)SE5)

  3. Anything else relevant

    Simple script using telnetlib also throwing timeout using example script

`import telnetlib

HOST = "hostname" timeout=5

tn = telnetlib.Telnet(HOST, timeout=time_out) tn.set_debuglevel(100)

tn.read_until(b"Username:")

tn.close()`

Expected behavior Should establish telnet connection

Stack Trace Traceback (most recent call last): File "/redacted/.venv/lib/python3.8/site-packages/nornir/core/task.py", line 99, in start r = self.task(self, **self.params) File "/redacted/.venv/lib/python3.8/site-packages/nornir_scrapli/tasks/core/send_configs_from_file.py", line 58, in send_configs_from_file scrapli_conn = task.host.get_connection("scrapli", task.nornir.config) File "/redacted/.venv/lib/python3.8/site-packages/nornir/core/inventory.py", line 494, in get_connection self.open_connection( File "/redacted/.venv/lib/python3.8/site-packages/nornir/core/inventory.py", line 546, in open_connection conn_obj.open( File "/redacted/.venv/lib/python3.8/site-packages/nornir_scrapli/connection.py", line 105, in open connection.open() File "/redacted/.venv/lib/python3.8/site-packages/scrapli/driver/base/sync_driver.py", line 84, in open self.transport.open() File "/redacted/.venv/lib/python3.8/site-packages/scrapli/transport/plugins/telnet/transport.py", line 152, in open self._handle_control_chars() File "/redacted/.venv/lib/python3.8/site-packages/scrapli/transport/plugins/telnet/transport.py", line 127, in _handle_control_chars c = self._read(1) File "/redacted/.venv/lib/python3.8/site-packages/scrapli/transport/plugins/telnet/transport.py", line 192, in _read return self.socket.sock.recv(n) socket.timeout: timed out

Screenshots n/a

OS (please complete the following information):

Additional context

carlmontanari commented 2 years ago

There is probably very very small chance this will get fixed unless you dig into it. The control character handling is a pita and was mostly taken for c python (but custom setup because it will be deprecated). Current setup has been tested with a fair few platforms but nothing that old afaik — there may be some crufty stuff floating around in there.

You’d need to be looking in here give or take, and you’d probably want to be checking against telnetlib as clearly that works.

egreenspan2 commented 2 years ago

Carl,

Thank you so much for the quick reply, I will review the code and see if I can figure it out, it is a bit of a low priority as telnet only devices are small and dwindling population. This seems to be an issue with newer telnetlib library, I suspect.

carlmontanari commented 2 years ago

Ah to be clear — this is not a telnetlib/cpython issue! This is a scrapli/my fault issue :)

telnetlib is getting prepared for deprecation in like 3.13 or something I think, so I made my own telnet transport that is very similar to telnetlib but not a straight up vendor. I had already done this for async telnet so I basically just made that but sync. It’s of course super similar to telnetlib because man who has time to go figure out all that control character nonsense haha so that’s why I mention referring back to telnetlib to see what I gutted that may be relevant in this current situation!

haccht commented 2 years ago

Hi! I also had a same issue, and created PR to fix this problem. #253 Is it possible to be reviewed or get an advice about this please?

carlmontanari commented 2 years ago

Going to close this and handle all the follow up in #253! thanks a bunch @egreenspan2 for reporting and for the work @haccht !

Carl