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

Issues with error handling for HelloHandler when using tty_netconf transport #1254

Open
monnierj opened this issue Aug 1, 2023 · 3 comments

Comments

@monnierj
Copy link

monnierj commented Aug 1, 2023

Hi,

I've found something off in the open function from lib/jnpr/junos/transport/tty_netconf.py.

The data read upon opening connection is fed into HelloHandler.parse can be of two types:

  • an XML element, if there are errors
  • a bytes sequence, if the data was XML-compliant

If the case of an unrecoverable error, _parse_buffer returns an XML element, on which we call decode(). It obviously raises an exception:

Traceback (most recent call last):
  File "/path/to/test/script/test_junos.py", line 15, in open
    jnpr_device.open()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 216, in open
    raise ex
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 207, in open
    self._tty_login()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 316, in _tty_login
    self._tty.login()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/transport/tty.py", line 117, in login
    self.nc.open(at_shell=self.at_shell)
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/transport/tty_netconf.py", line 72, in open
    self._session_id, _ = HelloHandler.parse(self.hello.decode("utf-8"))
AttributeError: 'lxml.etree._Element' object has no attribute 'decode'

The related PR fixes the issue by checking whether we have a bytes sequence or a XML element, and conditionally calling decode on it.

I can provide a reproduction script, even if it's not really useful (just a connection to an SRX300 through a terminal server).

monnierj added a commit to monnierj/py-junos-eznc that referenced this issue Aug 1, 2023
monnierj added a commit to monnierj/py-junos-eznc that referenced this issue Aug 1, 2023
@monnierj monnierj changed the title Netconf transport error handling issues in _parse_buffer() Issues with error handling for HelloHandler when using tty_netconf transport Aug 1, 2023
@chidanandpujar
Copy link
Collaborator

Hi @monnierj
Please share the scripts to replicate this issue ,

Thanks

@monnierj
Copy link
Author

monnierj commented Nov 21, 2023

Hi,

Here is a simple reproduction script, junos-eznc is in the latest tagged version, which matches what can be found in the documentation:

from jnpr.junos import Device

CS_HOSTNAME = "ts_hostname"
CS_USERNAME = "ts_user"
CS_PASSWORD = "xxx"

DEVICE_USERNAME = "noc"
DEVICE_PASSWORD = "xxx"

with Device(
    host=CS_HOSTNAME, cs_user=CS_USERNAME, cs_passwd=CS_PASSWORD,
    user=DEVICE_USERNAME, passwd=DEVICE_PASSWORD, timeout=6
) as dev:
    print("Connected!")
    print(dev.facts)

The issue has been reproduced both on macOS+Python 3.10 and Debian Linux 11+Python 3.9.

The target device is an SRX300 running JUNOS 21.4R3-S3.4, and the console server is an Avocent ACS8000.
Manually connecting to the console server works fine.

Thanks!

@chidanandpujar
Copy link
Collaborator

Hi @monnierj
Thanks for sharing the details .
I will try to replicate this issue on local setup .

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants