ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.31k forks source link

SCP error with large files #3052

Closed weiks80 closed 1 year ago

weiks80 commented 1 year ago

Hey Kirk, I'm hitting an issue that if i try to use the file_transfer function to transfer large files (IOS images) the script fails once the file transfer completes. Based on what i can see on the switch side I think the file_transfer function is opening a separate connection to the switch/router for the copy and the original connection goes stale and is closed by the switch. Once the transfer is complete netmiko comes back to the original connection to do it's verification tasks, but the connection is closed and it errors out. Is there any way to send a periodic "\n" on the original connection to keep it active while the file copy is in progress? FWIW the file copy itself completes fine, and a manual verify /md5 shows that it completed without error.

When i use the same code to copy a smaller file i have no issues.

` import netmiko from netmiko import ConnectHandler, file_transfer import logging logging.basicConfig(filename='test.log', level=logging.DEBUG) logger = logging.getLogger("netmiko")

conn_opts = { 'device_type':'cisco_xe', 'ip':'10.10.10.1', 'username':'admin', 'password':'password', 'fast_cli': False, 'secret':'password'}

sshconn = netmiko.ConnectHandler(**conn_opts)

source_file = "cat9k_iosxe.16.12.05b.SPA.bin" dest_file = "cat9k_iosxe.16.12.05b.SPA.bin" direction = "put"

transfer_dict = file_transfer( sshconn, source_file=source_file, dest_file=dest_file, file_system="flash:", direction=direction, overwrite_file=True, ) ` netmiko.log

ktbyers commented 1 year ago

Have you set the VTY timeout on the device so that the SSH control channel doesn't get disconnected?

weiks80 commented 1 year ago

I just tried that on a test device and it worked.

Unfortunately, I don't think i can use that option in our prod environment since the VTY timeout is part of our security policy. Is tehre a way to keep it active within Netmiko?

ktbyers commented 1 year ago

Can you just set it to be a large value as part of the process and then reset it back to original value after the transfer?

Look into the SSH-keepalive that might work.

Also you could catch the exception and do the MD5 after the fact (programmatically).