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

How to add device prompt using kwargs to identify the pattern? #65

Open
robthebuilder123 opened this issue Nov 1, 2023 · 13 comments
Open

Comments

@robthebuilder123
Copy link

while running:


from nornir import InitNornir
from nornir_utils.plugins.functions import print_result
from nornir_netmiko.tasks import netmiko_send_config

nr = InitNornir(config_file="config.yaml")
output = nr.run(netmiko_send_config, config_commands=["delete interface ae1 unit 0"])
print_result(output)

==================================================================

Error recieved

=============
netmiko.exceptions.ReadTimeout:

Pattern not detected: 'delete\\ interface\\ ae1\\ unit\\ 0' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.

=========================================================================
How to add device prompt using kwargs to identify the pattern?

@ktbyers
Copy link
Owner

ktbyers commented Nov 1, 2023

What does executing this command look like in the CLI (i.e. when you execute it in the CLI)?

"delete interface ae1 unit 0"

@robthebuilder123
Copy link
Author

It is a juniper device config mode command. It works on the device.

@robthebuilder123
Copy link
Author

robthebuilder123 commented Nov 1, 2023

With naplm nornir as well it gives error, i have observed that juniper device config mode commands dont work. Show commands work perfectly.

@ktbyers
Copy link
Owner

ktbyers commented Nov 1, 2023

Yes, I want to see the actual CLI output so I can see why Netmiko is complaining.

In other words, if you enter in this command:

"delete interface ae1 unit 0"

Is this what you see or is the above an abbreviation that gets expanded?

@robthebuilder123
Copy link
Author

robthebuilder123 commented Nov 1, 2023

device_login_name@Juniper_device_id> configure 
Entering configuration mode

[edit]
device_login_name@Juniper_device_id# delete interfaces ae1 unit 0 

[edit]
device_login_name@Juniper_device_id# commit 
commit complete

[edit]
device_login_name@Juniper_device_id# 

@ktbyers
Copy link
Owner

ktbyers commented Nov 1, 2023

Okay, try doing:

output = nr.run(netmiko_send_config, config_commands=["delete interface ae1 unit 0"], cmd_verify=False)

Also, make sure you are using the latest released version of Netmiko.

@ktbyers
Copy link
Owner

ktbyers commented Dec 7, 2023

@robthebuilder123 Did the above command work?

@denialmx
Copy link

denialmx commented Aug 3, 2024

Also trying to figure out if there is a way to pass kwargs to the enable() function inside netmiko_send_command.
the enable function in linux_ssh.py in netmiko contains a cmd, pattern and enable_pattern options, seems to work ok out of the box with Ubuntu, but other linux like Alpine break, as the cmd to enable is not sudo su, but su instead.

Is there a way to enable this? I fixed it for now adding some checks in my local repo, but not sure what the best idea is to fix this if possible.
Thanks

@ktbyers
Copy link
Owner

ktbyers commented Aug 3, 2024

@denialmx Can you post your code include the dictionary of arguments.

It is a standard Python method so you can pass in **kwargs (as per normal).

@denialmx
Copy link

denialmx commented Aug 3, 2024

Hi @ktbyers I'm currently solving my issue with this code.
the enable function inside linux_ssh.py in netmiko contains the following arguments below. I'm only mapping cmd and pattern

def enable(
        self,
        cmd: str = "sudo -s",
        pattern: str = "ssword",
        enable_pattern: Optional[str] = None,
        re_flags: int = re.IGNORECASE,
    ) -> str:
        """Attempt to become root."""

in netmiko_send_command the code I'm using is below:
First I'm mapping arguments, not sure if cmd or pattern are used in the other calls below and I pop the values out.

Basically, I look for enable_cmd, enable_pattern, and if they exist I put them in enable_kwargs.
then I call enable with those new kwargs and I pop them out of the original kwarg dictionary, otherwise it errors out on subsequent calls.

enable_kwargs = {mapped_arg: kwargs.pop(arg)
                     for arg, mapped_arg in {'enable_cmd': 'cmd', 'enable_pattern': 'pattern'}.items()
                     if arg in kwargs}
    if enable:
        net_connect.enable(**enable_kwargs)

so far it has worked without issue to sudo into alpine linux and ubuntu on my side.
not sure what the best way to contribute in this repo, I'm not really a programmer but a network engineer, also the mapping I do might not be desirable, but I'm open to commit and learn if it makes sense for this project.

Thanks

@ktbyers
Copy link
Owner

ktbyers commented Aug 5, 2024

@denialmx So you don't have any issue then?

@denialmx
Copy link

denialmx commented Aug 8, 2024

Hi @ktbyers, I currently don't have an issue. This code seems to work well for me in netmiko_send_command.
Anyway to integrate it upstream, or you suggest it's better to build my own task outside of this plugin?

Thanks in advance!

@ktbyers
Copy link
Owner

ktbyers commented Aug 8, 2024

@denialmx I would think this would be just in your own task outside of the plugin, but let me know if I am missing something on this (i.e. some reason that is not practical).

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

3 participants