1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import os import paramiko import logging
root = '.' port = 9801
ip = "x.x.x.x" username = "root" password = "xxxx"
def execute_command(command): """ 执行命令,返回命令的输出 """ ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,22,username,password)
stdin, stdout, stderr = ssh.exec_command(command) output = stdout.readlines() ssh.close() return output
print("删除远程服务器 jar 包 开始--------------------") execute_command('rm xxx.jar') print("删除远程服务器 jar 包 结束--------------------")
print("开始编译---------------------------------------") os.system("mvn clean package -f " + root) print("结束编译---------------------------------------")
print("开始上传文件---------------------------------------") t = paramiko.Transport((ip,22)) t.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(t) remotepath='xxx.jar' localpath= os.getcwd() + r'\target\scrm-api.jar' sftp.put(localpath,remotepath) t.close() print("结束上传文件---------------------------------------")
pid = execute_command('lsof -i:' + str(9801) + ' -t')[0] print(pid) if pid != '': print('远程服务器进程存在,pid 为 ' + pid) execute_command('kill -9 ' + pid) print('杀死进程完成')
command = "cd /home/admin ; nohup java -jar -Xms128m -Xmx256m -XX:PermSize=128M -XX:MaxPermSize=256M xxx.jar > api.log 2>&1 &"
execute_command(command) print("脚本执行完成")
|