nornir_netmiko icon indicating copy to clipboard operation
nornir_netmiko copied to clipboard

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

Open robthebuilder123 opened this issue 1 year ago • 13 comments

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?

robthebuilder123 avatar Nov 01 '23 07:11 robthebuilder123

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

"delete interface ae1 unit 0"

ktbyers avatar Nov 01 '23 16:11 ktbyers

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

robthebuilder123 avatar Nov 01 '23 18:11 robthebuilder123

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

robthebuilder123 avatar Nov 01 '23 18:11 robthebuilder123

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?

ktbyers avatar Nov 01 '23 19:11 ktbyers

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# 

robthebuilder123 avatar Nov 01 '23 21:11 robthebuilder123

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 avatar Nov 01 '23 22:11 ktbyers

@robthebuilder123 Did the above command work?

ktbyers avatar Dec 07 '23 18:12 ktbyers

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

denialmx avatar Aug 03 '24 16:08 denialmx

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

ktbyers avatar Aug 03 '24 20:08 ktbyers

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

denialmx avatar Aug 03 '24 22:08 denialmx

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

ktbyers avatar Aug 05 '24 22:08 ktbyers

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!

denialmx avatar Aug 08 '24 00:08 denialmx

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

ktbyers avatar Aug 08 '24 03:08 ktbyers