[问题] 如何在程式中分析 ifconfig

楼主: archon (内湖流川枫)   2014-07-15 16:53:30
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
 Linux
问题(Question):
 在 console 下 ifconfig 指令时,会出现几个 interface,
 我目前工作的环境下,基本会有 eth0, lo, ppp0 三个,
 但有时候因为某些因素,会造成 ppp0 消失(断线?)
 我希望能侦测到 ppp0 死掉的状况,这样才能重新拨接。
 目前是用比较笨的方式,把结果输出到档案
 system("ifconfig > xxx.txt");
 然后再针对档案的文字内容来做分析...
 呣...,想请问各位大大,我应该用什么方式来直接获取资料细节呢?
 ps:其实我只对 ppp0 还有它的 inet addr 有兴趣...
补充说明(Supplement):
eth0 Link encap:Ethernet HWaddr 00:0C:0C:A0:FF:FA
inet addr:10.2.1.9 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST ALLMULTI MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:40
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
ppp0 Link encap:Point-to-Point Protocol
inet addr:100.74.23.44 P-t-P:100.74.23.44 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:68 (68.0 B) TX bytes:135 (135.0 B)
作者: tjjh89017 (伊达政宗)   2014-07-15 16:55:00
为何不用shell script?
楼主: archon (内湖流川枫)   2014-07-15 16:56:00
唔... 不是很确定这是什么(google ing... 谢谢大大!!!
作者: uranusjr (←這人是超級笨蛋)   2014-07-15 17:10:00
其实你可以 ifconfig ppp0 这样输出会少很多
作者: scwg ( )   2014-07-15 17:18:00
stackoverflow.com/questions/4951257
楼主: archon (内湖流川枫)   2014-07-15 17:25:00
哦哦哦!!! 谢谢两位大大!!! 非常有用的资讯!!!
作者: lc85301 (pomelocandy)   2014-07-15 19:25:00
shell 表示哭哭
作者: kerwinhui (kezza)   2014-07-15 19:55:00
还在用ifconfig?ip*哭了
作者: redmilk   2014-07-15 20:32:00
为何不用netlink?
作者: mihs3124 (如钻石般璀灿)   2014-07-15 20:46:00
Try popen
作者: tjjh89017 (伊达政宗)   2014-07-15 23:06:00
netlink有没有3.x之后的教学啊@@ 我都看到2.6而已
作者: johnpage (johnpage)   2014-07-16 06:22:00
ping 会更好
楼主: archon (内湖流川枫)   2014-07-16 14:18:00
收到好多关键字啊... 谢谢大大们... <(_ _)> google ing
作者: loveflames (咕啾咕啾魔法阵)   2014-07-17 15:08:00
请用sed

Links booklink

Contact Us: admin [ a t ] ucptt.com