linux下获取mac地址

来源:互联网 发布:linux sleep cpu 编辑:程序博客网 时间:2024/05/16 08:38

在网上看到的程序,http://linux.die.net/man/7/netdevice

具体实现参考http://www.kegel.com/dkftpbench/dkftpbench-0.45/getifaddrs.c

    char ifr_name[IFNAMSIZ]; /* Interface name */    union {        struct sockaddr ifr_addr;        struct sockaddr ifr_dstaddr;        struct sockaddr ifr_broadaddr;        struct sockaddr ifr_netmask;        struct sockaddr ifr_hwaddr;        short           ifr_flags;        int             ifr_ifindex;        int             ifr_metric;        int             ifr_mtu;        struct ifmap    ifr_map;        char            ifr_slave[IFNAMSIZ];        char            ifr_newname[IFNAMSIZ];        char           *ifr_data;    };};struct ifconf {    int                 ifc_len; /* size of buffer */    union {        char           *ifc_buf; /* buffer address */        struct ifreq   *ifc_req; /* array of structures */    };};
===================================================================================================================================================

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>


int main(void)
{
  char buf[8192] = {0};
  struct ifconf ifc = {0};
  struct ifreq *ifr = NULL;
  int sck = 0;
  int nInterfaces = 0;
  int i = 0;
  char ip[INET6_ADDRSTRLEN] = {0};
  char macp[19];
  struct ifreq *item;
  struct sockaddr *addr;


  /* Get a socket handle. */
  sck = socket(PF_INET, SOCK_DGRAM, 0);
  if(sck < 0)
  {
    perror("socket");
    return 1;
  }


  /* Query available interfaces. */
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
  {
    perror("ioctl(SIOCGIFCONF)");
    return 1;
  }


  /* Iterate through the list of interfaces. */
  ifr = ifc.ifc_req;
  nInterfaces = ifc.ifc_len / sizeof(struct ifreq);


  for(i = 0; i < nInterfaces; i++)
  {
//    item = &ifr[i];
    item = &ifr[i];


    addr = &(item->ifr_addr);


    /* Get the IP address*/
    if(ioctl(sck, SIOCGIFADDR, item) < 0)
    {
      perror("ioctl(OSIOCGIFADDR)");
    }


    if (inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof ip) == NULL) 
        {
           perror("inet_ntop");
           continue;
        }


    /* Get the MAC address */
    if(ioctl(sck, SIOCGIFHWADDR, item) < 0) {
      perror("ioctl(SIOCGIFHWADDR)");
      return 1;
    }


    /* display result */


    sprintf(macp, " %02x:%02x:%02x:%02x:%02x:%02x",
    (unsigned char)item->ifr_hwaddr.sa_data[0],
    (unsigned char)item->ifr_hwaddr.sa_data[1],
    (unsigned char)item->ifr_hwaddr.sa_data[2],
    (unsigned char)item->ifr_hwaddr.sa_data[3],
    (unsigned char)item->ifr_hwaddr.sa_data[4],
    (unsigned char)item->ifr_hwaddr.sa_data[5]);


    printf("%s\t%s\t%s\n", item->ifr_name, ip, macp);


  }


  return 0;
}


===========================================================================================================================================

SIOCGIFHWADDRSIOCSIFHWADDR
Get or set the hardware address of a device using ifr_hwaddr. The hardware address is specified in a struct sockaddrsa_family contains the ARPHRD_* device type,sa_data the L2 hardware address starting from byte 0. Setting the hardware address is a privileged operation.