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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值