16

我是 node 新手,我正在使用node_pcap来破解一个 node 应用程序来捕获数据包数据并用它做一些有趣的事情。捕获数据的输入之一是要监听的网络接口,即“eth0”。

我认为如果我能以编程方式查找系统上可用的接口并将它们呈现给程序的用户并允许他们选择要收听的接口,那将是非常棒的。在 C 语言中,我将使用ioctl(或带有 winsock 的 ioctlsocket)使用 SIOCGIFCONF。

我的问题是,目前是否存在在节点中执行此操作的机制?我已经搜索了很多,还没有找到任何这样的解决方案。

如果此功能当前不存在,我会假设我可以使用 ioctl 在 C/C++ 中编写模块绑定来完成此操作。

感谢您的时间!

4

3 回答 3

21

从节点 13.7.0 开始更新有效

自从提交此答案以来,它已被重命名。现在是networkInterfaces()这样的:

require('os').networkInterfaces()

或者可能最好是这样:

import { networkInterfaces } from 'os';

const interfaces = networkInterfaces();

新文档网址:https ://nodejs.org/docs/latest/api/os.html#os_os_networkinterfaces

原始答案

从 Node.js 0.6.0 开始,您拥有

require('os').getNetworkInterfaces()

http://nodejs.org/docs/latest/api/os.html#os.getNetworkInterfaces

于 2011-10-15T10:10:55.950 回答
1

如果您只想列出接口的名称:

 Object.keys(os.getNetworkInterfaces())
  // [ 'lo0', 'en0', 'en3', 'awdl0' ]
于 2016-11-13T18:46:57.577 回答
0

os.networkInterfaces() 方法返回一个仅包含已分配网络地址的网络接口的对象,但如果我们想要机器中的所有网卡,我们可以使用此方法

var shell = require('shelljs'); 

 var interfaceCard = shell.ls('/sys/class/net');

这个 interfaceCard 有所有网络接口的列表

输出将是

[ 'eth0',
'eth1',
'lo',
 stdout: 'eth0\neth1\nlo\n',
  stderr: null,
code: 0,
cat: [Function: bound ],
exec: [Function: bound ],
grep: [Function: bound ],
head: [Function: bound ],
 sed: [Function: bound ],
sort: [Function: bound ],
 tail: [Function: bound ],
  to: [Function: bound ],
 toEnd: [Function: bound ],
 uniq: [Function: bound ] ]

 interfaceCard=interfaceCard.stdout.split('\n');

 interfaceCard = eth0, eth1, lo
于 2017-06-16T06:56:41.240 回答