1

I have node v14.17.0 and ssh2 1.1.0 https://www.npmjs.com/package/ssh2

I have tried to get the connection working with code below but it crashes on TypeError: NodeSSH is not a constructor

I have also tried

var NodeSSH= require('ssh2');

var c = new NodeSSH();

And

var NodeSSH= require('ssh2').Client;

And

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
    });

I can't seem to figure out what is causing this.


I think you should do something like this, the way you are getting the instance is incorrect.


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
    });

You should check the documentation how to use it though you have already shared it in the question. I suggest you going through it once.

4

1 回答 1

1

我认为你应该做这样的事情,你获取实例的方式是不正确的。


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
    });

尽管您已经在问题中共享了它,但您应该查看文档如何使用它。我建议你经历一次。

于 2021-07-21T07:01:53.020 回答