Paramiko scp copy from remote machine regular expression

后端 未结 2 471
感动是毒
感动是毒 2021-01-15 06:42

Is there way i can copy remote files that ends with name \"output\" using paramiko scp.

I have below code, which copies only if i provide full path or exact file nam

相关标签:
2条回答
  • 2021-01-15 07:18

    Maybe need to use ssh to get list first, then scp them one by one.

    Something like follows, just FYI.

    def get_copy(self, hostname, dst):
        ssh = createSSHClient(hostname)
    
        stdin, stdout, stderr = ssh.exec_command('ls /home/username/*output')
        result = stdout.read().split()
    
        scp = SCPClient(ssh.get_transport())
        for per_result in result:
            scp.get(per_result)
        scp.close()
        ssh.close()
    
    0 讨论(0)
  • 2021-01-15 07:24

    There are two more ways which I found useful in this context.

    1) You may also do it without using SCPClient, but just Paramiko itself. Like -

    def get_copy(self, hostname, dst):
        ssh = createSSHClient(hostname)
        sftp = ssh.open_sftp()
        serverfilelist = sftp.listdir(remote_path)
        for f in serverfilelist:
                if re.search("*output", f):
                    sftp.get(os.path.join(remote_path, f), local_path)
    
        ssh.close()
    

    2) If you want to use SCPClient to SCP files using regex(wildcards), THIS link will be helpful, I think.

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