#!/usr/bin/env python3 # -*- coding: utf-8 -*- import paramiko import time client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('42.194.232.22', port=22022, username='root', password='Zhiqun1984', timeout=15) print("=== 1. 检查端口占用 ===") cmd = "ss -tlnp | grep ':300' | head -10" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result if result else "无 300x 端口监听") print("\n=== 2. 检查 server.js 中的端口配置 ===") cmd = "grep -n 'PORT\\|port\\|3006\\|30006' /www/wwwroot/soul/server.js | head -10" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result) print("\n=== 3. 检查环境变量配置 ===") cmd = "cat /www/wwwroot/soul/.env 2>/dev/null | grep -i port || echo '无 .env 文件'" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result) print("\n=== 4. 停止 PM2 soul ===") cmd = "pm2 stop soul 2>&1" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') result = result.encode('ascii', errors='replace').decode('ascii') print(result) time.sleep(2) print("\n=== 5. 杀死占用 3006 端口的进程 ===") cmd = "lsof -ti:3006 | xargs kill -9 2>/dev/null || echo '无进程占用 3006'" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result) print("\n=== 6. 杀死占用 30006 端口的进程 ===") cmd = "lsof -ti:30006 | xargs kill -9 2>/dev/null || echo '无进程占用 30006'" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result) time.sleep(1) print("\n=== 7. 确认端口已释放 ===") cmd = "ss -tlnp | grep ':300'" stdin, stdout, stderr = client.exec_command(cmd, timeout=10) result = stdout.read().decode('utf-8', errors='replace') print(result if result else "[OK] 端口已全部释放") client.close() print("\n" + "=" * 60) print("下一步:修复 server.js 的端口配置为 30006")