0

这是我更大的程序的一部分,但我已将错误隔离到这个简短的示例中:

我在 Cisco IOS XE 设备上运行 ping 命令,并且 ping 需要一段时间才能完成。因此,为了避免不必要的超时,我将 'delay_factor = 5' 参数用于netmiko.ConnectHandler.send_command()

但是,我仍然收到错误:

[somsinha@cisco-cms.com@unlv1lnxjmpa01 pack_tests]$ time ./wo_thread.py 
Running Command: ping 1.1.1.1 so lo0 r 350 si 1400 df on R1
Traceback (most recent call last):
  File "./wo_thread.py", line 52, in <module>
    exec_cmd(v, 'ping 1.1.1.1 so lo0 r 350 si 1400 df')
  File "./wo_thread.py", line 30, in exec_cmd
    res += net_connect.send_command(cmd, delay_factor=5)
  File "/home/somsinha@cisco-cms.com/pyScripts/dev/devEnv/lib/python3.7/site-packages/netmiko/utilities.py", line 363, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "/home/somsinha@cisco-cms.com/pyScripts/dev/devEnv/lib/python3.7/site-packages/netmiko/base_connection.py", line 1488, in send_command
    search_pattern
OSError: Search pattern never detected in send_command_expect: R1#

real    0m23.647s
user    0m0.582s
sys     0m0.127s

由于 delay_factor 是 5,Netmiko 应该在抛出超时错误之前等待 5*100 = 500 secs = 3mins,但事实并非如此,因为我们看到:real 0m23.647s. 因此,它仅在抛出错误时执行 23 秒。此外,我尝试手动测试设备实际完成 ping 所需的时间。我发现:

R1#sh clock
06:41:31.334 UTC Sat Oct 17 2020
R1#ping 1.1.1.1 so lo0 r 350 si 1400 df
Type escape sequence to abort.
Sending 350, 1400-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
Packet sent with a source address of 145.55.234.7 
Packet sent with the DF bit set
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Success rate is 100 percent (350/350), round-trip min/avg/max = 47/53/190 ms
R1#sh clock                               
06:41:55.293 UTC Sat Oct 17 2020

正如您在上面看到的,开始和结束时间的实际差异是~24 secs- 所以没有理由这不应该工作。任何想法出了什么问题,伙计们?

我的代码是:

#!/usr/bin/python3
import os, sys
from netmiko import ConnectHandler
from pprint import pprint
from datetime import datetime, timedelta


dev = {
    "device_type": "cisco_xe",
    "host": '9.9.9.9',
    "username": 'user',
    "password": 'pass',
}


def exec_cmd(out, cmd):
    with ConnectHandler(**dev) as net_connect:
        if type(cmd) != list:
            d_name = net_connect.find_prompt()
            res = d_name + cmd + '\n'
            print(f"Running Command: {cmd} on {d_name.replace('#', '')}", end='', flush=True)
            res += net_connect.send_command(cmd, delay_factor=5)
            out.append(res)
            print('\x1b[2K\r', end = '') # Clear the old 'Running ...' Line
            print(f"Obtained result for {cmd}")
        else:
            out_lst = []
            for c in cmd:
                exec_cmd(out_lst, c)
            out.append(out_lst)


v = []
exec_cmd(v, 'ping 130.24.4.4 so lo0 r 350 si 1400 df')
print(v)
exit(1)
4

1 回答 1

1

原来是一个可能的错误。

见:https ://github.com/ktbyers/netmiko/issues/2008

使固定:

添加"fast_cli": False设备配置。完整代码:

dev = {
    "device_type": "cisco_xe",
    "host": '9.9.9.9',
    "username": 'user',
    "password": 'pass',
    "fast_cli": False,
}
于 2020-10-18T18:54:06.943 回答