• python 下载远程日志


    # coding:utf-8
    
    import paramiko
    import time
    import sys
    
    # Create your views here.
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-06-06
    # Filename: updateversion.py
    # Description: file upload ,base paramiko, sys, time
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    iplist = ['x.x.x.x']
    accpwd = {iplist[0]: 'xxxxx'}
    basefile = '/home/changbo'
    localpath = '/root/access.tar.gz'
    filename = 'access.tar.gz'
    # targetpath = '%s' % basefile
    targetpath = '%s/%s' % (basefile, filename)
    
    
    class SSHSession:
        def __init__(self, host, pwd, username='changbo', port=22):
            self.host = host
            self.port = port
            self.username = username
            self.pwd = pwd
    
        def connect(self):
            transport = paramiko.Transport((self.host, self.port))
            transport.connect(username=self.username, password=self.pwd)
            self.__transport = transport
    
        def close(self):
            self.__transport.close()
    
        def progress_bar(self, transferred, toBeTransferred, suffix=''):
            bar_len = 60
            filled_len = int(round(bar_len * transferred / float(toBeTransferred)))
            percents = round(100.0 * transferred / float(toBeTransferred), 1)
            bar = '=' * filled_len + '-' * (bar_len - filled_len)
            sys.stdout.write('Download [%s] %s%s ...%s
    ' % (bar, percents, '%', suffix))
            sys.stdout.flush()
    
        def getloadfile(self, target_path, local_path):
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            sftp = paramiko.SFTPClient.from_transport(self.__transport)
            print('Download remote log file :')
            sftp.get(target_path, local_path, callback=self.progress_bar)
    
    
    try:
        ssh = SSHSession(iplist[0], accpwd[iplist[0]])
        ssh.connect()
        ssh.getloadfile(targetpath, localpath)
    except Exception as e:
        print(e)
    

     END!

  • 相关阅读:
    Deep Learning ——Yann LeCun,Yoshua Bengio&Geoffrey Hinton
    numpy模块
    损失函数、正则化、优化
    最近邻分类器,K近邻分类器,线性分类器
    python json,pickle模块
    python os,sys模块
    python random模块
    到2030年,人工智能会发展成什么样?
    AI的偏见:机器就是绝对理性的么?
    5G和AI:现在和未来的互补技术
  • 原文地址:https://www.cnblogs.com/changbo/p/6951556.html
Copyright © 2020-2023  润新知