在node.js中检索网络接口列表(ioctl SIOCGIFCONF)

发布于 2024-12-10 04:52:15 字数 461 浏览 0 评论 0原文

我是节点新手,正在利用 node_pcap 编写一个节点应用程序来捕获数据包数据并用它做有趣的事情。捕获数据的输入之一是要侦听的网络接口,即“eth0”。

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

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

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

谢谢您的宝贵时间!

I'm new to node and am hacking together a node application utilizing node_pcap to capture packet data and do interesting things with it. One of the inputs to capturing data is the network interface to listen on, i.e. "eth0".

I thought it would be really great if I could programmatically look up the available interfaces on the system and present them to the user of the program and allow them to pick which interface to listen on. In C, I would use ioctl (or ioctlsocket with winsock) using SIOCGIFCONF.

My question is, does there currently exist a mechanism to do this in node? I've searched quite a bit and haven't arrived to any such solution.

If this functionality does not currently exist, I would assume I'd be able to write a Module binding in C/C++ using ioctl to accomplish this.

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2024-12-17 04:52:15

更新自 Node 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/最新/api/os.html#os.getNetworkInterfaces

Update valid as of Node 13.7.0

This has been renamed since this answer was submitted. It is now just networkInterfaces() like this:

require('os').networkInterfaces()

Or probably preferably like this:

import { networkInterfaces } from 'os';

const interfaces = networkInterfaces();

New docs url: https://nodejs.org/docs/latest/api/os.html#os_os_networkinterfaces

Original answer

As of Node.js 0.6.0 you have

require('os').getNetworkInterfaces()

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

雪花飘飘的天空 2024-12-17 04:52:15

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

 Object.keys(os.getNetworkInterfaces())
  // [ 'lo0', 'en0', 'en3', 'awdl0' ]

If you want to list only the name of interfaces :

 Object.keys(os.getNetworkInterfaces())
  // [ 'lo0', 'en0', 'en3', 'awdl0' ]
離殇 2024-12-17 04:52:15

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

os.networkInterfaces() method returns an object containing only network interfaces that have been assigned a network addresS but if we want all network card in machine we can use this method

var shell = require('shelljs'); 

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

this interfaceCard has list of all network interfaces

output will be

[ '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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文