Advertisement
pneave

Get MAC address

Jan 15th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <stdio.h>    //printf
  2. #include <string.h>   //strncpy
  3. #include <sys/socket.h>
  4. #include <sys/ioctl.h>
  5. #include <net/if.h>   //ifreq
  6. #include <unistd.h>   //close
  7.  
  8. int main()
  9. {
  10.     int fd;
  11.     struct ifreq ifr;
  12.     char *iface = "wlp1s0";
  13.     unsigned char *mac;
  14.      
  15.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  16.  
  17.     ifr.ifr_addr.sa_family = AF_INET;
  18.     strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
  19.  
  20.     ioctl(fd, SIOCGIFHWADDR, &ifr);
  21.  
  22.     close(fd);
  23.      
  24.     mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
  25.      
  26.     //display mac address
  27.     printf("Mac : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement