#!/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!