ssh2 npm 包的“connect”客户端方法中的“authHandler”有哪些示例?
我正在寻找重新排序方法和/或删除一些方法。
使用文档,我将尝试提供一个基本示例,其中包括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',
});
}
});
感谢@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 后解决