问题描述
ssh2 npm 包的“connect”客户端方法中“authHandler”的例子有哪些?
解决方法
使用 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',});
}
});