70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
|
|
"""
|
||
|
|
快速更新 index.html 到 NAS
|
||
|
|
"""
|
||
|
|
import paramiko, base64, os, sys, time, io, tarfile
|
||
|
|
|
||
|
|
NAS_HOST = "100.85.152.112"
|
||
|
|
NAS_PORT = 22
|
||
|
|
NAS_USER = "simon"
|
||
|
|
NAS_PASS = "Nas82829330"
|
||
|
|
NAS_DEPLOY_PATH = "/volume1/docker/gaokao-portal"
|
||
|
|
LOCAL_INDEX = r"C:\Users\simon\WorkBuddy\2026-05-20-15-22-53\deploy\nginx\html\index.html"
|
||
|
|
|
||
|
|
def connect():
|
||
|
|
c = paramiko.SSHClient()
|
||
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
|
|
c.connect(NAS_HOST, port=NAS_PORT, username=NAS_USER, password=NAS_PASS, timeout=15)
|
||
|
|
return c
|
||
|
|
|
||
|
|
def run(c, cmd, desc=""):
|
||
|
|
if desc: print(f"[{desc}]")
|
||
|
|
s, i, e = c.exec_command(cmd, timeout=30)
|
||
|
|
while not s.channel.exit_status_ready(): time.sleep(0.1)
|
||
|
|
out = s.read().decode("utf-8", errors="replace").strip()
|
||
|
|
err = e.read().decode("utf-8", errors="replace").strip()
|
||
|
|
return out, err
|
||
|
|
|
||
|
|
def run_stdin(c, cmd, data):
|
||
|
|
"""通过 stdin 传输单文件"""
|
||
|
|
t = c.get_transport()
|
||
|
|
ch = t.open_session()
|
||
|
|
ch.exec_command(cmd)
|
||
|
|
ch.sendall(data)
|
||
|
|
ch.shutdown_write()
|
||
|
|
while not ch.exit_status_ready():
|
||
|
|
if ch.recv_ready(): ch.recv(4096)
|
||
|
|
time.sleep(0.05)
|
||
|
|
out = b""
|
||
|
|
while ch.recv_ready(): out += ch.recv(4096)
|
||
|
|
return out.decode("utf-8", errors="replace").strip()
|
||
|
|
|
||
|
|
# 1. 读取新 index.html
|
||
|
|
with open(LOCAL_INDEX, "r", encoding="utf-8") as f:
|
||
|
|
html = f.read()
|
||
|
|
print(f"读取本地: {len(html)} 字节")
|
||
|
|
|
||
|
|
# 2. base64 编码
|
||
|
|
b64 = base64.b64encode(html.encode("utf-8")).decode("ascii")
|
||
|
|
print(f"Base64: {len(b64)} 字符")
|
||
|
|
|
||
|
|
# 3. 连接并上传
|
||
|
|
c = connect()
|
||
|
|
print("[连接 OK]")
|
||
|
|
|
||
|
|
try:
|
||
|
|
# 4. 上传 index.html
|
||
|
|
out, err = run_stdin(c, f"base64 -d > '{NAS_DEPLOY_PATH}/nginx/html/index.html' 2>/dev/null && echo OK || echo FAIL", b64.encode())
|
||
|
|
print(f"上传结果: {out}")
|
||
|
|
|
||
|
|
# 5. 验证
|
||
|
|
out, err = run(c, f"ls -la '{NAS_DEPLOY_PATH}/nginx/html/index.html'", "验证文件")
|
||
|
|
|
||
|
|
# 6. 重启 nginx 容器
|
||
|
|
docker = "echo 'Nas82829330' | sudo -S /usr/local/bin/docker"
|
||
|
|
out, err = run(c, f"{docker} compose -f '{NAS_DEPLOY_PATH}/docker-compose.yml' restart nginx 2>&1", "重启nginx")
|
||
|
|
print("nginx 已重启")
|
||
|
|
|
||
|
|
finally:
|
||
|
|
c.close()
|
||
|
|
|
||
|
|
print("index.html 更新完成!")
|