Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to authenticate using nodejs with ssh2 module, even though manual ssh works

I am trying to use nodejs (v0.8.2) with the ssh2 module to grab some files from a remote machine (using sftp). I am able to ssh to the remote machine by hand using either a password or rsa keys, but somehow the following code fails to yield a successful authentication. Any ideas about what might be going wrong?

Here is a code snippet (I've stripped out the code that does the sftp to make it easier to understand -- all I care about here is that I'm failing to authenticate.)

var express = require('express'),
    fs = require('fs'),
    Connection = require('ssh2');

var c = new Connection();
c.on('connect', function() {
  console.log('Connection :: connect');
});
c.on('ready', function() {
  console.log('Connection :: ready');
});
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.on('keyboard-interactive', function() {
  console.log('Connection :: keyboard-interactive');
});
c.connect({
  host: '9.xx.yy.zzz',
  port: 22,
  username: 'my_username_on_remote_machine',
  password: 'my_password_on_remote_machine'
});

When I run this code, I get a 'Connection :: connect' response, but nothing further until the ssh times out and I get the Connection :: end and Connection :: close in rapid succession:

node testssh.js
Connection :: connect
Connection :: end
Connection :: close
like image 213
user2548390 Avatar asked Apr 16 '26 08:04

user2548390


1 Answers

Make sure to pass tryKeyboard: true, do something interesting with the prompts to the keyboard-interactive event, and call the finish callback with array holding the answers to those prompts.

c.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
  console.log('Connection :: keyboard-interactive');
  finish(['my_password_on_remote_machine']);
});

c.connect({
  host: '9.xx.yy.zzz',
  port: 22,
  username: 'my_username_on_remote_machine',
  tryKeyboard: true
});
like image 132
Joe Hildebrand Avatar answered Apr 17 '26 21:04

Joe Hildebrand