python远程启动exe,用python来部署远程程序

此python程序功能: 执行git pull、 执行maven package、远程上传war到/tmp、 远程命令部署。

采用python第三方包paramiko做远程,请事先用pip install paramiko下载之。

远行环境: windows7, python版本:3.x

文件一: file_download_upload.py

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

import paramiko

"""

下载文件到本地

"""

ip=None

port=0

username=None

passwd=None

def validateParam():

if not (ip and port and username and passwd) :

raise Exception("请设置ssh参数!")

def ssh2_download():

validateParam();

t = paramiko.Transport((ip, port))

t.connect(username=username, password=passwd)

sftp = paramiko.SFTPClient.from_transport(t)

remotepath = '/var/log/system.log'

localpath = '/tmp/system.log'

sftp.get(remotepath, localpath)

t.close()

"""

上传文件

"""

import traceback

def ssh2_upload(file, remotepath):

validateParam()

try:

t = paramiko.Transport((ip, port))

t.connect(username = username, password = passwd)

sftp = paramiko.SFTPClient.from_transport(t)

sftp.put(file,remotepath)

t.close()

except Exception as e:

traceback.print_exc()

文件二: remote_cmd.py

'''

Created on Feb 19, 2017

@author: zhai

'''

import sys

import paramiko

__all__ = ['ssh2']

def ssh2(ip, username, passwd, cmds):

try:

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(ip, 22, username, passwd, timeout=5)

for m in cmds:

stdin, stdout, stderr = ssh.exec_command(m)

#           stdin.write("Y")   #简单交互,输入 ‘Y’

out = stdout.readlines()

# 屏幕输出

for o in out:

print(o, end=' ')

print('%s\tOK\n' % (ip))

ssh.close()

except :

print('%s\tError\n' % (ip))

print(sys.exc_info())

文件三、wildfly_deploy.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os

import linux.ssh.paramiko.file_download_upload as upload

from linux.ssh.paramiko import exec_cmd_multi_ip as cmd

java_code_path = 'D:/devTools/workspace/xxx/xxx_platform'

ignore_file = ['.gitignore', '.project', '.settings', 'pom.xml']

file_ext = [".war"]

class AutoDeploy:

war_files = []

def gitPull(self):

os.chdir(java_code_path)

os.system('git pull')

def mvnInstall(self):

os.chdir(java_code_path)

os.system('mvn clean install')

def scanDeployFiles(self, _dir):

for i in os.listdir(_dir):

if i in ignore_file :

continue;

sub_dir = os.path.join(_dir, i)

target_dir = sub_dir + "/target"

for file in os.listdir(target_dir):

dir3_file = os.path.join(target_dir, file)

if not os.path.isdir(dir3_file) :

ext = os.path.splitext(dir3_file)[1]

#print()

if ext in file_ext :

self.war_files.append(dir3_file)

"""

#if (os.path.splitext(sub_dir)[1] == "war"):

"""

def uploadDeployFile(self):

files = self.war_files;

print(files)

upload.ip = "192.168.60.50"

upload.port = 22

upload.username = "root"

upload.passwd = "dHie&tr#Ge"

for f in files :

#print('update file:%d', os.stat(f).st_size)

#print("upload file:" + os.path.split(f)[1])

remoteFile = os.path.join("/tmp/", os.path.split(f)[1])

upload.ssh2_upload(f, remoteFile)

def execDeploy(self):

cmd.ssh2(upload.ip, upload.username, upload.passwd, ['mv -f /tmp/*war /opt/wildfly/latest/standalone/deployments/'])

if __name__ == '__main__':

auto = AutoDeploy()

auto.gitPull()

auto.mvnInstall()

auto.scanDeployFiles(java_code_path)

auto.uploadDeployFile()

auto.execDeploy()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值