简体   繁体   English

即使使用手动ssh仍无法通过ssh2模块使用nodejs进行身份验证

[英]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). 我正在尝试将nodejs(v0.8.2)与ssh2模块一起使用,以从远程计算机(使用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. 我可以使用密码或rsa密钥手动SSH到远程计算机,但是以下代码无法以某种方式产生成功的身份验证。 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.) 这是一个代码片段(我剥离了执行sftp的代码,以使其更易于理解-我在这里关心的只是我无法通过身份验证。)

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: 当我运行这段代码时,我得到一个'Connection :: connect'响应,但是直到ssh超时,我才得到Connection :: end和Connection ::快速连续关闭的信息:

node testssh.js
Connection :: connect
Connection :: end
Connection :: close

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. 确保将tryKeyboard: true传递给keyboard-interactive事件,并对提示进行一些有趣keyboard-interactive ,并使用包含这些提示答案的数组调用finish回调。

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+. 确保您正在使用最新版本的ssh2模块,并且正在使用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 . 如果之后仍然遇到麻烦,可以通过在传递给connect()的对象中设置调试输出来启用调试输出: debug: console.log That should hopefully give a better sense of what's going on behind the scenes. 希望可以更好地了解幕后情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM