List files on SFTP server matching wildcard in Python using Paramiko

前端 未结 2 768
误落风尘
误落风尘 2020-12-18 07:22
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(\'hostname\', username=\'test1234\', passw         


        
相关标签:
2条回答
  • 2020-12-18 07:48

    Or use pysftp which is paramiko wrapper and write something like this:

    import pysftp
    
    
    def store_files_name(fname):
        pass
    
    
    def store_dir_name(dir_name):
        pass
    
    
    def store_other_file_type(other_file):
        pass
    
    with pysftp.Connection('server', username='user', password='pass') as sftp:
        sftp.walktree('.', store_files_name, store_dir_name, store_other_file_type)
    
    0 讨论(0)
  • 2020-12-18 07:51

    glob will not magically start working with a remote server, just because you have instantiated SSHClient before.

    You have to use Paramiko API to list the files, like SFTPClient.listdir:

    import fnmatch
    
    sftp = client.open_sftp()
    
    for filename in sftp.listdir('/home/test'):
        if fnmatch.fnmatch(filename, "*.txt"):
            print filename
    

    Side note: Do not use AutoAddPolicy. You lose security by doing so. See Paramiko "Unknown Server".

    0 讨论(0)
提交回复
热议问题