2016-12-01 3 views
0

Ich versuche, Dateien mit einer bestimmten Erweiterung zu kopieren. Sagen wir 3gp.So suchen Sie nach einer Datei mit einer bestimmten Erweiterung im gesamten Verzeichnis und dann auf einen anderen Server kopieren

Angenommen, die Datei anc1.xml hat den Namen g711u.3gp.

Ich muss in das gesamte Verzeichnis für 3gp Dateinamen suchen und kopieren Sie sie dann auf einen Server mit sftp.

[[email protected] regression]# cat annc1.xml | grep 3gp 
        <audio uri="http://10.211.0.159/mxml_clips/10_SecondClip_amr.3gp"/> 
      <send target="source" event="app.10_SecondClip_amr.3gp" namelist="play.amt play.end"/> 
+0

[Stamm @ DVRTPG009 Regression] # cat annc1.xml | grep 3gp

Antwort

0

Im Fall, dass Sie einen Python-Skript, das für Sie tun. Ich verwendete Python Paramiko, scp, suchen Sie nach einer sauberen Dokumentation auf, wie man sie benutzt.

pip install scp 
easy_install paramiko 

Testen Sie auf meinem PC (Ubuntu 16.04) 64bit. Sie müssen kleine Änderungen vornehmen: Servername, Benutzername, Passwort und Remote-Verzeichnis, in das die Dateien hochgeladen werden sollen.

import os 
import sys 
import glob 
import fnmatch 
import paramiko # for ssh may need to be installed. 
from scp import SCPClient 
import time 

# This may work on both linux and window or mac. 
DEFAULT_DIR=os.path.expanduser("~") 

server="192.168.0.100" 
port=22 
user="s8software" 
password="!your-password.com!" # may not bee needed when we use public key something.pem 
SFTP_REMOTE_PATH="/home/s8software/Documents/" 

def search_directory_for_files(dirname, extension="*.py"): 
    """ 
    Search an entire directory for particular files 
    denoted with extension. and copy them to a remote server. 
    @param: dirname--> The directory contains files we want to copy. 
    @param: extension--> The files with a particular extension we want to copy. 

    """ 
    if (not dirname): 
     raise AttributeError("We need at least a source directory!") 

    for root, direname, filenames in os.walk(dirname): 
     for filename in fnmatch.filter(filenames, extension): 
      yield {"fullpath":os.path.join(root, filename), "filename":filename} 

def open_scp_connection(server, port, user, password): 
    """ 
    Function returns a well established connection to the 
    server we want to transfer files to. 
    """ 
    try: 
     client = paramiko.SSHClient() 
     client.load_system_host_keys() 

     # Note the public key here don't. 
     # TODO. See how you can either pass the username and password. 

     pkey = "/home/s8software/Working/Keys/KeParis/TestInstance.pem" 
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

     # NOTE: if you use the public_key then uncomment this line and comment 
     # the one using the password. 
     #client.connect(server, port, user, key_filename=pkey) 

     client.connect(server, port, user, password=password) 

     return client 

    except: 
     raise 

def get_scpclient_connection(ssh_object): 
    try: 
     return SCPClient(ssh_object.get_transport()) 
    except: 
     raise 

def _send_remote_recursive(scp_client, file_full_path, filename): 
    """ 
    Given a file send them to the remote server. 
    """ 
    try: 
     print time.asctime(), 'Uploading....', filename 
     return_status = scp_client.put(file_full_path, remote_path=os.path.join(SFTP_REMOTE_PATH, filename))   
    except: 
     raise 
    else: 
     return return_status 

if __name__=="__main__": 
    # First open the connection using the port of the server 
    endpoint_connection = open_scp_connection(server, port, user, password) 
    scp_connection = get_scpclient_connection(endpoint_connection) 

    # Recursely get everything we need and send to remove server. 
    for f in search_directory_for_files(DEFAULT_DIR): 
     source_path = f.get("fullpath") 
     filename = f.get("filename") 

     # Send the files to the remote server. 
     _send_remote_recursive(scp_connection, source_path, filename) 

Eine kleine Änderung obwohl ich gebraucht werden.

Verwandte Themen