前提:pip install paramiko
执行:python redmine.py
#redmine.py
import paramiko
# 创建ssh对象
ssh = paramiko.SSHClient()
# 连接方式
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 发起连接
ssh.connect("ip地址", 端口号, "用户名", "密码")
# 在远程Linux执行命令
stdin, stdout, stderr = ssh.exec_command("cat /usr/local/redmine-3.4.7/tmp/pids/server.pid")
# 将执行结果stdout打印出来
id = stdout.read().decode("utf8")
print("进程id:", id)
if id:
shasijincheng = f"kill -9 {id}"
stdin1, stdout, stderr1 = ssh.exec_command(shasijincheng)
print("杀死进程id:", id)
#执行切换目录到redmine路径 查看路径 以及启动redmine
stdin, stdout, stderr = ssh.exec_command("cd /usr/local/redmine-3.4.7; pwd;ruby ./bin/rails server webrick -e production -d -b 0.0.0.0 -p 9003")
print(stdout.read().decode('utf8'))
# #关闭ssh连接
ssh.close()