Python's Auto release version

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.