代码拉取完成,页面将自动刷新
import os
import subprocess
from flask import Flask, request, Response
import paramiko
import socket # 添加socket模块的导入
app = Flask(__name__)
def execute_remote_command(hostname, username, password, commands):
output = ""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname, username=username, password=password)
print(f"连接到远程主机 {hostname} 成功")
ssh_stdin, ssh_stdout, ssh_stderr = client.exec_command(" && ".join(commands)) # 将多个命令以字符串的形式连接起来传递给 exec_command
for line in ssh_stdout:
print(line.strip('\n'))
output += line
for line in ssh_stderr:
print(line.strip('\n'))
# 或者将错误信息存储到变量中,以便后续处理
# error += line
except (paramiko.ssh_exception.AuthenticationException, paramiko.ssh_exception.SSHException, socket.error) as e: # 添加socket.error在异常处理中
print(f"连接到远程主机 {hostname} 失败: {e}")
finally:
client.close()
return output
@app.route('/build-and-compile')
def build_and_compile():
package = request.args.get('package')
target_platform = request.args.get('platform')
if package and target_platform:
project, package_name = package.split('/')
target_dir = f"{project}/{package_name}"
print(target_dir)
if target_platform == 'x86':
x86_hostname = "192.168.1.72"
x86_username = "wjh"
x86_passwd = "wjh@#1234"
commands = [
f"rm -rf ~/devel/{project}/{package_name}*",
f"cd ~/devel && osc co {project}/{package_name}",
f"cd ~/devel/{project}/{package_name} && osc up -S",
f"cd ~/devel/{project}/{package_name} && rm -f _service; for file in `ls | grep -v .osc`; do new_file=${{file##*:}}; mv $file $new_file; done",
f"cd ~/devel/{project}/{package_name} && osc build standard_x86_64_gcc x86_64"
]
result = execute_remote_command(x86_hostname, x86_username, x86_passwd, commands)
result_with_br = result.replace('\n', '<br>')
return Response(result_with_br, content_type='text/html')
if __name__ == "__main__":
app.run()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。