【发布时间】:2020-09-15 22:31:01
【问题描述】:
我对编程很陌生,我正在努力努力——也许这个问题真的不值得问,但我没有取得任何进展,因为……太久了。
我正在尝试在 node.js 脚本中对连接到本地网络的所有设备进行发现搜索。
我发现这个 npm 模块应该可以完成这项工作:https://www.npmjs.com/package/local-devices/v/3.0.0
所以我只是尝试复制粘贴他们给出的示例,以在我的网络中搜索所有设备:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
devices /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
但如果我现在运行脚本,什么都不会发生。
到目前为止我已经尝试过: 1.我尝试将输出保存在一个常量中,并尝试在控制台上输出它
const found = find().then(devices => {
devices
/*[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
return devices
})
console.log(found);
那么控制台输出是Promise { <pending> }。我还没想好怎么处理这个。
- 有这个命令行参数
arp -a,如果在命令行中执行,会在终端中列出网络中所有设备的所有IP地址。所以它基本上完全符合我的要求,但我需要在下面的代码中使用该输出(我需要从这个列表中找到一个智能插头的特定 IP 地址,但这是另一回事)。
如何获取此命令行参数,并在我的 javascript 代码/我的 node.js 脚本中执行它,并将输出保存在变量/const 中?
最好的问候, 迈克尔
【问题讨论】:
标签: javascript node.js search command-line discovery