我怎样才能运行这个函数?

问题描述

我正在尝试运行我的 python 代码,但第二个函数(见下文.. md5())不起作用!这是我需要解决错误吗?

**Connecting to device" 10.0.100.126 你想上传哪张图片?是的 你想给图像起什么名字? 测试 按回车继续 目标文件名 [测试]?正在上传.......................................

from netmiko import ConnectHandler
from rashhad import yml
from keyboard import press
import time
from pynput.keyboard import Key,Controller

from test import image_name

with open('dev_devices.txt') as f:
    devices_list = f.read().splitlines()

for ip_address in devices_list:
    print('___________________________________________________________\n')
    print('Connecting to device" ' + ip_address)
    ios_device = {
        'device_type': 'cisco_ios','ip': ip_address,'global_delay_factor': 4,"fast_cli": False,}

ios_device.update(yml('pass.yml','admin_a'))

cmd = "copy tftp://10.36.50.60/s2t54-ipservicesk9-mz.SPA.152-1.SY6.bin bootdisk:/"


def upload():
    net_connect = ConnectHandler(**ios_device)
    net_connect.enable()
    firmware = input("which image would you like to upload?")
    if firmware == "yes":
        name = input("what would you like to name the image?")
        output = net_connect.send_command(cmd + name,delay_factor=4,expect_string=r'Destination filename')
        input("press enter to continue")
        press('enter')
        print(output)
        print("UPLOADING.....................................")
        net_connect.send_command("wr")
        net_connect.disconnect()


upload()

time.sleep(20)


def md5(): # This function does not run for some reason,I have no idea
    net_connect = ConnectHandler(**ios_device)
    net_connect.enable()
    name = input("which image would you like to compare?")
    checksum = net_connect.send_command("verify /md5 bootdisk:" + name,delay_factor=4)
    print(checksum[-31:])
    net_connect.disconnect()



md5()

错误如下

Destination filename [testing]? 
UPLOADING.....................................
Traceback (most recent call last):
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\paramiko\channel.py",line 699,in recv
    out = self.in_buffer.read(nbytes,self.timeout)
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\paramiko\buffered_pipe.py",line 164,in read
    raise PipeTimeout()
paramiko.buffered_pipe.PipeTimeout

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\netmiko\base_connection.py",line 569,in _read_channel_expect
    new_data = self.remote_conn.recv(MAX_BUFFER)
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\paramiko\channel.py",line 701,in recv
    raise socket.timeout()
socket.timeout

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\rashhad.miah\Desktop\netauto\test.py",line 41,in <module>
    upload()
  File "C:\Users\rashhad.miah\Desktop\netauto\test.py",line 37,in upload
    net_connect.send_command("wr")
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\netmiko\utilities.py",line 430,in wrapper_decorator
    return func(self,*args,**kwargs)
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\netmiko\base_connection.py",line 1490,in send_command
    new_data = self.read_until_pattern(pattern=re.escape(cmd))
  File "C:\Users\rashhad.miah\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\netmiko\base_connection.py",line 651,in read_until_pattern
    return self._read_channel_expect(*args,line 579,in _read_channel_expect
    raise NetmikoTimeoutException(
netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel,data not available.

Process finished with exit code 1

解决方法

您的异常堆栈跟踪表明它在上传函数中的 wr 执行失败。

  File "C:\Users\rashhad.miah\Desktop\netauto\test.py",line 37,in upload
    net_connect.send_command("wr")

你甚至需要那个吗?

特别是它在 wr 命令的回显上失败,即 Netmiko 期望 wr 被回显,但它从未看到它:

packages\netmiko\base_connection.py",line 1490,in send_command
    new_data = self.read_until_pattern(pattern=re.escape(cmd))

您可能想查看 Netmiko session_log 或 Netmiko 的调试日志