【问题标题】:Loopback/Strongloop failed with ssh2 function callback Error: write ECONNABORTEDLoopback/Strongloop 因 ssh2 函数回调而失败 错误:写入 ECONNABORTED
【发布时间】:2018-02-21 15:28:06
【问题描述】:

我想要一个 REST API,它可以通过 REST 请求使用 ssh2 和环回,在 Web 浏览器中返回来自 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 测试网址:

http:// localhost:3000/api/adevices/some-ip-here/getIPInterfaceBrief

API 响应获取 REST 请求几秒钟后遇到错误:

【问题讨论】:

    标签: javascript loopbackjs strongloop cisco libssh2


    【解决方案1】:

    我临时添加了一些 try catch 语句,它以某种方式起作用。

    我认为环回不是问题的一部分。

    希望您有更好的解决方案,因为代码可以在 Linux 系统等其他机器上运行,但是对于 Cisco 路由器,“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);
    });
    

    My test did capture the error see here

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 2020-09-22
      • 1970-01-01
      • 2021-01-27
      • 2018-03-23
      • 2021-08-09
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      相关资源
      最近更新 更多