【问题标题】:Netmiko OSError: Search pattern never detected in send_command_expect: DestinationNetmiko OSError:在 send_command_expect 中从未检测到搜索模式:目标
【发布时间】:2021-08-03 05:56:16
【问题描述】:

我在这里被困了一个多小时,但似乎无法找到我的问题的解决方案。问题是我无法完全匹配字符串输出。

实际输出:

hostname#$192.168.1.1/out/c2960x-universalk9-mz.152-7.E3.bin flash:c2960x-universalk9-mz.152-7.E3.bin
Destination filename [c2960x-universalk9-mz.152-7.E3.bin]? 

我的代码不工作:

commandsend = "copy ftp://206.112.194.143/out/c2960x-universalk9-mz.152-7.E3.bin flash:c2960x-universalk9-mz.152-7.E3.bin"
output = connection.send_command(commandsend,expect_string=r'Destination filename')
output += connection.send_command('\n',expect_string=r'#')

复制ftp://x.x.x.x/out/c2960x-universalk9-mz.152-7.E3.binflash:c2960x-universalk9-mz.152-7.E3.bin 格林威治标准时间 21:11:27.067 2021 年 5 月 12 日星期三 回溯(最近一次通话最后): 文件“./svu.py”,第 292 行,在 output = uploading_update(models,ver[0],ver[1],ver[2],ver[3]) # 发送到 func {'CSR1000V': ['12.2.55', 文件“./svu.py”,第 119 行,在 uploading_update 中 output = connection.send_command(commandsend,expect_string=r'目标文件名') 文件“/home/repos/public/Python/lib/python3.6/site-packages/netmiko/base_connection.py”,第 1112 行,在 send_command 搜索模式)) OSError:在 send_command_expect 中从未检测到搜索模式:目标文件名

我尝试使用以下方法,但仍然无法正常工作

output = connection.send_command(commandsend,expect_string=r'Destination.*')
output = connection.send_command(commandsend,expect_string='Destination.*')
output = connection.send_command(commandsend,expect_string=r'Destination filename.+')

我什至尝试调整延迟因子和 fast_cli=False 但还是一样。

当我尝试使用确切的输出时。我看到以下错误。

output = connection.send_command(commandsend,expect_string=r'Destination filename [c2960x-universalk9-mz.152-7.E3.bin]? ')

第 27 位的字符范围错误 x-u

我这是一个错误还是什么?我需要使用任何缺少的选项吗?

【问题讨论】:

  • 您是否在设备上设置了ip ftp source-interface <interface>

标签: python python-3.x python-2.7 netmiko


【解决方案1】:

您的expect_string=r'Destination filename [c2960x-universalk9-mz.152-7.E3.bin]? ' 的问题在于,Netmiko 将expect_string 参数解释为Regular Expression (regex) 字符串,因此[] 括号内的内容被视为您正在尝试的字符范围匹配,这就是错误的来源。 在您的情况下,您希望匹配确切的 [] 字符,因此您需要在“原始”字符串中使用 \ 或在“正常”字符串中使用 \\ 来转义它们。这同样适用于?. 字符,它们在正则表达式中具有特殊含义。

因此正确的命令是:

output = connection.send_command(commandsend,expect_string=r'Destination filename \[c2960x-universalk9-mz\.152-7\.E3\.bin\]\? ')

您可以使用regex101 等在线正则表达式工具来帮助您构建和测试您的正则表达式。

另外,如果您正在与 Cisco 打交道,我认为您应该查看 file prompt quiet 命令,它会禁用这些提示。

【讨论】: