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

Viewed 289

I need help to make an ssh connection on a machine with nodeJs, I need to use this command first after logging in "sudo /usr/bin/rootsh -i -u root" and then go in this path "cd .." + "cd u01/scripts/" + "cat output.txt" only giving an error

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);
      }
    );
  }
);
1 Answers

It worked

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);
Related