使用 nodejs 与 mikrotik 路由器或一般的 bash通信

问题描述

我从 https://www.npmjs.com/package/mikronode 开始,但即使我安装了正确版本的依赖项(或至少足够接近,因此它不会因错误退出),它也无法连接。

如果我只有一个 shell_exec 函数返回一个 promise 并且有一个超时(因为收到最后一行文本,在下面的例子中),我可以编写我自己的版本。我什至不在乎 stdout 和 stderr 是分开的还是混在一起的。不幸的是,我几乎没有使用 promise 或 nodejs exec 函数的经验(过去主要使用 PHP 用于此目的),几个小时后我未能找到具有这两种功能的工作示例。

我需要的是可以通过以下方式在异步函数中使用的东西:

var res= await ShellExecReturnLineWithTimeout("ssh admin@192.168.88.1",1000);
if(res.includes('password:'){//check if response contains a password prompt
  res=await ShellExecReturnLineWithTimeout("the-password",200);//enter password
  //scroll through "you are Now connected" text till command prompt displayed
  while(res!="timeout" && res!="error" && !res.includes('[admin@my-router] >')
    res=await ShellExecReturnLineWithTimeout("",200);
  if(res.includes('[admin@my-router] >'){
    ...
    //run commands and look at output with console.log(res)
    ...
    ShellExecReturnLineWithTimeout("quit",200); 
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)