我想创建一个使用 npm 包 ssh2 的 Angular2 服务 - https://www.npmjs.com/package/ssh2 .
我已经按照预期安装和配置了它 - 我的服务代码如下所示(此代码基于 ssh2 npm 站点和 github 存储库上显示的第一个示例(唯一的变化是我使用密码而不是用于身份验证的 key )):
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(…)
其中大部分似乎都在挣扎,因为:无法读取未定义的属性“connect”
——但我正在关注示例文档。有人可以帮助我理解为什么这不起作用 - 以及我该如何解决它?
我试图将代码从我的项目中提取出来,放入它自己的文件中,如下所示:
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-package 用于 node.js,而不是前端/web。
浏览器不能那样使用网络(电子使用两个进程,浏览器/渲染器进程无法访问您需要的网络功能)
你的 electron 应用有 main.js 文件吗?
这就是您需要实现 ssh 功能的地方。
即在主进程中,而不是在渲染器进程中。
http://electron.atom.io/docs/tutorial/quick-start/#main-process
关于node.js - 在电子下运行的 Angular2 应用程序中使用 npm 包 ssh2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646733/