Soportar autenticacion SSH por clave privada

This commit is contained in:
2026-06-03 00:49:10 +02:00
parent 8a0e1e83ed
commit ffdf787aa3
6 changed files with 86 additions and 14 deletions
+27 -7
View File
@@ -364,17 +364,15 @@ function runCommandWithInput(command, args, input, timeoutMs) {
}
async function sshMetrics(device) {
if (!device.password) {
throw new Error("Password SSH no configurada");
const hasPrivateKey = Boolean(device.privateKeyPath);
if (!hasPrivateKey && !device.password) {
throw new Error("Credenciales SSH no configuradas");
}
const timeoutMs = Math.max(2, config.sshTimeoutSeconds || 8) * 1000;
const port = Number(device.port || 22);
const destination = `${device.username || "pi"}@${device.host}`;
const args = [
"-p",
device.password,
"ssh",
const sshArgs = [
"-o",
"StrictHostKeyChecking=no",
"-o",
@@ -388,7 +386,29 @@ async function sshMetrics(device) {
"-s"
];
const { stdout } = await runCommandWithInput("sshpass", args, REMOTE_METRICS_SCRIPT, timeoutMs);
let command = "ssh";
let args = sshArgs;
if (hasPrivateKey) {
args = [
"-i",
device.privateKeyPath,
"-o",
"BatchMode=yes",
"-o",
"IdentitiesOnly=yes",
...sshArgs
];
} else {
command = "sshpass";
args = [
"-p",
device.password,
"ssh",
...sshArgs
];
}
const { stdout } = await runCommandWithInput(command, args, REMOTE_METRICS_SCRIPT, timeoutMs);
const values = parseKeyValueOutput(stdout);
const temp = toNumber(values.cpuTempC, 0);
const loadAverage = String(values.loadAverage || "")