Search code examples
pythonpython-2.7paramikoscp

Paramiko scp copy from remote machine regular expression


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 name

Below is code

 import paramiko
 import os
 from paramiko import SSHClient
 from scp import SCPClient


def createSSHClient(self, server):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(server, self.port, self.user, self.password)
    return client

  def get_copy(self, hostname, dst):
    ssh = self.createSSHClient(hostname)
    scp = SCPClient(ssh.get_transport())
    scp.get(dst)
    scp.close()

What am trying is

     get_copy(1.1.1.1, "*output")

I am getting file not found Error


Solution

  • 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()