使用 ssh2-connect 和 ssh2-exec 与 nodeJS 的 SSH 连接

SSH conection using ssh2-connect and ssh2-exec with nodeJS

我需要帮助在带有 nodeJs 的机器上建立 ssh 连接,我需要在登录“sudo /usr/bin/rootsh -i -u root”后首先使用此命令,然后进入此路径“cd .." + "cd u01/scripts/" + "cat output.txt" 只给出一个错误

connect = require("ssh2-connect");
exec = require("ssh2-exec");

connect(
  {
    host: "XX.XX.XXX.XX",
    username: "XXXXXXXX",
    password: "XXXXX",
  },
  function (err, ssh) {
    child = exec(
      {
        command:
          "ssh -qtt username@host -- sudo /usr/bin/rootsh -i -u root; cd ..; cd u01/scripts/; cat saida.txt",
        ssh: ssh,
      },
      function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        console.log(err);
      }
    );
  }
);

有效

var host = {
  server: {
    host: "XX.XX.XXX.XX",
    username: "XXXXXXXX",
    password: "XXXXX",
  },
  commands: [
    "sudo /usr/bin/rootsh -i -u root",
    "cd ..; cd u01/scripts/; cat saida.txt",
  ],
};

var SSH2Shell = require("ssh2shell"),
  SSH = new SSH2Shell(host),
  callback = function (sessionText) {
    console.log(sessionText);
  };

SSH.connect(callback);