sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 24 forks source link

Question: How to add "keyboard-interactive" event #38

Closed vella-nicholas closed 5 years ago

vella-nicholas commented 5 years ago

I wish to set an ssh connection and setting tryKeyboard: true. The problem is I am adding the listener correctly because it does not seem to work (this works correctly in the ssh2 library). Can you assist please? This is my setup:

 var SSH2Promise = require("ssh2-promise");

const connectionInfo = {
          host: newValue.server_hostname,
          port: 5022,
          username: newValue.server_username,
          password: newValue.server_password,
          tryKeyboard: true
};

var ssh = new SSH2Promise(connectionInfo);
        ssh = ssh.addListener(
          "keyboard-interactive",
          (name, instructions, instructionsLang, prompts, finish) => {
            console.log("Connection :: keyboard-interactive");
            finish([connectionInfo.password]);
          }
        );

        ssh
          .connect()
          .then(x => {
            console.log("Connection established");
          })
          .catch(error => {
            console.error(error);
          });
sanketbajoria commented 5 years ago

@vella-nicholas

You don't have to add listener for keyboard-interactive. This library internally handles it. For making connection with tryKeyboard just do as below

var SSH2Promise = require("ssh2-promise");

const connectionInfo = {
          host: newValue.server_hostname,
          port: 5022,
          username: newValue.server_username,
          tryKeyboard: true
};

var ssh = new SSH2Promise(connectionInfo);

//This will prompt for password, when ssh.connect execute. once password is entered, it can be connected or Not Connected based on password
ssh.connect().then((ssh) => {
            console.log("Connected")
        }, (error) => {
            console.log("Not Connected")
        })
vella-nicholas commented 5 years ago

Thank you for your answer.

sanketbajoria commented 5 years ago

@vella-nicholas Did it work for you? I haven't exposed keyboard-interactive event from this library. If you think, of any use case, where you would require this event, then pls let me know.

vella-nicholas commented 5 years ago

@sanketbajoria Unfortunately it did not work for me because I want to connect programmatically by supplying password in connectionInfo and using the keyboard-interactive event because the server allows only keyboard-interactive authentication.. I am getting these series of errors:

DEBUG: Parser: IN_PACKETBEFORE (expecting 16)

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit -> Even though it was set to 99999

'0': 'ssh',
  '1': 'disconnect',
  '2': 
   SSHConnection {
     domain: 
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        members: [Array] },
     _events: 
      { ssh: [Function: bound ],
        tunnel: [Function: bound ],
        'ssh:disconnect': [Function: disconnectCb],
        'ssh:continue': [Function: continueCb] },
     _eventsCount: 4,
     _maxListeners: undefined,
     activeTunnels: {},
     '__$connectPromise': Promise { <pending>, domain: [Object] },
     __retries: 11,
     __err: { Error: Timed out while waiting for handshake
    at Timeout._onTimeout (/srv/node_modules/ssh2/lib/client.js:694:19)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) level: 'client-timeout' },

I did not report a bug because I am not sure it is.

salvadorplj commented 3 years ago

@sanketbajoria it would be very useful to expose "keyboard-interactive". AWS ec2 instances require a "yes" input when connecting in addition to the private key.

    sshConn.addListener('keyboard-interactive', (name, instructions, instructionsLang, prompts, finish) => {
      console.log('Connection :: keyboard-interactive');
      finish(['yes']);
    });

I would really appreciate if the event could be exposed.

btxtiger commented 2 years ago

It's not working for me, it does not show any password prompt. Any ideas?

public static async connect(config: ServerConfig): Promise<SSHConnection> {
      const ssh2Config = {
         host: config.hostName,
         username: config.username,
         // password: config.password,
         identity: config.identityFilePath,
         tryKeyboard: true
      };

      const ssh = new SSH2Promise.SSH(ssh2Config);

      let error: Error | null;
      let success: SSHConnection | undefined;
      [error, success] = await to(ssh.connect());
      if (error) {
         console.error(colors.bgRed(colors.black(' ❌ SSH connection '+ config.connectionName +" failed \n")), error);
         throw new Error();
      } else {
         console.log(colors.bgGreen(colors.black(' ✅ SSH connection '+ config.connectionName +' established ')));
      }

      return ssh;
   }
 ❌ SSH connection production failed
 Error: All configured authentication methods failed
...
at Socket.emit (node:events:390:28) {
level: 'client-authentication'