Netmiko OSError:在发送命令\u expect:Destination中从未检测到搜索模式

2024-10-03 11:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

实际输出:

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行,在 输出=上传更新(模型,版本[0],版本[1],版本[2],版本[3])#发送到func{'CSR1000V':['12.2.55', 文件“/svu.py”,第119行,上传更新 output=connection.send_命令(commandsend,期望_字符串=r'Destination filename') 文件“/home/repos/public/Python/lib/python3.6/site packages/netmiko/base_connection.py”,第1112行,在send_命令中 搜索(U模式) OSError:send_命令中从未检测到搜索模式\u expect:Destination filename

我试着使用下面的方法,但仍然不起作用

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

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


Tags: 版本sendoutputstringbinconnectionfilenameout
1条回答
网友
1楼 · 发布于 2024-10-03 11:19:47

你的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命令,该命令将禁用此类提示

相关问题 更多 >