# coding:utf-8import Paramikoimport timeimport sys# Create your views here.# ========================================== ==========# author:chang-email:[email protected]# 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 '% Basefiletargetpath = '%s/%s '% (basefile, filename) class SSH Session: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.__TRANSP ORT = Transport Def close (self): self.__transport.close() def progress_bar (self, transferred, tobetransferred, suffix= "): Bar_len = Filled_len = Int (round (b Ar_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\n '% (bar, p ercents, '% ', 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.getloadfil E (TargetPath, localpath) except Exception as E:print (e)
end!
Python Download remote log