question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Secure Copy with Progressbar

See original GitHub issue

Hello @ktbyers,

I am trying to improve my code and add the progressbar to the file_transfer function. I was wondering how can I calculate the sent argument? I found this answer on SOF and this demo on GitHub, but I can’t figure out how to use it with my provided code.

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?
        ),
    )

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ktbyerscommented, Dec 25, 2021

Okay, nice…yeah, we should update Netmiko’s default progress bar to use “rich”.

1reaction
Tes3awycommented, Dec 25, 2021

@Tes3awy Did you work out a solution?

Finally, I had the time to find a pretty method of printing the progress in terminal.

from rich import print

def progress_bar(filename, size, sent, peername):
    print(
        f"[blue]{filename.decode('ascii')} progress[/blue] on {peername[0]}:{peername[1]}: {float(sent) / float(size) * 100:.1f}%",
        end=" \r",
        flush=True,
    )

This is actually the same as in the scp.py README but using print() from rich instead of sys.stdout.write() to have a colored output in the terminal.

The only drawback of using end=" \r" is that the print statement after the progress_bar print statement overwrites it.

UPDATE:

A simple workaround to keep the progress bar print statement in the terminal and not to be overwritten by the next print statement is to prepend a \n to the latter.

from rich import print

def progress_bar(filename, size, sent, peername):
    print(
        f"[blue]{filename.decode('ascii')} progress[/blue] on {peername[0]}: {float(sent) / float(size) * 100:.1f}%",
        end=" \r",
        flush=True,
    )

print(f"\n[green]Transferred {bin_file} to host successfully")
file.bin progress on 192.168.1.1: 100.0% 
Transferred file.bin to host successfully
Read more comments on GitHub >

github_iconTop Results From Across the Web

Progress bar for scp command - ssh - Ask Ubuntu
@fuero man scp : " -3 ​ ​ ​ ​ Copies between two remote hosts are transferred through the local host. Without this...
Read more >
How to use the SCP command in Linux - BudgetVM
Short for Secure Copy, the SCP command copies files and ... option to suppress the progress bar as data is being copied to...
Read more >
Advanced Copy - Shows Progress While Copying Files in Linux
Advanced-Copy is a command line program that is very much similar, ... Advanced Copy Command – Shows Progress Bar While Copying/Moving Files ...
Read more >
c# - File Copy with Progress Bar - Stack Overflow
I wanted to continuously increment the value of the progress bar while copying, especially large files. What happens in this sample code is...
Read more >
Progress bar in SCP transfers #938 - ktbyers/netmiko - GitHub
Any plans to add a progress bar to the SCP transfers? I ask because you're using the package, and he gives an example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found