javascript - SSH into remote machine and execute commands in Node.js - Stack Overflow

I'm looking for a way to SSH into a virtual machine and then execute certain scripts inside the vi

I'm looking for a way to SSH into a virtual machine and then execute certain scripts inside the virtual machine using Node.js

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.

My shell script for login to remote server

spawn ssh root@localhost
expect "password"
send "123456"
send "\r"
interact

This is my server.js

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

exec_process.js

var exec = require('child_process').exec;

var result = function(mand, cb){
    var child = exec(mand, 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;

Any help is appreciated thanks

I'm looking for a way to SSH into a virtual machine and then execute certain scripts inside the virtual machine using Node.js

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.

My shell script for login to remote server

spawn ssh root@localhost
expect "password"
send "123456"
send "\r"
interact

This is my server.js

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

exec_process.js

var exec = require('child_process').exec;

var result = function(mand, cb){
    var child = exec(mand, 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;

Any help is appreciated thanks

Share Improve this question edited Aug 18, 2017 at 20:25 user7888003 asked Aug 18, 2017 at 20:18 user7888003user7888003 131 gold badge1 silver badge5 bronze badges 2
  • 1 node server.js – Erazihel Commented Aug 18, 2017 at 20:20
  • yeah i ran that but i'm getting the output to something like this: Error: – user7888003 Commented Aug 18, 2017 at 20:45
Add a ment  | 

1 Answer 1

Reset to default 1

Why dont you use simple-ssh ?

I made a simple example of how to load a file with a list of mands and execute them in chain.

exampleList : (mands must be separated in new lines)

echo "Testing the first mand"
ls

sshtool.js : (it can be buggy, example: if any mand contains \n)

const _ = require("underscore");
//:SSH:
const SSH = require('simple-ssh');
const ssh = new SSH({
    host: 'localhost',
    user: 'username',
    pass: 'sshpassword'
});
//example usage : sshtool.js /path/to/mand.list
function InitTool(){
 console.log("[i] SSH Command Tool");
 if(process.argv[2]){AutomaticMode();}
 else{console.log("[missing argument : path to file containing the list of mands]");}
}
//:MODE:Auto
function AutomaticMode(){
 const CMDFileName = process.argv[2];
 console.log("  ~ Automatic Command Input Mode");
 //Load the list of mands to be executed in order
 const fs = require('fs');
 fs.readFile(process.argv[2], "utf8", (err, data) => {
  if(err){console.log("[!] Error Loading Command List File :\n",err);}else{
    var CMDList = data.split("\n"); // split the document into lines
    CMDList.length = CMDList.length - 1; //fix the last empty line
    _.each(CMDList, function(this_mand, i){
      ssh.exec(this_mand, {
       out: function(stdout) {
        console.log("[+] executing mand",i,"/",CMDList.length,"\n  $["+this_mand+"]","\n"+stdout);
       }
      });
    });
    console.log("[i]",CMDList.length,"mands will be performed.\n");
    ssh.start();
  }
 });
}
//:Error Handling:
ssh.on('error', function(err) {
 console.log('[!] Error :',err);
 ssh.end();
});

InitTool();

It uses underscore for looping through the mand list.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181969a4615459.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信