Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

ssh - Using wildcards in file names using Python's SCPClient library

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.

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='.')

I get an exception

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

Running from shell works just fine

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...