sanketbajoria / ssh2-promise

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

tryKeyboard not working #74

Closed btxtiger closed 2 years ago

btxtiger commented 2 years ago

It's not working for me, it does not show any password prompt. Seems tryKeyboard is ignored at all. 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'
btxtiger commented 2 years ago

Shorter test code, also not prompting for password. Passing the password with the config works fine.

let SSH2Promise = require('ssh2-promise');

const ssh2Config = {
   host: 'shop.com',
   username: 'admin',
   // password: 'asdasd',
   identity: '~/.ssh/ssh-shop-prod',
   tryKeyboard: true,
};

let ssh = new SSH2Promise.SSH(ssh2Config);

ssh.on('keyboard-interactive', (name, instructions, instructionsLang, prompts, finish) => {
   console.log('Connection :: keyboard-interactive');
   finish(['my_password_on_remote_machine']);
})
   .connect(ssh2Config)
   .then((val) => {
      console.log('ok');
   })
   .catch((err) => {
      console.log('err', err);
   });
btxtiger commented 2 years ago

The problem was caused by multiple factors regarding the target server: 1) The key authentication was not working properly due to invalid permissions of the home directory of the remote user. Login via password and running chmod 755 ~ && logout resolved this problem. 2) The path of identity in the provided ssh config must be an ABSOLUTE PATH, so either pass the absolute path or write some function to convert ~/ starting paths into absolute paths.