#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