2016-12-01 27 views
0

我试图复制具有特定扩展名的文件。比方说3gp如何在整个目录中查找具有特定扩展名的文件,然后复制到另一台服务器

例如,假设文件annc1.xml具有g711u.3gp文件名。

我需要查看整个目录中的3gp文件名,然后使用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

[root @ DVRTPG009 regression]#cat annc1.xml | grep 3gp 01​​

回答

0

如果你需要一个Python脚本来为你做的。 我使用python paramiko,scp,查找关于如何使用它们的干净文档 。

pip install scp 
easy_install paramiko 

测试我的电脑(Ubuntu 16.04)64位。您需要对要将文件上载到的更改,服务器名称,用户名,密码和远程目录进行更改。

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) 

虽然我需要一点修改。

相关问题