admin管理员组

文章数量:1300034

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

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
Share Improve this question edited Jul 3, 2013 at 23:30 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jul 3, 2013 at 22:32 user2548390user2548390 211 silver badge2 bronze badges 2
  • Why aren't you using the full code from the example? – raam86 Commented Jul 3, 2013 at 23:47
  • I take it you mean the full code from the example provided at github./mscdex/ssh2, under the heading "Authenticate using password, start an SFTP session, and get a directory listing:"? That is indeed what I used as a basis. I thought it would simplify my question but perhaps that was a mistake. When I run exactly the code example provided on github, only replacing the host, username, and password with my values, I get the behavior described in my original post -- no Connection :: ready; just a timeout culminating in Connection :: end and Connection :: close. – user2548390 Commented Jul 4, 2013 at 6:23
Add a ment  | 

2 Answers 2

Reset to default 7

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
});

Make sure you are using the latest version of the ssh2 module and that you are using node.js v0.8.7+.

If you are still running into trouble after that, you can enable debug output by setting this in the object passed to connect(): debug: console.log. That should hopefully give a better sense of what's going on behind the scenes.

本文标签: