[問題] 如何在程式中分析 ifconfig

看板C_and_CPP作者 (三腳貓的把戲)時間10年前 (2014/07/15 16:53), 編輯推噓4(409)
留言13則, 10人參與, 最新討論串1/1
開發平台(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) -- I want you to join my army. === http://0rz.tw/V9OIa === -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.7.179 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1405414414.A.74E.html

07/15 16:55, , 1F
為何不用shell script?
07/15 16:55, 1F

07/15 16:56, , 2F
唔... 不是很確定這是什麼(google ing... 謝謝大大!!!
07/15 16:56, 2F

07/15 17:10, , 3F
其實你可以 ifconfig ppp0 這樣輸出會少很多
07/15 17:10, 3F

07/15 17:18, , 4F
stackoverflow.com/questions/4951257
07/15 17:18, 4F

07/15 17:25, , 5F
哦哦哦!!! 謝謝兩位大大!!! 非常有用的資訊!!!
07/15 17:25, 5F

07/15 19:25, , 6F
shell 表示哭哭
07/15 19:25, 6F

07/15 19:55, , 7F
還在用ifconfig?ip*哭了
07/15 19:55, 7F

07/15 20:32, , 8F
為何不用netlink?
07/15 20:32, 8F

07/15 20:46, , 9F
Try popen
07/15 20:46, 9F

07/15 23:06, , 10F
netlink有沒有3.x之後的教學啊@@ 我都看到2.6而已
07/15 23:06, 10F

07/16 06:22, , 11F
ping 會更好
07/16 06:22, 11F

07/16 14:18, , 12F
收到好多關鍵字啊... 謝謝大大們... <(_ _)> google ing
07/16 14:18, 12F

07/17 15:08, , 13F
請用sed
07/17 15:08, 13F
文章代碼(AID): #1JnEmETE (C_and_CPP)