ktbyers / nornir_netmiko

Netmiko Plugins for Nornir
Apache License 2.0
80 stars 24 forks source link

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

Open robthebuilder123 opened 10 months ago

robthebuilder123 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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

robthebuilder123 commented 10 months ago

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

ktbyers commented 10 months ago

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 commented 10 months ago
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 commented 10 months ago

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 commented 9 months ago

@robthebuilder123 Did the above command work?

denialmx commented 1 month ago

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 commented 1 month ago

@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 commented 1 month ago

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 commented 1 month ago

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

denialmx commented 1 month ago

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 commented 1 month ago

@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).