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

Fix for 1243 #1306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Aaron-MJohn
Copy link

Fix for 1243.

Previously if a bad password is given for serial connection, there is EzErrors.ConnectAuthError(self, "Bad username/password") raised. But this doesn't disconnect the serial port, till the script is killed. I have added a disconnect call before the raise to account for this.

@Aaron-MJohn
Copy link
Author

Aaron-MJohn commented Apr 14, 2024

This is the test code I ran


#!/usr/bin/python3

from jnpr.junos import Device
from jnpr.junos.utils.config import Config

def connect_device(password,i=0):
    try:
        with Device(
            user="root",
            port="COM5",
            mode="serial",
            passwd=password,
            gather_facts=False,
            conn_open_timeout=1.5,
        ) as dev:
            dev.facts
    except Exception:
        print(i)
    finally:
        i += 1

i= 0
password_list = ["wrong password", "wrong password"]
while(i<2):
    password = password_list[i]
    print(i)
    connect_device(password,i)
    i+=1

Py-junos-eznc latest output:

0
b'\r\n\r\n\r\n\r\nLogin incorrect\r\n' badpasswd
None None
b'<close-session/>\r\n' cli
ERROR:  login:Error: netconf not responding


Complete traceback message: Traceback (most recent call last):
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 207, in open
    self._tty_login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 316, in _tty_login
    self._tty.login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty.py", line 116, in login
    self.nc.open(at_shell=self.at_shell)
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty_netconf.py", line 68, in open
    raise RuntimeError("Error: netconf not responding")
RuntimeError: Error: netconf not responding

0
1
ERROR:  login:open_failed:None


Complete traceback message: Traceback (most recent call last):
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty_serial.py", line 44, in _tty_open
    self._ser.open()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM5': PermissionError(13, 'Access is denied.', None, 5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 207, in open
    self._tty_login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 316, in _tty_login
    self._tty.login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty.py", line 107, in login
    self._tty_open()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty_serial.py", line 46, in _tty_open
    raise RuntimeError("open_failed:{}".format(err.strerror))
RuntimeError: open_failed:None

1

With the changes :

0
b'\r\r\n\r\n\r\n\r\r\nFreeBSD/arm () (ttyu0)\r\r\n\r\r\nlogin: \r\n' login
b'\r\r\nFreeBSD/arm (t) (ttyu0)\r\r\n\r\r\nlogin: \r\n' login
b'\r\r\nFreeBtechops-ex2300-c) (ttyu0)\r\r\n\r\r\nlogin: \r\n' login
b'\r\r\nFreeBSD/arm (techops-ex2300-c) (ttyu0)\r\r\n\r\r\nlogin: root\r\nPassword:\r\n' passwd
b'Login incorrect\r\n' badpasswd
b'login: root\r\nPassword:\r\n' passwd
b'\r\n\r\nLogin incorrect\r\n' badpasswd
Exception occurred: login:ConnectAuthError(host: None, msg: Bad username/password)

0
1
b'\r\n\r\n\r\n\r\nlogin: login: login: Password:' passwd
b'\r\nLogin incorrect\r\n' badpasswd
None None
b'<close-session/>\r\n' cli
ERROR:  login:Error: netconf not responding


Complete traceback message: Traceback (most recent call last):
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 207, in open
    self._tty_login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\console.py", line 316, in _tty_login
    self._tty.login()
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty.py", line 117, in login
    self.nc.open(at_shell=self.at_shell)
  File "C:\Users\BGUser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\jnpr\junos\transport\tty_netconf.py", line 69, in open
    raise RuntimeError("Error: netconf not responding")
RuntimeError: Error: netconf not responding

1

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

Successfully merging this pull request may close these issues.

None yet

1 participant