0

我正在尝试改进我的代码并将进度条添加到file_transfer函数中。我想知道如何计算sent参数?我在这里GitHub 上找到了这个答案,但我不知道如何将它与我提供的代码一起使用。

from os.path import getsize

from netmiko import ConnectHandler, file_transfer, progress_bar

router = {
    "device_type": "cisco_ios",
    "host": "sandbox-iosxe-latest-1.cisco.com",
    "username": "developer",
    "password": "C1sco12345",
    "port": 22,
    "verbose": True,
    "conn_timeout": 12,
    "fast_cli": False,
    "session_log": "sandbox-iosxe-latest-1.cisco.com.log",
}

src_file = dest_file = input("Name of file to copy: ")

with ConnectHandler(**router) as net_connect:
    scp = net_connect.send_config_set(config_commands=["ip scp server enable"])
    transfer = file_transfer(
        net_connect,
        source_file=src_file,
        dest_file=dest_file,
        file_system="flash:",
        direction="put",
        overwrite_file=True,
        socket_timeout=100.0,
        progress=progress_bar(
            filename=src_file,
            size=getsize(src_file),
            sent=sent,  # How to calculate? What should be placed here?
        ),
    )
4

1 回答 1

1

应该只是progress=progress_baror progress4=progress_bar。您只需提供一个以某种方式构造的可调用对象(并且您正在使用 Netmiko 提供的 progress_bar 函数,这样应该没问题)。

这是一个使用它的示例 file_transfer:

ssh_conn = ConnectHandler(**cisco)
transfer_dict = file_transfer(
    ssh_conn,
    source_file=source_file,
    dest_file=dest_file,
    file_system=file_system,
    direction=direction,
    # Force an overwrite of the file if it already exists
    overwrite_file=True,
    progress4=progress_bar,
)
于 2021-07-15T00:27:59.430 回答