1

My Code Looks Like this

var Client = require('ssh2').Client;
var fs=require('fs');
var conn = new Client();
conn.on('ready', function() {
    console.log('Client :: ready');
    conn.shell('xtail', function(err, stream) {
        stream.write('xtail\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) {
           // stream.stdin.write('?');
            console.log('hey');
            console.log('STDOUT: ' + data);
            fs.appendFile('D:\\Breme\\As1.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);
            conn.destroy();

        });
    });
}).connect({
    host: '10.214.14.15',
    port: 22,
    username: 'bwadn',
    password: 'bwain'
});

after connecting to server I am downloading the file but I want to send 'Ctrl+C' command from client, so that it stops receiving data

Can anyone please help me in solving this, As i am new to NodeJS I need your support in solving this...thanks

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • See here http://stackoverflow.com/questions/11696967/node-js-how-to-send-control-c-to-child-process – Ali Niaz May 04 '17 at 05:29
  • @AliNiaz i am unable follow it can you please help little more ? – 1031210155 145046 May 04 '17 at 05:56
  • 2
    Possible duplicate of [Node.js: How to send control C to child process](http://stackoverflow.com/questions/11696967/node-js-how-to-send-control-c-to-child-process) – Jakuje May 04 '17 at 06:44

1 Answers1

0

Most reliable way in this particular case, since you are using a variant of tail in your shell, is to use

 stream.write('\x03'); //send ctrl+c

you would need to send it when you know you don't need xtail output anymore. It is beyond the scope of this question to discuss the means, but setTimeout is definitely not a good idea.

Alternative (preferred) method:

stream.signal('INT')

and for other uses see Ssh2 Channel API

Alex Pakka
  • 9,466
  • 3
  • 45
  • 69