Linux c gets the NIC status (UP or DOWN), linuxup

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.