#!/usr/local/bin/python3#Coding:utf-8# ====================================================#Author:chang-email:[email protected]#Last modified:2017-5-17#Filename:updateversion.py#description:file Upload, base Paramiko, sys, time#Blog:http://www.cnblogs.com/changbo# ===================================================="""1,close Tomcat 2,bakup website3,delete old Website4,start Tomcat"""ImportParamikoImport TimeImportsyswebsitelist= ['API']iplist= ['10.0.0.2','10.0.0.149','10.0.0.122']accpwd= {Iplist[0]:'abc-123', Iplist[1]:'abc-123', Iplist[2]:'abc-123'}basefile='/usr/local/tomcat'LocalPath='123.pdf'TargetPath='%s/webapps/%s'%(Basefile, websitelist[0])classsshsession:def __init__(Self, host, PWD, username='Root', port=22): Self.host=host Self.port=Port Self.username=username Self.pwd=pwddefConnect (self): transport=Paramiko. Transport ((Self.host, Self.port)) Transport.connect (username=self.username, password=self.pwd) self.__transport=TransportdefClose (self): self.__transport. Close ()defshutdown (self): SSH=Paramiko. Sshclient () Ssh._transport= self.__transport Print("[*] Tomcat would be shutdown ....") stdin, stdout, stderr= Ssh.exec_command (Basefile +"/bin/shutdown.sh") Time.sleep (2) stdin, stdout, stderr= Ssh.exec_command ("ps-axu|grep Tomcat |grep-v Grep|awk ' {print $} '") Tomcatpid=Stdout.read (). Decode ()ifTomcatpid! =0:Print('>>> Tomcat already shutdown!') Else: Print('>>> Tomcat not shutdown complete! would kill the tomcat PID') Ssh.exec_command ("kill-9"+tomcatpid)Print('>>> the Tomcat PID already killed!') defStartapp (self): SSH=Paramiko. Sshclient () Ssh._transport= self.__transport Print("[*] Tomcat would be the start, please check tomcat log ....") stdin, stdout, stderr= Ssh.exec_command (Basefile +"/bin/start.sh") Print(Stdout.read (). Decode ())defBackup (self): SSH=Paramiko. Sshclient () Ssh._transport= self.__transport Print('[*] bakup old site now, It'll take a few minutes .....') Ssh.exec_command ('"time= ' date +%m%d%h%m '' '&& Cd/usr/local/tomcat/webapps' '&& tar czf/home/websitebakup/webapps$time.tar.gz'+websitelist[0])Print('>>> the Web site backup complete!') defProgress_bar (self, transferred, tobetransferred, suffix="'): Bar_len= 60Filled_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 ('upload [%s]%s%s ...%s\n'% (bar, percents,'%', suffix)) Sys.stdout.flush ()defUploadFile (self, Local_path, Target_path): SSH=Paramiko. Sshclient () Ssh._transport= self.__transport Print('[*] Delete old site now, It'll take a few minutes .....') Ssh.exec_command ('mv-f/usr/local/tomcat/webapps/'+ websitelist[0] +'/ tmp') Print('>>> the Web site delete complete!\n') Print('[*] Clean tomcat Web cache now ....') Ssh.exec_command ("mv-f"+ Basefile +"/temp/*/tmp && mv-f"+ Basefile +"work/catalina/localhost/*/tmp") Print('>>> Tomcat Web cache already cleaned!') Print("[*] Upload website file to remote server ...") sftp= Paramiko. Sftpclient.from_transport (self.__transport) Sftp.put (Local_path, Target_path, callback=Self.progress_bar)if __name__=='__main__': I=Iplist[0]Try: SSH=sshsession (i, Accpwd[i]) ssh.connect () ssh.uploadfile (LocalPath, TargetPath) ssh.close () exceptException as E:Print(e)
Python's Auto release version