2

i have a simple intaeractive ssh client using ssh2 npm module and readline. On each line i sending data to server stream, but for some reason entered command sends too

var Client = require('ssh2').Client;
var readline = require('readline')

var conn = new Client();
conn.on('ready', function() {
  console.log('Client :: ready');
  conn.shell(function(err, stream) {
    if (err) throw err;
    // create readline interface
    var rl = readline.createInterface(process.stdin, process.stdout)

    stream.on('close', function() {
      process.stdout.write('Connection closed.')
      console.log('Stream :: close');
      conn.end();
    }).on('data', function(data) {
      // pause to prevent more data from coming in
      process.stdin.pause()
      process.stdout.write('DATA: ' + data)
      process.stdin.resume()
    }).stderr.on('data', function(data) {
      process.stderr.write(data);
    });

    rl.on('line', function (d) {
      // send data to through the client to the host
      stream.write(d.trim() + '\n')
    })

    rl.on('SIGINT', function () {
      // stop input
      process.stdin.pause()
      process.stdout.write('\nEnding session\n')
      rl.close()

      // close connection
      stream.end('exit\n')
    })

  });
}).connect({
        host: 'www58.lan',
        port: 22,
        username: 'gorod',
        password: '123qwe'
});

but every entered command are duplicated. How to make it with no duplicates? Thank you!

output:

gorod@www58:~$ ls
ls
temp.sql                       yo              sm_www94
a.out                          sm_dev1017      System Volume Information
dump20180801                   sm_qa1017       www58_sm_2310
dumps                          sm_www58
gorod@www58:~$

expected output:

gorod@www58:~$ ls
temp.sql                       yo              sm_www94
a.out                          sm_dev1017      System Volume Information
dump20180801                   sm_qa1017       www58_sm_2310
dumps                          sm_www58
gorod@www58:~$
Max Max
  • 389
  • 1
  • 2
  • 12

1 Answers1

3

Currently ssh2 does not support passing of terminal modes (e.g. disabling of remote terminal echo) when setting up the pseudo-TTY for the interactive shell session, although ssh2-streams does already support it.

Until that feature is added to ssh2, there are at least two possible workarounds:

  1. Automatically write 'stty -echo\n' once to the shell stream. That will effectively do the same thing as disabling the remote terminal echo from the get-go, except the stty command itself will be echoed.

  2. Use process.stdin.setRawMode(true) to disable the local echo and receive only the remote terminal echo. This has two drawbacks however: the remote terminal echo could be delayed (causing confusion) and you won't be able to catch ctrl-c via a 'SIGINT' event handler (this could potentially be a feature as it will transparently dispatch the ctrl-c to the remote server instead, which can be useful in some situations).

mscdex
  • 104,356
  • 15
  • 192
  • 153