Linux get NIC Information

Source: Internet
Author: User
Tags socket error

#include <sys/ioctl.h> #include <net/if.h> #include <unistd.h> #include <netinet/in.h>#    Include <string.h> int main () {struct ifreq IFR;    struct ifconf ifc;    Char buf[2048];     int success = 0;    int sock = socket (af_inet, SOCK_DGRAM, IPPROTO_IP);        if (sock = =-1) {printf ("Socket error\n");    return-1;    } Ifc.ifc_len = sizeof (BUF);    Ifc.ifc_buf = BUF;        if (IOCTL (sock, siocgifconf, &ifc) = =-1) {printf ("ioctl error\n");    return-1;    } struct ifreq* it = ifc.ifc_req;    const struct ifreq* Const END = it + (ifc.ifc_len/sizeof (struct ifreq));    Char szmac[64];    int count = 0;        for (; it! = end; ++it) {strcpy (ifr.ifr_name, it->ifr_name); if (IOCTL (sock, siocgifflags, &ifr) = = 0) {if (! (Ifr.ifr_flags & Iff_loopback))                    {//don ' t count Loopback if (IOCTL (sock, siocgifhwaddr, &ifr) = = 0) {count + +; Unsigned char * PTR;                    ptr = (unsigned char *) &ifr.ifr_ifru.ifru_hwaddr.sa_data[0];                    snprintf (szmac,64, "%02x:%02x:%02x:%02x:%02x:%02x", *ptr,* (ptr+1), * (ptr+2), * (ptr+3), * (PTR+4), * (ptr+5));                printf ("%d,interface name:%s, MAC address:%s \ n", Count,ifr.ifr_name,szmac);            }}}else{printf ("Get Mac info error\n");        return-1; }    }}

Linux get NIC Information

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.