#!/usr/bin/python # coding:utf-8 import paramiko from scp import SCPClient # !/usr/bin/python # coding:utf-8 import paramiko # paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接;此模块需要pip install paramik进行安装 from scp import SCPClient # 使用SCPClient实现文件上传,文件下载等 import os import time def auto(): os.chdir(r'D:\TOMCAT\publishing-web\bin') # 修改当前工作目录,原因是执行startup.bat时已当前脚本为工作目录导致启动startup。bat失败 command_list = [r'start D:\TOMCAT\publishing-web\bin\shutdown.bat', r'move /-y D:\TOMCAT\publishing-web\webapps\ROOT D:\TOMCAT\publishing-web\temp\ROOT_back%s' % time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()), r'del D:\TOMCAT\publishing-web\webapps\ROOT.war'] for command in command_list: if os.system(r'tasklist | find "java.exe"') == 0: os.system(command) elif os.path.exists('D:\TOMCAT\publishing-web\webapps\ROOT'): os.system(command) elif os.path.exists('D:\TOMCAT\publishing-web\webapps\ROOT.war'): os.system(command) Host = '172.16.110.106' user = "root" passwd = "*****" port = 22 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(Host, port, user, passwd) # (stdin, stdout, stderr) = ssh.exec_command("killall -9 process_main") (stdin, stdout, stderr) = ssh.exec_command("ls /home/fileDownload/update/bole") # 获取所有版本列表 st = stdout.readlines() version = st[len(st) - 1] # 最新版本的文件夹 (stdin1, stdout1, stderr1) = ssh.exec_command("ls /home/fileDownload/update/bole/%s" % version) # 获取最新版本下的war表 st1 = stdout1.readlines() for index, tt in enumerate(st1): print(index, tt[:-1]) scpclient = SCPClient(ssh.get_transport(), socket_timeout=15.0) remotepath = r'/home/fileDownload/update/bole/%s/%s' % (version[:-1], st1[0][:-1]) # 分片去掉后面的换行符号 localpath = r'D:\TOMCAT\publishing-web\webapps\ROOT.war' scpclient.get(remotepath, localpath) # 从服务器中获取war包文件 remotepath_rn = r'/home/fileDownload/update/bole/%s/%s' % (version[:-1], st1[1][:-1]) # 分片去掉后面的换行符号 localpath_rn = r'D:\TOMCAT\publishing-web\webapps\releaseNotes.txt' scpclient.get(remotepath_rn, localpath_rn) ssh.close() os.system(r'start D:\TOMCAT\publishing-web\bin\startup.bat') return 0 if __name__ == "__main__": auto()