SSH [SOLVED]: Using wildcards in file names using Python's SCPClient library

SSH [SOLVED]: Using wildcards in file names using Python's SCPClient library

Home Forums SSH SSH [SOLVED]: Using wildcards in file names using Python's SCPClient library

Tagged: , , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36535

    Anonymous

    QuestionQuestion

    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 .

    #36536

    Anonymous

    Accepted AnswerAnswer

    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.

    Source: https://stackoverflow.com/questions/47926123/using-wildcards-in-file-names-using-pythons-scpclient-library
    Author: Hannu
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.