【发布时间】:2016-12-10 17:24:22
【问题描述】:
我想创建一个使用 npm 包 ssh2 - https://www.npmjs.com/package/ssh2 的 Angular2 服务。
我已经按照我的预期安装和配置了它 - 我的服务的代码如下所示(此代码基于 ssh2 npm 站点和 github repo 上显示的第一个示例(唯一的变化是我使用的是密码而不是用于身份验证的密钥)):
const Client = require('ssh2').Client;
const fs = require('fs');
import { NextObserver } from 'rxjs/Observer';
import { Observable } from 'rxjs/Rx';
export class SSHClient {
username: string;
password: string;
host: string;
port: number;
ssh: any;
constructor() {
}
init(username, password, host, port) {
console.log("initiating SSH connection to:" + host + "on port:" + port);
this.username = username;
this.password = password;
this.host = host;
this.port = port;
console.log("Connecting now...");
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: this.host,
port: this.port,
username: this.username,
password: this.password
});
console.log("SSH Client created and demo has run");
}
}
我在我的组件中按此调用服务:
this.ssh.init("username","password","my.little.host",22);
并且我希望通过控制台获取在我的服务器上通过 SSH 执行的 uptime 命令的输出。然而,而不是那个 - 我得到这个:
EXCEPTION: Uncaught (in promise): Error: Error in ./ServerBrowserComponent class ServerBrowserComponent_Host - inline template:0:0 caused by: Cannot read property 'connect' of undefined
TypeError: Cannot read property 'connect' of undefined
at SSHClient.init (file:///Users/myapp/build/app.js:40259:12)
at new ServerBrowserComponent (file:///Users/myapp/build/app.js:34158:19)
at new Wrapper_ServerBrowserComponent (/AppModule/ServerBrowserComponent/wrapper.ngfactory.js:7:18)
at DebugAppView._View_ServerBrowserComponent_Host0.createInternal (/AppModule/ServerBrowserComponent/host.ngfactory.js:16:38)
at DebugAppView.AppView.create (file:///Users/myapp/build/app.js:27754:26)
at DebugAppView.create (file:///Users/myapp/build/app.js:27954:49)
at ComponentFactory.create (file:///Users/myapp/build/app.js:24393:41)
at ViewContainerRef_.createComponent (file:///Users/myapp/build/app.js:23441:50)
at RouterOutlet.activate (file:///Users/myapp/build/app.js:72709:45)
at ActivateRoutes.placeComponentIntoOutlet (file:///Users/myapp/build/app.js:72207:21)
ORIGINAL STACKTRACE:
Error: Uncaught (in promise): Error: Error in ./ServerBrowserComponent class ServerBrowserComponent_Host - inline template:0:0 caused by: Cannot read property 'connect' of undefined
TypeError: Cannot read property 'connect' of undefined
at SSHClient.init (ssh.service.ts:49)
at new ServerBrowserComponent (server.browser.component.ts:31)
at new Wrapper_ServerBrowserComponent (wrapper.ngfactory.js:7)
at DebugAppView._View_ServerBrowserComponent_Host0.createInternal (host.ngfactory.js:16)
at DebugAppView.AppView.create (core.umd.js:9170)
at DebugAppView.create (core.umd.js:9370)
at ComponentFactory.create (core.umd.js:5809)
at ViewContainerRef_.createComponent (core.umd.js:4857)
at RouterOutlet.activate (router.umd.js:3457)
at ActivateRoutes.placeComponentIntoOutlet (router.umd.js:2955)
at resolvePromise (zone-node.js:468)
at zone-node.js:445
at ZoneDelegate.invoke (zone-node.js:232)
at Object.onInvoke (core.umd.js:6206)
at ZoneDelegate.invoke (zone-node.js:231)
at Zone.run (zone-node.js:114)
at zone-node.js:502
at ZoneDelegate.invokeTask (zone-node.js:265)
at Object.onInvokeTask (core.umd.js:6197)
at ZoneDelegate.invokeTask (zone-node.js:264)
Unhandled Promise rejection: Error in ./ServerBrowserComponent class ServerBrowserComponent_Host - inline template:0:0 caused by: Cannot read property 'connect' of undefined ; Zone: angular ; Task: Promise.then ; Value: ViewWrappedError TypeError: Cannot read property 'connect' of undefined
at SSHClient.init (file:///Users/myapp/build/app.js:40259:12)
at new ServerBrowserComponent (file:///Users/myapp/build/app.js:34158:19)
at new Wrapper_ServerBrowserComponent (/AppModule/ServerBrowserComponent/wrapper.ngfactory.js:7:18)
at DebugAppView._View_ServerBrowserComponent_Host0.createInternal (/AppModule/ServerBrowserComponent/host.ngfactory.js:16:38)
at DebugAppView.AppView.create (file:///Users/myapp/build/app.js:27754:26)
at DebugAppView.create (file:///Users/myapp/build/app.js:27954:49)
at ComponentFactory.create (file:///Users/myapp/build/app.js:24393:41)
at ViewContainerRef_.createComponent (file:///Users/myapp/build/app.js:23441:50)
at RouterOutlet.activate (file:///Users/myapp/build/app.js:72709:45)
at ActivateRoutes.placeComponentIntoOutlet (file:///Users/myapp/build/app.js:72207:21)
Error: Uncaught (in promise): Error: Error in ./ServerBrowserComponent class ServerBrowserComponent_Host - inline template:0:0 caused by: Cannot read property 'connect' of undefined(…)
其中大部分似乎都在挣扎,因为:Cannot read property 'connect' of undefined——但我正在关注示例文档。有人可以帮助我理解为什么这不起作用 - 以及我该如何解决它?
我试图将代码从我的项目中提取出来并放入它自己的文件中,如下所示:
const Client = require('ssh2').Client;
console.log("Connecting now...");
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: 'my.little.host',
port: 22,
username: 'uname',
password: 'pwd'
});
console.log("SSH Client created and demo has run");
但我得到了同样的错误:
var conn = new Client();
^
ReferenceError: Client is not defined
at Object.<anonymous> (/Users/myapp/attempt.js:2:21)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
所以这里出了点简单的问题,可能是什么!?
【问题讨论】:
-
你安装的是什么版本的
ssh2? -
"ssh2": "^0.5.4",
-
如果您只对它自己进行测试(没有类、导出等),相同的
ssh2代码是否有效? -
Acutally no - 我得到完全相同的错误 - 请参阅上面的更新问题
-
您似乎缺少
const Client = require('ssh2').Client行。
标签: node.js angular ssh npm angular-services