介绍
今天就跟大家聊聊有关使用C语言怎么检测linux网卡,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
# include & lt; stdlib.h> # include & lt; stdio.h> # include & lt; string.h> # include & lt; unistd.h> , , # include & lt; sys/socket.h> # include & lt; sys/ioctl.h> # include & lt; linux/if.h> # include & lt; linux/mii.h> # include & lt; linux/sockios.h> # include & lt; errno.h> , # include & lt; ifaddrs.h> # include & lt; arpa/inet.h>, , # include & lt; linux/ethtool.h> , int cshell_netlink_status (char * if_name) { ,char 缓冲区(BUFSIZ); ,char cmd [100]; ,FILE * read_fp; ,int ,, chars_read; ,int ,, ret =0; , ,memset(,缓冲区,0,,BUFSIZ ); ,memset (cmd,大敌;0,100,); ,sprintf (cmd,“ifconfig -a |, grep % s", if_name);=,,read_fp  popen (cmd,“r"); ,if (, read_fp !=, NULL ) ,{=,chars_read 从文件中读(缓冲区,,sizeof (char), BUFSIZ-1,, read_fp); ,pclose (read_fp); , ,if (chars_read 祝辞,0) ,{ 时间=ret 才能;1; ,} 其他的, ,{ 流才能(stderr,“DEVICE_NONE \ r \ n"); return 才能;0; ,} ,} , ,如果(ret ==, 1) ,{ ,memset(,缓冲区,0,,BUFSIZ ); ,memset (cmd,大敌;0,100,); ,sprintf (cmd,“ifconfig | grep % s", if_name);=,,read_fp  popen (cmd,“r"); ,if (, read_fp !=, NULL ) ,{ chars_read =,才能从文件中读(缓冲区,,sizeof (char), BUFSIZ-1,, read_fp); pclose才能(read_fp); , if 才能;(chars_read 祝辞,0) {才能 ret 才能=,2; ,,} 其他的才能 {才能 流才能(stderr,“DEVICE_DOWN \ r \ n"); return 才能;1; ,,} ,} ,} , ,如果(==ret 2) ,{ ,memset(,缓冲区,0,,BUFSIZ ); ,memset (cmd,大敌;0,100,); ,sprintf (cmd,“ifconfig % s |, grep RUNNING |, awk & # 39; {print 3美元}& # 39;“,if_name);=,,read_fp  popen (cmd,“r"); ,if (, read_fp !=, NULL ) ,{ ,,chars_read =,从文件中读(缓冲区,,sizeof (char), BUFSIZ-1,, read_fp); ,,pclose (read_fp); ,, ,,if (chars_read 祝辞,0) ,,{ 流才能(stderr,“DEVICE_LINKED \ r \ n"); return 才能;3; ,,} 其他的,, ,,{ 流才能(stderr,“DEVICE_UNPLUGGED \ r \ n"); return 才能;2; ,,} ,} ,} , ,return 1; } , , int c_netlink_status (const char * if_name ) { ,int fd =, 1,, ,struct ifreq 仪表; , ,struct ifconf 国际金融公司; ,struct ifreq  ifrs_buf [100];, ,int if_number =0; ,int 我; , , ,if ((fd =,插座(AF_INET, SOCK_DGRAM,, 0)), & lt;, 0) ,{ ,流(stderr,“% s:, socket error [% d], % s \ r \ n", if_name,, errno,, strerror (errno)); ,关闭(fd); ,return 1; ,} ,=,,ifc.ifc_len  sizeof (ifrs_buf);,=,ifc.ifc_buf (caddr_t) ifrs_buf;, ,if (ioctl (fd, SIOCGIFCONF,, (char *), ifc), & lt; 0), ,{ ,流(stderr,“% s:, ioctl SIOCGIFCONF error [% d], % s \ r \ n", if_name,, errno,, strerror (errno)); ,关闭(fd); ,return 1; ,} ,=,,if_number  ifc.ifc_len /, sizeof (struct ifreq); ,(我=0;,i<, if_number;,我+ +) ,{ ,如果(比较字符串(if_name ifrs_buf[我].ifr_name ),==, 0) ,{ 打破才能; ,} ,} , ,如果,我的在=,if_number) ,{ ,关闭(fd); ,流(stderr,“DEVICE_NONE \ r \ n"); ,return 0; ,} , ,bzero(及仪表,sizeof (ifr)); 那么,strncpy (ifr.ifr_name, if_name,, IFNAMSIZ-1),, ,ifr.ifr_name [IFNAMSIZ-1],=, 0;, ,if (ioctl (fd, SIOCGIFFLAGS,, (char *),仪表),& lt; 0), ,{ ,流(stderr,“% s:, ioctl SIOCGIFFLAGS error [% d], % s \ r \ n", if_name,, errno,, strerror (errno)); ,关闭(fd); ,return 1; ,} 1 # if null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null使用C语言怎么检测linux网卡