来自远程机器正则表达式的 Paramiko scp 副本 [英] Paramiko scp copy from remote machine regular expression

查看:53
本文介绍了来自远程机器正则表达式的 Paramiko scp 副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 paramiko scp 复制以名称output"结尾的远程文件.

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

下面是代码

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

我正在尝试的是

     get_copy(1.1.1.1, "*output")

我收到文件未找到错误

推荐答案

可能需要先用 ssh 获取 list,然后再 scp 一个一个.

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

类似如下,仅供参考.

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

这篇关于来自远程机器正则表达式的 Paramiko scp 副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆