当前位置: 首页 > news >正文

网卡获取模组ip失败问题解析

现象

执行网关后端程序,发现ip不显示,ifconfig也不显示ip:

ifconfig enx62fde47ffdbb enx62fde47ffdbb: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 62:fd:e4:7f:fd:bb txqueuelen 1000 (Ethernet) RX packets 150 bytes 11956 (11.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 87 bytes 7754 (7.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

但是模组已经拿到ip了

+SPCHMDATAINFO:1,1,sipa_eth0,1,172.170.11.114/16,8.8.8.8 8.8.4.4,,1400,1 notification: ^NDISDUN:sipa_usb0,1,enabled,IP,172.170.11.114,255.255.255.255,172.170.11.114,8.8.8.8,8.8.4.4

解决

用gdb检查后端程序,发现问题:mypopen执行失败。

string UsbnameGet() { string res; // string cmd = "dmesg | grep 'CDC NCM' | grep 'register' | awk '{print $5}' | cut -d: -f1"; // 这个获取的是驱动刚注册时获取的名字 // 获取到的就是实际在用的接口名: string cmd = "for i in /sys/class/net/*; do " "readlink $i/device/driver 2>/dev/null | grep -q cdc_ncm && basename $i; " "done"; if(!myPopen(cmd, res)){ hloge("check 5G net card failed"); return ""; } std::cout<<"5G网卡:"<<res<<std::endl; return res; }

可能是指令没权限,可能mypopen有问题,先逐步排查从最有可能的地方入手:

1.修改mypopen为popen

ifconfig正常显示ip,说明确实是mypopen有问题

std::string UsbnameGet() { std::string res; const char* cmd = "for i in /sys/class/net/*; do " "drv=$(readlink $i/device/driver 2>/dev/null); " "echo $drv | grep -Eq 'cdc_ncm|cdc_ether|rndis_host' && basename $i; " "done"; FILE* fp = popen(cmd, "r"); if (!fp) { perror("popen failed"); return ""; } char buf[256]; while (fgets(buf, sizeof(buf), fp)) { res += buf; } int rc = pclose(fp); std::cout << "pclose rc=" << rc << std::endl; std::cout << "RAW res=[" << res << "] len=" << res.size() << std::endl; return res; }
ifconfig enx62fde47ffdbb enx62fde47ffdbb: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.170.11.114 netmask 255.255.255.255 broadcast 0.0.0.0 inet6 fe80::a66d:3382:f0a2:fd41 prefixlen 64 scopeid 0x20<link> ether 62:fd:e4:7f:fd:bb txqueuelen 1000 (Ethernet) RX packets 159 bytes 13476 (13.4 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 119 bytes 12114 (12.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

mypopen代码如下:

static bool executeChild(const string &cmd, string &res) { FILE *fp = popen(cmd.c_str(),"r"); if(NULL == fp) { // if fork(2) or pipe(2) calls fail, or cannot callocate memory. // it does not set errno if memory allocation fails. // if the underlying fork(2) or pipe(2) fails, errno is set // appropriately. // if the type arguments is invalid, and this condition is detected, // errno is set to EINVAL. res += static_cast<string>("popen failed. ") + strerror(errno); return false; } char buffer[512] = {0}; while (fgets(buffer, sizeof(buffer), fp) != NULL) { res += buffer; } int status = pclose(fp); //❗❗❗❗❗❗❗逻辑有严重问题❗❗❗❗❗❗❗❗❗❗ if (status == 0) { return true; } if (status == -1) { res += static_cast<string>(" failed to get child status: ") + strerror(errno); } else { if (WIFEXITED(status)) { // 命令不存在,exit_status = 127; 命令不存在或无权限执行,仍然是执行成功,只是exit_status>0 res += " normal termination, exit_status = " + to_string(WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { res += " termination by a signal, signal number = " + to_string(WTERMSIG(status)); } else if (WIFSTOPPED(status)) { res += " the child is stopped, status = " + to_string(WSTOPSIG(status)); } else { res += static_cast<string>(" unknown error: ") + strerror(errno); } } return false; } bool myPopen(string cmd, string &res, int maxTimes) { if (cmd.empty()) { hloge("myPopen cmd null"); return false; } cmd += " 2>&1"; for(int tryTimes = 0; tryTimes < maxTimes; tryTimes++) { bool success = executeChild(cmd, res); // ❗❗❗❗严重问题❗❗❗❗❗ if (success) { res = hv::rtrim(res); return true; } hlogi("mySystem cmd: %s, err msg: %s, try times: %d", cmd.c_str(), res.c_str(), tryTimes+1); res.clear(); } hloge("mySystem cmd: %s, failed", cmd.c_str()); return false; } bool mySystem(string cmd) { string res; return myPopen(cmd, res); }

我的指令是:

for i in /sys/class/net/*; do drv=$(readlink $i/device/driver 2>/dev/null) echo $drv | grep -Eq 'cdc_ncm|cdc_ether|rndis_host' && basename $i done

grep -q:匹配到,退出码0,没匹配到,退出码1.

for循环里,只要某次grep -q没匹配,最后一个退出码可能是1

但stdout里早就已经输出了正确的enx62fde47ffdbb\n

这在shell里是完全成功的,但在executeChild()里被当成“失败”

还把stderr 合并进来了,雪上加霜:

cmd += " 2>&1";

stderr 的提示,shell 的调试信息,都被拼进res

然后又在失败分支里继续拼:

res += " normal termination, exit_status = X";

res被污染
更不可能“等于期望的接口名”

修改:

static bool executeChild(const string &cmd, string &res) { FILE *fp = popen(cmd.c_str(),"r"); if (!fp) { res += string("popen failed: ") + strerror(errno); return false; } char buffer[512]; while (fgets(buffer, sizeof(buffer), fp)) { res += buffer; } int status = pclose(fp); // 仅用于日志 if (status != 0) { if (WIFEXITED(status)) { hlogi("cmd exit_status=%d", WEXITSTATUS(status)); } } // ✅ 不用 exit code 决定成败 return !res.empty(); }

问题

按照错误码本身没什么问题呀?

for 循环里最后一次命令的退出码 ≠ 你想表达的“整体成功/失败”

  • for 循环 + grep -q → 非常不适合用 exit code 判断成功

  • cmd 是“语义正确的”,但“退出码语义不可靠”

这个是经典的shell坑,shell的语义本身就不是想象中的“整体成功或失败”

我的指令:

for i in /sys/class/net/*; do drv=$(readlink $i/device/driver 2>/dev/null) echo $drv | grep -Eq 'cdc_ncm|cdc_ether|rndis_host' && basename $i done

shell 的退出码规则只有一句话

整个 for 循环的退出码 = 最后一次执行的那条“简单命令”的退出码

不是:

  • 有没有输出

  • 有没有“某一次成功”

  • 有没有basename打印

而是:

最后一个grep -Eq的返回值

系统网卡顺序是:

lo enP5p1s0f0 enP5p1s0f1 ... enx62fde47ffdbb ← 这是 cdc_ncm usb0 usb1 ← 最后一个

执行过程是:

1️⃣ 前面一堆接口
grep -Eq不匹配
→ exit code = 1

2️⃣ 执行到enx62fde47ffdbb
grep -Eq匹配成功
basename被执行
stdout 正确输出enx62fde47ffdbb

3️⃣ 接着继续循环(for 不会停)

4️⃣ 最后一个接口(比如usb1
grep -Eq不匹配
→ exit code = 1

最终结果(致命点)

  • stdout:有我想要的内容

    enx62fde47ffdbb

  • shell 最终退出码:1

这在shell语义里是完全合理的,但是被executeChild当成失败

所以:

不要用 exit code 判命令

C++里已经有:

res += buffer;

正确的工程判断是:

判断项适合吗
popen 是否成功
stdout 是否非空
shell exit code❌(在这个场景)

修改后崩溃!

由于把通用执行器改了,影响了全局:

「exit code == 0」改成了「stdout 非空」

命令实际上成功了,但被当成失败,后续流程被中断。

程序崩溃没有ip,所以只能改回原来的通用执行器,修改UsbnameGet(),使其忽略返回值或者用popen代替。如果没有匹配到,也是返回空。

std::string UsbnameGet() { std::string res; const char* cmd = "for i in /sys/class/net/*; do " "drv=$(readlink $i/device/driver 2>/dev/null); " "echo $drv | grep -Eq 'cdc_ncm|cdc_ether|rndis_host' && basename $i; " "done"; FILE* fp = popen(cmd, "r"); if (!fp) { perror("popen failed"); return ""; } char buf[256]; while (fgets(buf, sizeof(buf), fp)) { res += buf; } int rc = pclose(fp); std::cout << "pclose rc=" << rc << std::endl; std::cout << "RAW res=[" << res << "] len=" << res.size() << std::endl; return res; }
http://www.cnnetsun.cn/news/67310.html

相关文章:

  • 【李沐 | 动手实现深度学习】9-1 Pytorch神经网络基础
  • Miniconda安装后无法使用conda命令?原因与解决方法
  • LobeChat插件系统详解:如何扩展AI助手的无限可能?
  • 【中国科学报】深圳先进院揭示低剂量尼古丁延缓衰老机制
  • NIFA:基于噪声强度场感知网络的低剂量CT成像|文献速递-文献分享
  • 视频成品牌“通用语言”,集之互动推出AI创意视频服务助力营销内容升级
  • 从海报时代迈向短片时代,集之互动用AI品牌短片服务帮品牌讲更多“被看到的故事”
  • 全球视频广告支出突破1900亿美元,集之互动以AI广告大片服务瞄准“高可控”的品牌出片标准
  • LobeChat能否对接Asana任务管理?项目协作智能化
  • 重构开发链路:低代码如何成为企业数智化转型的关键抓手
  • 使用PyTorch训练微调Qwen3-14B的入门级教程
  • 从代码看BuildingAI:企业级智能体平台设计解析
  • 负责处理大数据量的Excel导出功能
  • JMeter---正则表达式提取器
  • 如何利用diskinfo下载官网资源优化Qwen3-VL-8B存储性能
  • 量子电导式氢气浓度检测仪在制氢系统中的优势
  • 牛了个牛,做好功能测试就靠“它”
  • AutoGPT任务执行风险预警系统设计理念
  • 树形结构遍历与递归应用解析
  • 雷科电力-REKE2195电缆路径及定位仪
  • 轻量级部署方案:LobeChat在树莓派上的可行性实验
  • 口碑是营销出来的?格行真实用户实测:网速和售后真有那么好? “流量靠猜”“网速成迷”3 大场景实测给答案
  • AI搜索排名GEO优化服务商行业排行榜
  • AutoGPT支持Apple Silicon芯片加速了吗?M系列Mac实测
  • LWGANet:两大核心模块:TGFI(减空间冗余)和 LWGA(减通道冗余。
  • 如何用AI大数据在1秒内构建完整客户画像,获取高质量线索的源码系统
  • 好写作AI:专治学术“写作困难户”,让你告别深夜emo和DDL恐惧!
  • 好写作AI:论文格式“救星”,一键告别“调参”噩梦
  • halcon3d 求角平分面
  • 家校沟通不用“猜”,小二查成绩让每分进步都清晰可见