Skip to content

Instantly share code, notes, and snippets.

@mariusavram91
Last active June 4, 2023 11:14
Show Gist options
  • Save mariusavram91/d84ce89645f5215a9c0b to your computer and use it in GitHub Desktop.
Save mariusavram91/d84ce89645f5215a9c0b to your computer and use it in GitHub Desktop.
Copy remote files to local with Python's Paramiko
import os
import paramiko
paramiko.util.log_to_file('/tmp/paramiko.log')
paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
host = 'local'
port = 22
username = 'user'
files = ['file1', 'file2', 'file3', 'file4']
remote_images_path = '/remote_path/images/'
local_path = '/tmp/'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=port, username=username)
sftp = ssh.open_sftp()
for file in files:
file_remote = remote_images_path + file
file_local = local_path + file
print file_remote + '>>>' + file_local
sftp.get(file_remote, file_local)
sftp.close()
ssh.close()
@abstruse123
Copy link

abstruse123 commented Feb 1, 2019

Hi! I ran this in Python 3.4. Got the following error:

line 20, in <module>
   for file in file:
NameError: name 'file' is not defined

@loictim
Copy link

loictim commented Feb 14, 2019

Hi,
It's due to a typo, replace this line
for file in file:
by
for file in files:
and it should work.

Regard,

@FirasKORKAD
Copy link

Hello,
I tried to run the script on windows but there are several errors.
Could you give me the changes to make the script work on windows.
Best regards.

@jccbbb
Copy link

jccbbb commented Apr 21, 2019

My SSH requires password. How could the code be updated to support login to a SSH server with password?

@mdrijwan123
Copy link

My SSH requires password. How could the code be updated to support login to a SSH server with password?

ssh.connect(hostname=host, port=port, username=username,password='Your password')

@Sreevalli961
Copy link

I did the same thing as you except username ,host and password but i am not able to copy the files

@balajiee007
Copy link

Works Perfect! Thank you so much for sharing.

@mmani360
Copy link

how to fetch latest or oldest ones using paramiko/SFTP client library? pls share code samples for that. I am traversing directories, and at any given path, have to fetch file that arrived earliest.

@OrestisRo
Copy link

Hi! I ran this in Python 3.4. Got the following error:

line 20, in <module>
   for file in file:
NameError: name 'file' is not defined

Did you even read the error? I died inside a bit by your laziness.

@OrestisRo
Copy link

My SSH requires password. How could the code be updated to support login to a SSH server with password?

https://lmgtfy.com/?q=paramiko+documentation+ssh+password

@rajeshpolinati
Copy link

Hi All,

Any script to copy files from local to remote system using Python's Paramiko

@adibyte95
Copy link

how to put directory to a remote location

@smandal7
Copy link

smandal7 commented Aug 28, 2020

How to download multiple files with like option(*) ?

abc.I*.today's date.input

@sreekumar360
Copy link

wrong print command

@msaad2209
Copy link

Hey,
Thanks for this code, wanted to copy any file which start with some specific string, for example my sftp has serval projects files and all projects files are saved as project_number and some file name and extension, I want to loop all the files of same project number, can you please guide?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment