【问题标题】:What are some examples of "authHandler" in the "connect" client method of the ssh2 npm package?ssh2 npm 包的“connect”客户端方法中的“authHandler”有哪些示例?
【发布时间】:2021-08-27 22:41:12
【问题描述】:

ssh2 npm 包的“connect”客户端方法中的“authHandler”有哪些例子?

我正在寻求重新排序方法和/或删除一些方法。

【问题讨论】:

  • 我绝不是要冒犯您作为作者。我只是陈述一个观察。我的意思是,由于您对问题/问题的了解最多,因此在您的眼中将很容易理解。但是,当我阅读问题时,我不相信它很清楚(我的观点)。但是,其他人可能不会。该文档显示了authHandler 的示例。 npmjs.com/package/ssh2#user-content-client-methods
  • @Riddell 不用担心,我不会对建设性的批评感到生气——只有那些随便不尊重他人努力而没有投入任何努力的人。我对你在这里度过的时间表示感谢。至于引用的例子,那些似乎只是对象的一个​​例子?作为一个相当新的 js 开发人员,我(以及我见过的其他一些人)需要一个更广泛地使用该方法的示例才能理解它。

标签: node.js ssh2


【解决方案1】:

使用documentation,我将尝试提供一个基本示例,其中包括您问题中提到的authHandler 的用法。

// Require Client Class from ssh2
const { Client } = require('ssh2');

// Create instance of Client (aka connection)
const conn = new Client();

// Create our ready event that's called after we connect
conn.on('ready', () => {
    console.log('Client :: ready');
});

// Connect with a config object passed in the parameters
conn.connect({
    host: '192.168.100.100',
    port: 22, // SSH

    // Authentication Handler
    authHandler: function (methodsLeft, partialSuccess, callback) {

        // Code e.g. get credentials from database
    
        // Once your logic is complete invoke the callback
        // http://npmjs.com/package/ssh2#client-examples
        callback({
            type: 'password',
            username: 'foo',
            password: 'bar',
        });
    }
});

如果凭据发生更改,上述内容应提供一个工作示例。代码可以稍微简洁一些,conn 类的调用可以像这样链接:

conn.on('ready', () => {
    console.log('Client :: ready');
}).connect({ // Chained
    host: '192.168.100.100',
    port: 22, // SSH

    // Authentication Handler
    authHandler: function (methodsLeft, partialSuccess, callback) {

        // Code e.g. get credentials from database
    
        // Once your logic is complete invoke the callback
        // http://npmjs.com/package/ssh2#client-examples
        callback({
            type: 'password',
            username: 'foo',
            password: 'bar',
        });
    }
});

【讨论】:

    【解决方案2】:

    感谢@Riddell! 只是添加到那个答案。使用这种方法,我仍然收到一个错误提示“无效的用户名”。

    问题是,即使使用 authHandler() 并在回调中返回凭据,您仍然需要在基本配置对象中提及“用户名”,如下所示:

    conn.on('ready', () => {
        console.log('Client :: ready');
    }).connect({
        host: '192.168.100.100',
        port: 22,
        username: 'foo',
    
        authHandler: function (methodsLeft, partialSuccess, callback) {
            callback({
                type: 'password',
                username: 'foo',
                password: 'bar',
            });
        }
    });
    

    此外,如果有人在使用此方法时遇到 Auth Type 错误,则需要升级 ssh2 软件包版本。使用 ssh2@0.8.9 时遇到此问题,升级到 ssh2@1.5.0 后解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-10
      • 2015-12-12
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 2016-05-06
      相关资源
      最近更新 更多