chore: sync ops/rsh.js

This commit is contained in:
admin committed 2026-11-24 17:06:09 +00:00
1 parent 47c27f4176
commit 63c603ddd6
1 file changed
+21
+21
View File
@@ -0,0 +1,21 @@
// 非交互 SSH 执行器: node rsh.js <ub1|ub2|ub3> "<command>"
// 自动在命令含 sudo 时注入密码; stdout/stderr 透传, 退出码透传
const { Client } = require("ssh2");
const HOSTS = { ub1: "192.168.50.177", ub2: "192.168.50.161", ub3: "192.168.50.207" };
const [alias, cmd] = [process.argv[2], process.argv[3]];
const host = HOSTS[alias] || alias;
let full = cmd;
if (/\bsudo\b/.test(cmd) && !cmd.includes("sudo -S")) {
full = "echo 123456 | sudo -S bash -c " + JSON.stringify(cmd);
}
const conn = new Client();
conn.on("keyboard-interactive", (n, d, i, l, next) => next(null, ["123456"]))
.on("ready", () => conn.exec(full, (e, stream) => {
if (e) throw e;
let code = 0;
stream.on("close", (c) => { code = c || 0; conn.end(); process.exitCode = code; })
.on("data", (d) => process.stdout.write(d))
.stderr.on("data", (d) => process.stderr.write(d));
}))
.on("error", (e) => { console.error("SSH_ERR " + e.message); process.exitCode = 127; })
.connect({ host, port: 22, username: "czk", password: "123456", readyTimeout: 8000 });