메뉴 건너뛰기

enjoyTools.net

System unique id 추출

2016.06.23 00:39

꿈돌이 조회 수:274

Ancient C codes. :-p

 

Windows

/* Get cpu-id */
 
#include <stdio.h>
#include <intrin.h>
 
int main() {
 int b[4];
 
 for (int a = 0; a < 5; a++) {
  __cpuid(b, a);
  printf("The code %i gives %i, %i, %i, %i\r\n", a, b[0], b[1], b[2], b[3]);
 }
 
 return 0;
}

 

/* Get MAC Address */
 
#pragma comment(lib, "Iphlpapi.lib")
 
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
 
int main() {
 IP_ADAPTER_INFO *info = NULL, *pos;
 DWORD size = 0;
 
 GetAdaptersInfo(info, &size);
 
 info = (IP_ADAPTER_INFO *)malloc(size);
 
 GetAdaptersInfo(info, &size);
 
 //for (pos = info; pos != NULL; pos = pos->Next) {
 pos = info;
 if (info != NULL) {
  printf("Mac : ");
  printf("%2.2x", pos->Address[0]);
  for (unsigned int i = 1; i < pos->AddressLength; i++)
   printf(":%2.2x", pos->Address[i]);
  printf("\r\n");
 }
 //}
 
 free(info);
 
 return 0;
}

 

 

Linux

/* Get cpu-id */
 
#include <stdio.h>
 
int main() {
  int a, b;
 
  for (a = 0; a < 5; a++) {
    __asm__("cpuid"
            :"=a"(b)                 // EAX into b (output)
            :"0"(a)                  // a into EAX (input)
            :"%ebx","%ecx","%edx");  // clobbered registers
 
    printf("The code %i gives %i\n", a, b);
  }
 
  return 0;
}

 

/* Get MAC Address */

 

#include <stdio.h>    //printf
#include <string.h>   //strncpy
#include <sys/ioctl.h>
#include <net/if.h>   //ifreq
#include <unistd.h>   //close
 
int main() {
    int fd;
    struct ifreq ifr;
    char *iface = "eth0";
    unsigned char *mac;
    
    fd = socket(AF_INET, SOCK_DGRAM, 0);
 
    ifreq* it = ifc.ifc_req;
    const ifreq* const end = it + (ifc.ifc_len / sizeof(ifreq));
 
    ifr.ifr_addr.sa_family = AF_INET;
 
    //for ( ; it != end ; ++it) {
    strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
    //strncpy(ifr.ifr_name , it->ifr_name , IFNAMSIZ-1);

    ioctl(fd, SIOCGIFHWADDR, &ifr);
    close(fd);

    mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
    
    //display mac address
    printf("Mac : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    //}
 
    return 0;
}

 

출처:

 - https://en.wikipedia.org/wiki/CPUID

 - http://stackoverflow.com/questions/2069855/getting-machines-mac-address-good-solution

 - http://www.binarytides.com/c-program-to-get-mac-address-from-interface-name-on-linux/

 - http://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program

 - http://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program