使用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',
});
}
});