我希望有一个REST API,可以在Web浏览器中使用ssh2和loopback通过REST请求从Cisco CLI命令输入返回一些cisco路由器CLI输出。
由于这些错误,我无法弄明白。
下面的代码将运行几毫秒返回一些正确和预期的输出但是然后打破服务器端的环回实例将中断/停止。
遇到错误:
C:\LOCAL_APPS\Loopback\filedir\node_modules\async\dist\async.js:955
if (fn === null) throw new Error("Callback was already called.");
^
Error: Callback was already called.
at C:\LOCAL_APPS\Loopback\filedir\node_modules\async\dist\async.js:955:32
at C:\LOCAL_APPS\Loopback\filedir\node_modules\async\dist\async.js:3871:13
at interceptInvocationErrors
(C:\LOCAL_APPS\Loopback\filedir\node_modules\strong-remoting\lib\remote-
objects.js:713:22)
at C:\LOCAL_APPS\Loopback\filedir\node_modules\loopback-phase\node_modules\async\lib\async.js:154:25
at C:\LOCAL_APPS\Loopback\filedir\node_modules\loopback-phase\node_modules\async\lib\async.js:154:25
at C:\LOCAL_APPS\Loopback\filedir\node_modules\loopback-phase\node_modules\async\lib\async.js:154:25
at C:\LOCAL_APPS\Loopback\filedir\node_modules\strong-remoting\lib\remote-objects.js:679:7
at C:\LOCAL_APPS\Loopback\filedir\node_modules\strong-remoting\lib\http-context.js:305:7
at callback (C:\LOCAL_APPS\Loopback\filedir\node_modules\strong-remoting\lib\shared-method.js:
Stream :: close :: code: 0, signal: undefined
events.js:183
throw er; // Unhandled 'error' event
^
Error: write ECONNABORTED
at _errnoException (util.js:1024:11)
at Socket._writeGeneric (net.js:767:25)
at Socket._write (net.js:786:8)
at doWrite (_stream_writable.js:387:12)
at writeOrBuffer (_stream_writable.js:373:5)
at Socket.Writable.write (_stream_writable.js:290:11)
at Socket.write (net.js:704:40)
at SSH2Stream.ondata (_stream_readable.js:639:20)
在common / models / device.js下:
'use strict';
module.exports = function(Device) {
Device.getIPInterfaceBrief = function(ipaddr,cb){
var filter = {
include:{
relation: 'infosheet',
scope:{
fields:['data']
}
}
}
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.exec('show ip int br', 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) {
cb(null,"data: " + 'done') //or cb(null,"data: " + data)
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: ipaddr,
port: 22,
username: 'routerusername',
password: 'passwordhere'
});
console.log("check 1");
};
Device.remoteMethod('getIPInterfaceBrief',{
description: "Returns the brief interface information of a device",
accepts:{
arg:'ipaddr',
type: 'string',
required: false
},
http:{
path:'/:ipaddr/getIPInterfaceBrief',
verb: 'get'
},
returns:{
arg:'interfaceinfo',
type:'any'
}
});
}; //module.exports
REST TEST URL:
http:// localhost:3000/api/adevices/some-ip-here/getIPInterfaceBrief
几秒钟后,API响应了获取REST请求时遇到错误:
答案 0 :(得分:1)
我暂时添加了一些try catch语句,它以某种方式工作。
我认为环回不是问题的一部分。
希望你有更好的解决方法,因为代码可以在Linux系统等其他机器上运行,但是对于思科路由器来说,线路有一些问题" conn.end"。
添加了try和catch以捕获错误..
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
try {
conn.end();
}catch (err){
console.log("error occured",err);
}
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});