• python之paramiko密钥库+密码登录


    #!/usr/local/anaconda3/bin/python
    # coding:utf-8
    
    # Create your views here.
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-07-11
    # Filename: downloadrem.py
    # Description: file upload ,base paramiko, sys, time
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    """
    1, 每日晚1:30分执行webserver控制台和项目日志的备份,脚本Path:/home/yunwei/jb/mvlogs.sh
    2,每日凌晨2:30分执行移动tar以外的已打包日志到回收站/tmp/trash(回收站定期)
    3,将保存的日志回传到本地各相关站点目录下
    4,每天22:30清空回收站
    5,初始化用户环境alias rm='mv --verbose -f --backup=numbered --target-directory /tmp/trash'
    6,find -not -name '*.tar.gz' |xargs mv -f --backup=numbered -t /tmp/trash/ 2>/dev/null
    """
    
    import paramiko
    import time
    import sys
    import threading
    
    
    ipList = ['xx.x.x.x']
    # accPwd = {ipList[0]: 'xxxxxxxxxx'}
    
    
    bakTime = time.strftime('%Y%m%d')
    filename = 'remdown' + bakTime + '.tar.gz'
    
    logPath = '/data'
    locHome = logPath + '/hkdaex/home/' + filename
    locTrade = logPath + '/hkdaex/trade/' + filename
    
    remHome = logPath + '/homelogs/' + filename
    remTrade = logPath + 'tradelogs/' + filename
    
    private_key = "F:\testcode\id_rsa"
    
    
    class SSHSession:
        def __init__(self, host, username='xxxxx', port=22):
            self.host = host
            self.port = port
            self.username = username
            # self.pwd =
    
        def connect(self):
            transport = paramiko.Transport((self.host, self.port))
            key = paramiko.RSAKey.from_private_key_file(private_key, 'xxxxxxxxxx')
            transport.connect(username=self.username, pkey=key)
            self.__transport = transport
    
        def close(self):
            self.__transport.close()
    
        def progressbar(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):u*TB4oCjdUZb2%m6
        def getloadfile(self):
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            stdin, stdout, stderr = ssh.exec_command('ls')
            print(stdout.read().decode())
            # sftp = paramiko.SFTPClient.from_transport(self.__transport)
            # print('Download remote log file :')
            # sftp.get(target_path, local_path, callback=self.progressbar)
            return 'success'
    
    
    def execDown():
        # try:
            ssh = SSHSession(ipList[0])
            ssh.connect()
            # status = ssh.getloadfile(remHome, locHome)
            # print(status)
            # status2 = ssh.getloadfile(remTrade, locTrade)
            status = ssh.getloadfile()
            print(status)
            ssh.close()
        # except Exception as e:
        #     print(e)
    
    if __name__ == '__main__':
        t1 = threading.Thread(target=execDown)
        t1.start()

    END!

  • 相关阅读:
    【大话设计模式】——浅谈设计模式基础
    mongodb入门安装
    Unity3D研究之多语言用中文做KEY
    java7新特性之Try-with-resources (TWR)
    ORACLE取周、月、季、年的開始时间和结束时间
    topas命令详解
    topas解析(AIX)
    top(topas),vmstat,iostat在linux和AIX操作系统下显示情况
    怎么检查网站的死链接呢?
    网站死链接检测查询工具
  • 原文地址:https://www.cnblogs.com/changbo/p/7149878.html
Copyright © 2020-2023  润新知