The source code is as follows:
#include <sys/socket.h>#include <sys/ioctl.h>#include <linux/if.h>#include <string.h>#include <stdio.h>Char*net_detect(char* net_name) {intSKFD =0; struct Ifreq IFR; SKFD =Socket(Af_inet, Sock_dgram,0);if(SKFD <0) {printf("%s:%d Open socket error!\n", __file__, __line__);returnNULL; } strcpy (Ifr.ifr_name, net_name);if(IOCTL (SKFD, Siocgifflags, &IFR) <0) {printf("%s:%d IOCTL error!\n", __file__, __line__);printf("Maybe Ethernet inferface %s is not valid!", ifr.ifr_name);Close(SKFD);returnNULL; }if(Ifr.ifr_flags & Iff_running) {return "Up"; }Else{return "Down"; }}intMain () {printf('%s\ n ', Net_detect ("Eth0"));return 0;}
Summarize:
The program is a useful RUNNING to test the ifconfig command in the specified NIC. Can be combined with ifconfig eth0 up and ifconfig eth0 down tests.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux C gets the NIC status (up or down)