3

I am trying to read data in stream from a remote server log file, which is continuously growing. I want to display the new lines added to my local console. I am using ssh for connection from local to remote server.

I found below solution on github which is writing local file content to remote file but i want in other way. Not getting an idea to convert this in reverse direction.

var Connection = require('ssh2');
var fs = require('fs');
var BufferedStream = require('buffered-stream');

console.log('sshstream started')
connectSSH();

function readFile(filepath, startOffset, outputStream) {
    var fileSize = fs.statSync(filepath).size;
    var length = fileSize - startOffset;
    var myFD = fs.openSync(filepath, 'r');
    var readable = fs.createReadStream(filepath, {
        fd: myFD, start: startOffset, end: fileSize, autoClose: true
    })
    return readable;
}
function connectSSH(){
    var c = new Connection();
    c.on('connect', function() {
      console.log('Connection :: connect');
    });
    c.on('ready', function() {
      console.log('Connection :: ready');
      c.shell('', function(err, stream) {
        if (err) throw err;         
        stream.on('data', function(data, extended) {
          console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ')
                      + data);
        });
        stream.on('end', function(code, signal) {
          console.log('Stream :: EOF', code, signal);
        });
        stream.on('close', function(code, signal) {
          console.log('Stream :: close', code, signal);
        });
        stream.on('exit', function(code, signal) {
          console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
          c.end();
        });
        stream.on('drain', function() {
          console.log('Stream :: drain');
        });       
            var bufferStream = new BufferedStream(4*1024*1024);
            bufferStream.pipe(stream);
            stream.write('cat - >> /home/username/mylog.log');
            readable = readFile('test.log', 0, bufferStream);
            readable.once('end', function(){
                    console.log("ENDED");
            });
            readable.pipe(bufferStream).pipe(stream);           
        });
    });
    c.on('error', function(err) {
      console.log('Connection :: error :: ' + err);
    });
    c.on('end', function() {
      console.log('Connection :: end');
    });
    c.on('close', function(had_error) {
      console.log('Connection :: close');
    });
    c.connect({
      host: 'xx.xxx.xx.xxx',
      port: 22,
      username: 'username',
      password: 'password'   
    });
}

Please suggest Thanks

usersam
  • 1,125
  • 4
  • 27
  • 54

1 Answers1

4

This works and it could be customized as you wish.

'use strict';

const SSH = require('simple-ssh');

function run(sshConfig, script) {
  return new Promise((resolve, reject) => {
    let scriptOutput = '';
    const sshFtw = new SSH(sshConfig);
    sshFtw.exec(script,
      { out: console.log.bind(console) })
      .on('error', (err) => reject(err))
      .on('close', () => resolve(scriptOutput))
      .start();
  });
};


run({
  "host": "1.2.3.4",
  "user": "my-user",
  "pass": "my-psw"
}, 'tail -f /home/my-app/log/api-out.log');
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • Thanks @Manuel. It is perfect. :-) For many days i was struggling with this issue but you made my day. I also tried tail but it was without promises and caused data loss at some places. This solution is working so far but I will check more rigorously and let you know. – usersam May 21 '19 at 18:27