我有节点v14.17.0
和ssh21.1.0
https://www.npmjs.com/package/ssh2
我试图用下面的代码使连接正常工作,但它在TypeError: NodeSSH is not a constructor
上崩溃
我也试过
var NodeSSH= require('ssh2');
var c = new NodeSSH();
和
var NodeSSH= require('ssh2').Client;
和
const {NodeSSH} = require('ssh2');
const c = new NodeSSH();
c.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
console.log('Connection :: keyboard-interactive');
finish(['pswd']);
}).on('end', function() {
console.log('Connection :: end');
console.log(callback());
}).on('error', function(error) {
console.log(error);
}).connect({
host: 'XX.XX.XXX.XXX',
username: 'usr',
port: "22",
tryKeyboard: true,
debug: console.log
});
我似乎不明白是什么原因造成的。
我认为应该这样做,获取实例的方式不正确。
const { Client } = require('ssh2'); // it exports Client not NodeSSH
const conn = new Client();
conn.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
console.log('Connection :: keyboard-interactive');
finish(['pswd']);
}).on('end', function() {
console.log('Connection :: end');
console.log(callback());
}).on('error', function(error) {
console.log(error);
}).connect({
host: 'XX.XX.XXX.XXX',
username: 'usr',
port: "22",
tryKeyboard: true,
debug: console.log
});
尽管您已经在问题中分享了它,但您应该查看如何使用它的文档。我建议你复习一遍。