如何使用NodeJS检测计算机上已连接的4G dongle设备
NodeJS 检测已连接4G Dongle设备实现方案
你目前使用的os.networkInterfaces()仅能返回基础网络配置,不包含设备总线类型、硬件描述等信息,无法直接筛选4G Dongle,可通过以下两种方案实现:
方案1:调用系统原生命令实现(无第三方依赖)
不同平台的4G Dongle网络接口都有统一特征:属于USB总线、名称/描述包含WWAN/LTE/4G/Modem类关键词,通过调用对应平台的系统命令过滤即可:
核心实现代码
const { exec } = require('child_process'); const os = require('os'); function get4GDongles() { return new Promise((resolve, reject) => { let cmd; const platform = os.platform(); // 匹配关键词列表,可根据实际设备扩展 const matchKeywords = ['4g', 'lte', 'wwan', 'modem', 'dongle', '移动网络', '联通', '电信']; if (platform === 'win32') { cmd = 'wmic nic where "PNPDeviceID like \'USB%\'" get Name,MACAddress,NetConnectionID /format:csv'; } else if (platform === 'linux') { cmd = `ls /sys/class/net/ | xargs -I {} sh -c 'echo "{} $(udevadm info --query=property --path=/sys/class/net/{})"'`; } else if (platform === 'darwin') { cmd = 'system_profiler SPNetworkDataType'; } else { return reject(new Error('不支持的操作系统')); } exec(cmd, (err, stdout) => { if (err) return reject(err); const dongles = []; const allNetInterfaces = os.networkInterfaces(); Object.values(allNetInterfaces).forEach(netInfo => { const info = netInfo[0]; if (!info || info.internal) return; const lowerDesc = JSON.stringify(stdout).toLowerCase(); const hasKeyword = matchKeywords.some(key => lowerDesc.includes(key) && lowerDesc.includes(info.mac.toLowerCase())); if (hasKeyword) { dongles.push({ name: info.name || '未知4G Dongle', mac: info.mac, address: info.address, family: info.family }) } }) resolve(dongles); }) }) } // 调用示例 get4GDongles().then(console.log).catch(console.error)
方案2:结合USB设备枚举库实现(匹配精度更高)
通过usb库枚举所有USB设备,匹配4G Dongle的VID/PID或者设备描述,再通过MAC地址关联到os.networkInterfaces()的结果,即可精准筛选。
实现步骤
- 安装依赖
npm install usb
- 核心代码
const usb = require('usb'); const os = require('os'); function get4GDongles() { // 可提前收集所用4G Dongle的VID/PID加入列表,也可以通过设备描述匹配 const matchVIDPID = [[0x12d1, 0x1506]]; // 示例为华为部分4G Dongle的VID/PID const matchKeywords = ['4g', 'lte', 'wwan', 'modem', 'dongle']; const allNetIf = os.networkInterfaces(); const dongles = []; // 枚举所有USB设备 const devices = usb.getDeviceList(); devices.forEach(dev => { const desc = dev.deviceDescriptor; const isMatchByVIDPID = matchVIDPID.some(([vid, pid]) => desc.idVendor === vid && desc.idProduct === pid); let isMatchByKeyword = false; try { dev.getStringDescriptor(desc.iManufacturer, (err, str) => { if (str) isMatchByKeyword = matchKeywords.some(key => str.toLowerCase().includes(key)); }); dev.getStringDescriptor(desc.iProduct, (err, str) => { if (str) isMatchByKeyword = isMatchByKeyword || matchKeywords.some(key => str.toLowerCase().includes(key)); }); } catch(e) {} if (isMatchByVIDPID || isMatchByKeyword) { // 关联对应网络接口,可根据实际情况调整关联逻辑 Object.values(allNetIf).forEach(netInfo => { const info = netInfo[0]; if (info && !info.internal) { dongles.push({ vendorId: desc.idVendor, productId: desc.idProduct, mac: info.mac, ip: info.address, name: info.name }) } }) } }) return dongles; } // 调用示例 console.log(get4GDongles())
注意事项
- 部分小众4G Dongle的设备描述不含关键词,可提前收集设备的VID/PID加入匹配列表,精度更高
- Linux环境下运行需要root权限才能读取完整的USB设备信息
内容的提问来源于stack exchange,提问作者Henri
相关产品推荐
相关产品推荐

