繁体   English   中英

如何在整个目录中查找具有特定扩展名的文件,然后将其复制到另一台服务器

[英]How to look for a file with a particular extension in the entire directory and then copy to another server

我正在尝试复制具有特定扩展名的文件。 假设3gp

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

我需要查看整个目录中可用的3gp文件名,然后使用sftp将它们复制到服务器。

[root@DVRTPG009 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"/> 
$ find /location/of/directory -name '*.3gp' -exec scp {} username@remoteip:/remote/location/ \;

如果您需要一个python脚本来为您执行此操作。 我使用了python paramiko,scp,寻找有关如何使用它们的干净文档。

pip install scp
easy_install paramiko

在我的PC(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)

虽然需要一些修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM