简体   繁体   中英

Allowing configure in send_command in netmiko

I'm trying to use.netmiko to work as well as cli_command module for Ansible. cli_command allows to send arbitrary list of strings into device (fe configure , sh te 1/0/1 , exit in a single multi-line string) and it works without issues.

Netmiko requires a separate send_config and send_command and causes hangs for connection if 'configure' is send as a command.

Is there any way to force.netmiko to accept modified prompt in configure mode like it's a normal command?

Basically, I want this to work:

connection.send_command('''
  configure
  shutdown te 1/0/1
  end
  show interface te 1/0/1
''')

send_command works by detecting the device prompt (pattern-based), then it knows the command is done when the prompt is detected. This happens every command when you send them using send_command .

Instead, use send_command_timing it's another version of send_command but delay-based and doesn't look for the device prompt.

from netmiko.ssh_dispatcher import ConnectHandler

device = {
    "device_type": "cisco_ios",
    "ip": "",
    "username": "",
    "password": "",
    "session_log": "log.log",
}

with ConnectHandler(**device) as ssh_conn:
    output = ssh_conn.send_command_timing(  # Notice the send_command_timing()
        command_string="""
    configure terminal
    interface loopback 925
    shutdown
    end
    show interface loopback 925
    """
    )
print(output)

Also check this blog post on Python for Network Engineers, I think you will find it very useful.

I was getting similar error in send command:

"netmiko.exceptions.ReadTimeout: Pattern not detected: '{device hostname}\\#' in output."

And I can confirm tat the solution proposed by @Tes3away fixed the issue for me.

As in this example, you can take advantage of both the timing and prompt properties.

prompt_gpon_fnk = net_connect.find_prompt()
hostname_fnk = prompt_gpon_fnk.strip("<" + ">")
print(hostname_fnk)
net_connect.send_command_timing("enable")
net_connect.send_command_timing("undo smart")
output = net_connect.send_command_timing("config")
print("config moda girildi")
net_connect.send_command_timing("acl 2010 ")
net_connect.send_command_timing("quit")
net_connect.send_command_timing("save")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM