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

enable() method doesn't work when connection is initialized with 'auto_connect' = False #3459

Open
VeryVVell opened this issue Jul 9, 2024 · 3 comments

Comments

@VeryVVell
Copy link

Description of Issue/Question

When ConnectHandler is initialized without parameter 'auto_connect' all works as expected, but if I submit 'auto_connect' = False to ConnectHandler and invoke establish_connection(), then subsequent enable() fails with error:

ValueError: Failed to enter enable mode. Please ensure you pass the 'secret' argument to ConnectHandler.

Netmiko version

netmiko==4.3.0

Netmiko device_type (if relevant to the issue)

cisco_ios

Steps to Reproduce the Issue

In [26]: params
Out[26]: 
{'device_type': 'cisco_ios',
 'host': '10.50.51.126',
 'username': 'wizard',
 'password': 'sanfra',
 'secret': 'cisco',
 'verbose': True,
 'auto_connect': False}

In [27]: dev = netmiko.ConnectHandler(**params)

In [28]: dev.establish_connection()
SSH connection established to 10.50.51.126:22
Interactive SSH session established

In [29]: dev.enable()

Error Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[25], line 1
----> 1 dev.enable()

File ~/pylabs/venv/lib/python3.10/site-packages/netmiko/cisco_base_connection.py:26, in CiscoBaseConnection.enable(self, cmd, pattern, enable_pattern, check_state, re_flags)
     17 def enable(
     18     self,
     19     cmd: str = "enable",
   (...)
     23     re_flags: int = re.IGNORECASE,
     24 ) -> str:
     25     """Enter enable mode."""
---> 26     return super().enable(
     27         cmd=cmd,
     28         pattern=pattern,
     29         enable_pattern=enable_pattern,
     30         check_state=check_state,
     31         re_flags=re_flags,
     32     )

File ~/pylabs/venv/lib/python3.10/site-packages/netmiko/base_connection.py:2050, in BaseConnection.enable(self, cmd, pattern, enable_pattern, check_state, re_flags)
   2048     else:
   2049         if not self.check_enable_mode():
-> 2050             raise ValueError(msg)
   2052 except NetmikoTimeoutException:
   2053     raise ValueError(msg)

ValueError: Failed to enter enable mode. Please ensure you pass the 'secret' argument to ConnectHandler.

Relevant Python code

params = {
    'device_type': 'cisco_ios',
    'host': '10.50.51.126',
    'username': 'wizard',
    'password': 'sanfra',
    'secret': 'cisco',
    'auto_connect': False,
    'verbose': True,
}

dev = netmiko.ConnectHandler(**params)
dev.establish_connection()
dev.enable()
@ktbyers
Copy link
Owner

ktbyers commented Jul 10, 2024

You need to call dev._open() if you are going to bypass the normal connection process (i.e. if you set auto_connect=False).

@ktbyers
Copy link
Owner

ktbyers commented Jul 10, 2024

So code should be:

params = {
    'device_type': 'cisco_ios',
    'host': '10.50.51.126',
    'username': 'wizard',
    'password': 'sanfra',
    'secret': 'cisco',
    'auto_connect': False,
    'verbose': True,
}

dev = netmiko.ConnectHandler(**params)
dev._open()
dev.enable()

@VeryVVell
Copy link
Author

Thanks a million for your help!

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

No branches or pull requests

2 participants