python实现远程scp文件

import sys
import os,re
import pipes
import subprocess
from subprocess import *
from config import * //参数可配置化
import random
from multiprocessing import Pool
def copy_log(log_path_2,ssh_host,file):  //拷贝文件函数
    os.environ['ssh_host'] = str(ssh_host)
    os.environ['file'] = str(file)
    os.environ['log_path_2'] = str(log_path_2)
    os.system("scp -l 100000 ${ssh_host}:${file} ${log_path_2}/")
    os.system("chmod 777 ${log_path_2}/${log}")

pool = Pool(2)  //开启多进程
def scp_log(date,log,hj,Date,App):
    if hj == "bx" :
        log_path =log_path_bx +App + '/' + date + '/' +Date
        #ssh_host = " ".join(random.sample(ssh_host_bx,1))
        print log_path
        #print ssh_host
    if hj == "zj" :
        log_path =log_path_zj + App + '/' + date
        #ssh_host = " ".join(random.sample(ssh_host_zj,1))
        print log_path
        #print ssh_host
    file = log_path + '/' + log
    log_file = log_path_2 + '/' + log
    resp = subprocess.call( ['ssh',ssh_host, 'test -e ' + pipes.quote(file)] )  //检查远程服务器文件是否存在
    print "subprocess..."
    if ( 0 == resp ):
        if (os.access(log_file,os.F_OK)==True):
            res =  "log is exists "
            #print res
            return res;
        else:
            du = Popen( ['ssh',ssh_host, 'du -hm ' + pipes.quote(file)],stdout=PIPE,stdin=PIPE) //查看文件大小
            s = du.stdout.read()
            l = s.split('\t')
            size = int(l[0])
            time =(size/12)/60
            res = "文件将在"+str(time)+"分钟后拷贝完成"
            #print res
            pool.apply_async(func=copy_log,args=(log_path_2, ssh_host, file,))
            return res

    else:
        res = ('{} does not exist '.format(log))
        return res
        #print res

你可能感兴趣的:(python)