小豆浆 自动部署本地jar包到服务器并启动(windows python3) www.xdoujiang.com

  • A+
所属分类:python实战
  1. 该脚本未测试 这个脚本适合单jar包上传 重启 我这里没环境 大家可以自行测试下 作者未知
  2. #!/usr/bin/python3
  3. # -*- coding: utf-8 -*-
  4. import paramiko
  5. import time
  6. import configparser
  7. class sshDeploy:
  8.     env = str
  9.     hostname = ''
  10.     username = ''
  11.     password = ''
  12.     ####包名配置
  13.     package = ''
  14.     ####上传路径配置
  15.     remotePath = ''
  16.     ####本地包路径配置
  17.     localpath = ''
  18.     ####重启脚本配置
  19.     restartSh = ''
  20.     ####本地包
  21.     localPackage = ''
  22.     ####服务器包
  23.     remotePackage = ''
  24.     ####备份包名
  25.     cpPackage = ''
  26.     ####备份包
  27.     remoteCP = ''
  28.     port = 22
  29.     timeout = 3
  30.     ssh = ''
  31.     def upload(self):
  32.         #####备份
  33.         cmd = 'cp' + ' ' + self.remotePackage + ' ' + self.remoteCP
  34.         print('backup success...')
  35.         try:
  36.             stdin, stdout, sterr = self.ssh.exec_command(cmd)
  37.             sftp = self.ssh.open_sftp()
  38.             #####上传到服务器
  39.             sftp.put(self.localPackage, self.remotePackage)
  40.             print('upload success...')
  41.             restartCmd = 'sh ' + self.restartSh
  42.             print('restart cmd [' + restartCmd + ']')
  43.             stdin, stdout, sterr = self.ssh.exec_command('cd ' + self.remotePath + '\n' + restartCmd)
  44.             stdout.read().strip()
  45.             print('deploy success...')
  46.         except Exception:
  47.             print('call Yy')
  48.             raise
  49.     def __init__(selfstr):
  50.         cf = configparser.ConfigParser()
  51.         cf.read('deploy.conf', encoding='utf-8')
  52.         self.env = str
  53.         print('read conf...')
  54.         self.hostname = cf.get(self.env, "hostname")
  55.         print('hostname:' + self.hostname)
  56.         self.username = cf.get(self.env, "username")
  57.         print('username:' + self.username)
  58.         self.password = cf.get(self.env, "password")
  59.         print('password:' + self.password)
  60.         ####包名配置
  61.         self.package = cf.get(self.env, "package")
  62.         print('package:' + self.package)
  63.         ####上传路径配置
  64.         self.remotePath = cf.get(self.env, "remotePath")
  65.         print('to path:' + self.remotePath)
  66.         ####本地包路径配置
  67.         self.localpath = cf.get(self.env, "localpath")
  68.         print('from path:' + self.localpath)
  69.         ####重启脚本配置
  70.         self.restartSh = cf.get(self.env, "restartSh")
  71.         print('restartSh:' + self.restartSh)
  72.         ####本地包
  73.         self.localPackage = self.localpath + self.package
  74.         ####服务器包
  75.         self.remotePackage = self.remotePath + self.package
  76.         ####备份包名
  77.         self.cpPackage = self.package + time.strftime("_%Y%m%d-%H:%M", time.localtime())
  78.         ####备份包
  79.         self.remoteCP = self.remotePath + self.cpPackage
  80.         port = 22
  81.         timeout = 3
  82.     def connect(self):
  83.         #####连接到服务器
  84.         try:
  85.             self.ssh = paramiko.SSHClient()
  86.             self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  87.             self.ssh.connect(self.hostname, 22, self.username, self.password)
  88.         except paramiko.AuthenticationException:
  89.             print('auth failed, check Authentication')
  90.             #exit()
  91.         except Exception:
  92.             print('call Yy..')
  93.             raise
  94.     def close(self):
  95.         try:
  96.             self.ssh.close()
  97.         except Exception:
  98.             print('close failed, do not worry')
  99.             #exit()
  100.     def deploy(self):
  101.         #连接
  102.         self.connect()
  103.         #上传部署启动
  104.         self.upload()
  105.         #关闭
  106.         self.close()
  107. if __name__ == "__main__":
  108.     str = input("waiting for input environment conf...\n")
  109.     sshDeploy(str).deploy()
  110. 配置文件[99]
    hostname = 192.168.30.99
    username = root
    password = quanyan888
    package = businessPlace-alpha.jar
    remotePath = /opt/place-alpha/place-business/
    localpath = D:/market3_server/businessPlace/target/
    restartSh = run.sh

    [104]
    hostname = 192.168.30.104
    username = root
    password = quanyan888
    package = businessPlace-alpha.jar
    remotePath = /opt/businessPlace/
    localpath = D:/market3_server/businessPlace/target/
    restartSh = run.sh

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: