Search code examples
node.jsexeccd

How to specify folder location using `cd` command inside exec function?


Instead of specifying root of file for execution in a remote server, how we can use cd command in exec() in nodejs.What i am done is like this.

         var command_part1 = `ssh -p 22 root@ip`;
         var command_part2 = `python3 test.py ${input}`;
         var folderPath    = `/root/folder/`;
         var child         = exec(`${command_part1} && cd ${folderPath} && ${command_part2}` , function (error, stdout, stderr) {
          if (error !== null) {
           callback(error)
          } else {
           callback();
          }
        });

But this code is not working, when i am executing this command locally,ssh login only is happening, further commands are not working.How can i make this working ? I dont want to specify the folder path like python3 /root/folder/test.py ${input};

Thank You Inadvance


Solution

  • Try this code,

     `var command =  `sshpass -p givePassword ssh -o "StrictHostKeyChecking no" root@ip "cd ${folderPath} && ${command_part2}"`
    

    sshpass is a simple and lightweight command line tool that enables us to provide password (non-interactive password authentication) to the command prompt itself, so that automated shell scripts can be executed