Python Netmiko OSError:在send_command_expect中未检测到搜索模式:

时间:2020-03-19 18:45:56

标签: python python-3.x netmiko

我最近开始使用Python进行编程。我是网络工程师,目前正在构建程序以从Ciena设备中提取“状态转储”。我使用netmiko连接到设备。 现在,我总是收到以下错误:

OSError:在send_command_expect中未检测到搜索模式:5160_1>

“ 5160_1>”是交换机上的主机名/提示符。我已经读过我可以将“ expect_string”赋予“ send_command”。不幸的是,这没有任何效果,我仍然会收到此错误。

这是我用来创建状态转储并调用文件下载功能的函数。

def create_sd():
    for ip in sw_connect_ips:
        try:
            sw_connection = ConnectHandler(device_type=sw_dev_type, host=ip, username=sw_usr, password=sw_pw)
            try:
                print('\nconnnected to host > ' + ip + '\n')
                hostname = sw_connection.find_prompt()
                print('hostname of device > ' + hostname)
                sw_connection.send_command('system server sftp enable', expect_string=hostname)
                sw_connection.send_command('configuration save', expect_string=hostname)
                sw_output = sw_connection.send_command('system state-dump file-name ' + ip + '_sd_timestamp_' + str(date.hour) + '_' + str(date.minute) + '_' + str(date.second), expect_string=hostname + ' ')
                filename = ip + '_sd_timestamp_' + str(date.hour) + '_' + str(date.minute) + '_' + str(date.second)
                print('got state-dump ' + filename + ' from host > ' + ip)
                logging.debug(sw_output)
                logging.debug(sw_connection)
                sw_connection.disconnect()
                try:
                    sftp_get(filename, ip)
                except:
                    raise Exception('scp failed')
            except:
                raise Exception('command does not exist on switch > ' + ip)
        except:
            raise SSHException('unable to connect to switch check username, password and ip address')

我不知道所有例外是否都有意义。也许有人给我小费。

谢谢。

编辑:我认为奇怪的是,它仅在某些开关下发生。

1 个答案:

答案 0 :(得分:1)

在创建状态转储的命令中,我将“ send_command”延迟因子增加到了5。

sw_output = sw_connection.send_command ('system state-dump file-name' + ip + '_sd_timestamp_' + str (date.hour) + '_' + str (date.minute) + '_' + str (date.second) , expect_string = hostname, delay_factor = 5)

结果,我不再遇到netmiko异常,程序运行没有问题。