2

I have a node application that uses the ssh2(https://www.npmjs.com/package/ssh2) npm package. I am trying to SSH using this. The server has 2-factor authentication enabled. I have tried the following code and it returns as 'All configured authentication methods failed'. PrivateKey, token(OTP Generated), and the passphrase that I am passing are correct. Can someone help me to achieve this?

var { authenticator } = require('otplib');
const { Client } = require('ssh2');
var fs = require('fs');
var path = require('path');

/* Input constants */
const secret = 'secret_key';


try{
    const token = authenticator.generate(secret);
    const sshConfig = {
        host: '<ip>',
        port: 22,
        username: '<user_name>',
        passphrase: '<passphrase>',
        tryKeyboard: true
    }
    
    sshConfig.privateKey = fs.readFileSync(path.resolve('./private-key.pem'),'utf-8');
    conn = new Client();
    
    conn.on('keyboard-interactive',(name, instructions, instructionsLang, prompts, finish)=>{
        finish([token])
    })
    .connect(sshConfig)
    conn.on('ready',() => {
        console.log('SSH Connected')
    })
    .on('error',(error) => { 
        console.log(error) })
    console.log(token);
}
catch(error){
    console.log(error)
}
Kaushik P
  • 31
  • 4

0 Answers0