123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- const os = require('os');
- const { exec } = require('child_process');
- const path = require('path');
- const constants = require('./constants');
- const fs = require('fs');
- // 判断是否为物理网卡的函数
- function isPhysicalNetworkInterface(name, description) {
- // 默认的真实物理网卡名称(如 "以太网")
- const defaultPhysicalNames = ["以太网", "eth0", "en0"]; // 可以根据需要扩展
- // 如果名称匹配默认的真实物理网卡,直接返回 true
- if (defaultPhysicalNames.includes(name)) {
- return true;
- }
- // 排除虚拟网卡的关键词
- const virtualKeywords = [
- "virtual", "hyper-v", "virtualbox", "vmware", "veth", "virbr", "tunnel",
- "docker", "loopback", "vpn", "ppp", "slip", "bridge", "vnic", "vif",
- "tap", "vlan", "vswitch", "vxlan", "gre", "ipsec", "vrf", "vport",
- "vnet", "vmac", "vxnet"
- ];
- // 检查网卡描述或名称中是否包含虚拟关键词
- const lowerName = name.toLowerCase();
- const lowerDescription = (description || '').toLowerCase();
- for (const keyword of virtualKeywords) {
- if (lowerName.includes(keyword) || lowerDescription.includes(keyword)) {
- return false; // 是虚拟网卡
- }
- }
- // 默认认为是物理网卡
- return true;
- }
- // 获取所有网络接口信息
- function getNetworkInterfaces() {
- // 执行 wmic 命令来获取网卡描述信息
- exec('wmic nic get Name,Description,NetConnectionID', (error, stdout, stderr) => {
- if (error) {
- console.error(`执行命令时出错: ${error.message}`);
- return;
- }
- if (stderr) {
- console.error(`命令执行过程中出现错误: ${stderr}`);
- return;
- }
- // 解析命令输出
- const lines = stdout.trim().split('\n');
- const headers = lines[0].trim().split(/\s+/);
- const devices = [];
- for (let i = 1; i < lines.length; i++) {
- const values = lines[i].trim().split(/\s{2,}/);
- if (values.length === headers.length) {
- const device = {};
- for (let j = 0; j < headers.length; j++) {
- device[headers[j]] = values[j];
- }
- devices.push(device);
- }
- }
- // 获取网络接口信息
- const networkInterfaces = os.networkInterfaces();
- // 为每个网卡添加 IPv4 地址信息
- devices.forEach((device) => {
- const netInterface = networkInterfaces[device.NetConnectionID];
- if (netInterface) {
- const ipv4Info = netInterface.find((info) => info.family === 'IPv4');
- if (ipv4Info) {
- device.ipv4 = ipv4Info.address;
- } else {
- device.ipv4 = '未找到 IPv4 地址';
- }
- } else {
- device.ipv4 = '未找到对应的网络接口';
- }
- });
-
- });
- const interfaces = os.networkInterfaces();
-
- //console.log(interfaces);
- const result = [];
- // 遍历所有网络接口
- for (const [name, details] of Object.entries(interfaces)) {
- for (const detail of details) {
- if (detail.family === 'IPv4' && !detail.internal) {
- const isPhysical = isPhysicalNetworkInterface(name, detail.mac);
- const networkInfo = {
- name: name,
- mac: detail.mac,
- ip: detail.address,
- physical: isPhysical ? 1 : 0
- };
- result.push(networkInfo);
- }
- }
- }
- // 将 physical: 1 的网卡排在前面
- result.sort((a, b) => b.physical - a.physical);
- // 读取文件内容
- let filePath = path.join(constants.serverPath, 'certificate.bat');
-
- filePath == `"${filePath}"`;
- //console.log("filePath:", filePath);
- fs.readFile(filePath, 'utf8', (err, data) => {
- if (err) {
- console.error('读取文件失败:', err);
- return;
- }
- // 替换内容
- const oldContent = '192.168.8.150';
- const newContent = result[0].ip;
- const updatedData = data.replace(new RegExp(oldContent, 'g'), newContent);
- //console.log("新的文件内容", updatedData);
- // 将更新后的内容写回文件
- fs.writeFile(filePath, updatedData, 'utf8', (err) => {
- if (err) {
- console.error('写入文件失败:', err);
- return;
- }
- console.log('文件内容已成功替换!');
- });
- });
- // 输出网卡描述信息和 IPv4 地址
- //console.log('网卡描述信息和 IPv4 地址:', result);
- result.forEach((device) => {
- console.log(`名称: ${device.name}, 是否物理网卡: ${device.physical},mac地址:${device.mac}, IPv4 地址: ${device.ip}`);
- });
- return result;
- }
- // 导出模块
- module.exports = {
- getNetworkInterfaces
- };
|