const { Client } = require('ssh2'); const config = { host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' }; const conn = new Client(); conn.on('ready', () => { console.log('SSH连接成功'); const cmds = [ 'killall nginx 2>/dev/null; pkill nginx 2>/dev/null; sleep 1', 'netstat -tlnp | grep ":80 " || echo "port_80_free"', 'docker stop huishou; docker rm huishou', 'cd /www/huishou && sed -i "s/4001:80/80:80/g" docker-compose.yml', 'cd /www/huishou && docker-compose up -d', 'sleep 10 && docker ps | grep huishou' ]; let i = 0; function execNext() { if (i >= cmds.length) { conn.end(); console.log('修复完成'); return; } const cmd = cmds[i]; console.log(`\n执行命令 ${i + 1}: ${cmd}`); i++; conn.exec(cmd, (err, stream) => { if (err) { console.error('错误:', err.message); execNext(); return; } stream.stdout.on('data', (data) => { console.log(data.toString().trim()); }); stream.stderr.on('data', (data) => { console.log('stderr:', data.toString().trim()); }); stream.on('close', () => { execNext(); }); }); } execNext(); }); conn.on('error', (err) => { console.error('SSH连接错误:', err.message); }); conn.on('end', () => { console.log('SSH连接已关闭'); }); conn.connect(config);