使用 Python 的 SCPClient 库在文件名中使用通配符 [英] Using wildcards in file names using Python's SCPClient library

查看:128
本文介绍了使用 Python 的 SCPClient 库在文件名中使用通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 脚本将文件从远程机器复制到我的本地机器.我只知道文件名的扩展名,所以我想使用通配符来表示文件名.

I want to copy files from remote machine using Python script to my local machine. I only know the extension of the file names so I want to use wildcards to express the file name.

此外,我想直接使用 SCPClient Python 库而不是 os.system,如题为 在python的scp文件名中使用通配符

In addition, I want to use the SCPClient Python library and not the os.system directly as suggested in the question titled using wildcards in filename in scp in python

但是当我运行以下代码时:

But when I run the following code:

from paramiko import SSHClient
import paramiko
from scp import SCPClient

with SSHClient() as ssh:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('10.10.100.5', username= 'root', password='Secret')
    with SCPClient(ssh.get_transport()) as scp:
        scp.get(remote_path='/root/*.py', local_path='.')

出现异常

scp.SCPException: scp:/root/*.py: 没有那个文件或目录

scp.SCPException: scp: /root/*.py: No such file or directory

从 shell 运行效果很好

Running from shell works just fine

scp root@10.10.100.5:/root/*.py .

scp root@10.10.100.5:/root/*.py .

推荐答案

您需要在 get_transport() 中添加 sanitize:

You need to add sanitize to your get_transport():

with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
        scp.get(remote_path='/root/*.py', local_path='.')

否则通配符按字面处理.

Otherwise wildcards are treated literally.

编辑 2020 年 9 月:对此答案存在一些混淆.上面的代码清理任何东西,但它提供了强制清理功能.Paramiko 没有我们现在要关闭的默认消毒剂.无论发生或不发生清理,都必须来自实施,因为 Paramiko 无法知道哪些路径在您的特定环境中是安全的.

EDIT Sep 2020: There has been some confusion about this answer. The above code does not sanitise anything but it provides the mandated sanitisation function. Paramiko does not have a default sanitiser we would now be turning off. Whatever sanitisation happens or does not happen must come from the implementation, as there is no way for Paramiko to know which paths are safe in your particular environment.

如果根本不存在清理功能,则所有通配符都按字面意思解释-出于安全原因,我假设通配符对于不受信任的输入是危险的.该设计通过添加某种清理功能来强制实现考虑清理.如果没有,通配符就不能用作通配符.

If there is no sanitising function present at all, all wildcards are interpreted literally - I assume for security reasons as wildcards are dangerous with untrusted input. The design forces the implementation to consider sanitisation by adding a sanitising function of some kind. If there is none, wildcards do not work as wildcards.

为了使通配符起作用,需要有一个返回清理路径的清理函数.上面的 lambda 是一个虚拟函数,它返回输入给它的任何东西——这意味着它根本不做任何清理工作,但由于现在有一个清理函数,通配符现在开始作为通配符工作.

To make wildcards work, there needs to be a sanitising function that returns the sanitised path. The above lambda is a dummy function that returns whatever is fed to it - which means it does not do any sanitisation at all but as there now is a sanitising function, wildcards start now working as wildcards.

如果您不相信自己的输入,不要这样做.在这种情况下,您需要编写适当的消毒剂以确保路径可以安全使用.您将需要编写一个返回清理路径的函数.

If you do not trust your inputs, do not do this. In that case you need to write a proper sanitiser that will make sure the path is safe to use. You will need to write a function that returns the sanitised path.

这篇关于使用 Python 的 SCPClient 库在文件名中使用通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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