Linux下如何用C语言获得网络信息,比如IP,掩码,mac地址,网关

来源:互联网 发布:自然语言编程 编辑:程序博客网 时间:2024/05/19 20:18
 在Linux下执行以下程序:
#include <stdio.h>#include <stdlib.h>int main( ){    FILE* netinfo = popen("/sbin/ifconfig", "r");           if(!netinfo){        puts("error while open pipe");        exit(1);    }      char str[200];           while( fgets(str, 199, netinfo) != NULL ){         printf("%s", str);    }             pclose(netinfo);}

[root@localhost ~]# ./test eth0      Link encap:Ethernet  HWaddr 00:0C:29:44:FB:6D            inet addr:192.168.232.128  Bcast:192.168.232.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe44:fb6d/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:24 errors:0 dropped:0 overruns:0 frame:0          TX packets:44 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:7666 (7.4 KiB)  TX bytes:6087 (5.9 KiB)          Interrupt:18 Base address:0x2024 lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:1706 errors:0 dropped:0 overruns:0 frame:0          TX packets:1706 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:2640180 (2.5 MiB)  TX bytes:2640180 (2.5 MiB)

原创粉丝点击