ssh2 npm 包的 "connect" 客户端方法中 "authHandler" 的一些示例是什么?
What are some examples of "authHandler" in the "connect" client method of the ssh2 npm package?
ssh2 npm 包的“connect”客户端方法中的“authHandler”有哪些示例?
我希望重新排序这些方法 and/or 删除一些。
使用 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
class 的调用:
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!
只是添加到那个答案。使用这种方法,我仍然收到一条错误消息 'Invalid username'。
问题是,即使使用 authHandler() 并在回调中返回凭据,您仍然需要在基本配置对象中提及 'username',如下所示:
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
后解决
ssh2 npm 包的“connect”客户端方法中的“authHandler”有哪些示例?
我希望重新排序这些方法 and/or 删除一些。
使用 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
class 的调用:
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! 只是添加到那个答案。使用这种方法,我仍然收到一条错误消息 'Invalid username'。
问题是,即使使用 authHandler() 并在回调中返回凭据,您仍然需要在基本配置对象中提及 'username',如下所示:
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
后解决