我对nodejs相当新。我正在尝试使用主机,端口,用户名和密码连接到我的网络服务器,并获得正常响应,如下所示。从命令行执行文件不给我任何输出,它只给我一个空的命令行。删除ip,port或username / pwd中的任何一个都会给我带来错误。我做错了什么?
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.exec('uptime', function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: '192.168.100.100',
port: 2122,
username: '!@#!@#!@',
password: '!@#!@#!'
});
// example output:
// Client :: ready
// STDOUT: 17:41:15 up 22 days, 18:09, 1 user, load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close
答案 0 :(得分:0)
您需要在任何其他处理程序之前将'error'
事件处理程序附加到实际的连接对象。
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('error', function(err) {
console.log('SSH - Connection Error: ' + err);
});
conn.on('end', function() {
console.log('SSH - Connection Closed');
});
conn.on('ready', function() {
// code to work with SSH
});
conn.connect({
host: '192.168.100.100',
port: 2122,
username: '!@#!@#!@',
password: '!@#!@#!'
});
答案 1 :(得分:0)
我遇到了同样的问题。做下面解决它 -
const { Client } = require('ssh2');