Thursday, 8 June 2017

How to send same command to shell session with different instance?

I have a http server running , I'm using ssh2 package for interactive shell session which is serving as a rest API inside HTTP server. First I will send a http request, to start a command (http://localhost:8080?TC=start) Now the ssh shell is created command executed and I will get some data in realtime on my console.

Next I want to send same command after sometime (http://localhost:8080?TC=start).

Now I want this start command to open a new ssh2 connection,but when i try for second time it is opening two connections . kindly help

dispatcher.onGet("/executessh", function(req, res) {
var str = '';
var url = require('url');
var url_parts = url.parse(req.url, true);
console.log(url_parts);
var TC = url_parts.query.TC;
var TC1 = url_parts.query.TC1;
var sleep =require('system-sleep');
var process=require('process');
var CtrlC = require("ctrl-c");
/if(TC==='stop')
{
process.exit(1);

}/
if(TC==='start')
{

conn.on('ready', function() {
    console.log('Client :: ready');
    conn.shell('sampleWindow', function(err, stream) {
    if(TC1==='stop')
    {
       //How to stop 

    }
    stream.write('startCommand\n\r');

    if (err) throw err;
    stream.on('close', function(code, signal) {
        console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
        conn.end();
    }).on('data', function(data) {


        console.log('STDOUT: ' + data);
        fs.appendFile('D:\\Breme\\sample.txt',data,function(err){
            if(err)
            {
                console.log(err);
            }
            //console.log('file saving..')
        });
        // setTimeout(function(){
        //     process.exit(1);
        // }, 20000);

    }).stderr.on('data', function(data) {
        console.log('STDERR: ' + data);

    });

});
}).connect({
    host: 'x.x.x.x',
    port: 22,
    username: '123',
    password: '123'
});
}

});



via vinay

No comments:

Post a Comment