Linux c gets the NIC status (UP or DOWN), linuxup
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){ int skfd = 0; struct ifreq ifr; skfd = socket(AF_INET, SOCK_DGRAM, 0); if(skfd < 0) { printf("%s:%d Open socket error!\n", __FILE__, __LINE__); return NULL; } 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); return NULL; } if(ifr.ifr_flags & IFF_RUNNING) { return "UP"; } else { return "DOWN"; }}int main(){ printf("%s\n",net_detect("eth0")); return 0;}
Summary:
This program is used to test the ifconfig command and specify that the NIC is RUNNING. You can use ifconfig eth0 up and ifconfig eth0 down for testing.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.