Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
I want to first connect to aws and hit a python command from Nodejs via Api. Not sure how to do?

I want to execute 'python3 sample.py --path.data=/root/tej4' command from nodejs.The below is my code -

const runningPython = async(req,res,next) => {
    ssh.connect({
        host: '******',
        port: 22,
        username: 'ubuntu',
        privateKey: '*******'
    }).then(function () {
     exec('python3 sample.py --path.data=/root/tej4', (err, stdout, stderr) => {
        if (err) {
          console.log(err)
        }
      
        // the *entire* stdout and stderr (buffered)
        console.log(`stdout: ${stdout}`);
        console.log(`stderr: ${stderr}`);
      });
    })
    }

But what I am getting response is its trying to run locally rather than on aws cli. Not sure how to achieve this?

Error: Command failed: python3 sample.py --path.data=/root/tej4
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

    at ChildProcess.exithandler (node:child_process:397:12)
    at ChildProcess.emit (node:events:390:28)
    at maybeClose (node:internal/child_process:1064:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
  killed: false,
  code: 9009,
  signal: null,
  cmd: 'python3 sample.py --path.data=/root/tej4'
}
stdout: 
stderr: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to create an instance of type node_ssh and use that instance to call connect and executeCommand methods

const node_ssh = require('node-ssh');
const ssh = new node_ssh();

ssh
  .connect({
        host: '******',
        port: 22,
        username: 'ubuntu',
        privateKey: '*******'
  })
  .then(() => {
    ssh.execCommand('python3 sample.py --path.data=/root/tej4', { cwd:'/your-python-script-path-here' })
       .then((result) => {
          console.log('STDOUT: ' + result.stdout)
          console.log('STDERR: ' + result.stderr)
       });
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use ssh lib, but for sudo su and running multiple commands switch to ssh2shell, the code for that is mentioned below:

const runningPython = async (req, res, next) => {
  var host = {
    server: {
      host: ***** ,
      username: ***** ,
      privateKey: fs.readFileSync('ppk file path'),
    },
    commands: ["sudo su -", "python3 sample.py --path.data=root/tej4"],
    onCommandComplete: function (command, response, sshObj) {
      if (sshObj.debug) {
        this.emit("msg", this.sshObj.server.host + ": host.onCommandComplete event, command: " + command);
      }
      if (command === "sudo su -" && response.indexOf("/root") != -1) {

        sshObj.commands.unshift("msg:The command and response check worked. Added another cd command.");
        sshObj.commands.unshift("cd .ssh");
      } else if (command === "python3 sample.py --path.data=root/tej4") {
        this.emit("msg", response);

      }
    },

    onEnd: function (sessionText, sshObj) {

      if (sshObj.debug) {
        this.emit("msg", this.sshObj.server.host + ": host.onEnd event");
      }
    }
  };

  let SSH = new SSH2Shell(host),
    //Use a callback function to process the full session text
    callback = function (sessionText) {
      console.log(sessionText)
    }

  //Start the process
  SSH.connect(callback);

}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda