title: SSH into remote machine and execute commands in Node.js
body: <p>I'm looking for a way to SSH into a virtual machine and then execute certain scripts inside the virtual machine using Node.js</p>
  
  <p>So far I've created the shell script that automate the login to the virtual machine but i'm unable to figure out how to move forward.</p>
  
  <p>My shell script for login to remote server</p>
  
  <pre><code>spawn ssh root@localhost
  expect "password"
  send "123456"
  send "\r"
  interact
  </code></pre>
  
  <p>This is my server.js</p>
  
  <pre><code>var http = require('http');
  var execProcess = require("./exec_process.js");
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    execProcess.result("sh sshpass.sh", function(err, response){
          if(!err){
              res.end(response);
          }else {
              res.end("Error: ", err);
          }
      });
  }).listen(3000);
  console.log('Server listening on port 3000');
  </code></pre>
  
  <p>exec_process.js</p>
  
  <pre><code>var exec = require('child_process').exec;
  
  var result = function(command, cb){
      var child = exec(command, function(err, stdout, stderr){
          if(err != null){
              return cb(new Error(err), null);
          }else if(typeof(stderr) != "string"){
              return cb(new Error(stderr), null);
          }else{
              return cb(null, stdout);
          }
      });
  }
  
  exports.result = result;
  </code></pre>
  
  <p>Any help is appreciated thanks</p>