最近刚刚学python,非想要做点东西不可。。。。
做了一个自动化部署的脚本,献丑了。
以下假设数据库与应用在一起
过程如下:
本地maven打包
上传到服务器
服务器数据库库备份
服务器关闭tomcat
服务器应用程序备份
服务器清除应用、work
服务器启动tomcat
/utils/SSHConnection.py ,远程操作工具类
#! /usr/bin/python
import os
import paramiko
class SSHConnection:
__hostname = ''
__port = 22
__username = ''
__password = ''
__ssh = ''
def __init__ (self, hostname, port, username, password):
self.__hostname = hostname
self.__port = port
self.__username = username
self.__password = password
def SSHClient(self):
print ( 'ssh %s@%s ....' % (self.__username, self.__hostname) )
try:
self.__ssh = paramiko.SSHClient()
self.__ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
self.__ssh.connect(hostname = self.__hostname, username = self.__username, port = self.__port, password = self.__password)
print ( 'ssh %s@%s success!!!' % (self.__username, self.__hostname) )
except Exception as e:
print ( 'ssh %s@%s: %s' % (self.__username, self.__hostname, e) )
os._exit(0)
def exec_command(self, command):
print ('command:', command)
stdin, stdout, stderr = self.__ssh.exec_command(command)
err_list = stderr.readlines()
if len( err_list ) > 0:
print ('ssh exec remote command [%s] error: %s' % (command, err_list[0]))
print (stdout.read().decod