Linux 获取本机IP、MAC地址用法大全

 //#include <sys/types.h>
#include <ifaddrs.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <arpa/inet.h>
void pHx(unsigned char* p,int len){
printf("Hex: ");
for(int i=;i<len;i++){
printf("%02X:",p[i]);
}
printf("\b\n");
}
char* getMac(char* mac,char* dv){
struct ifreq ifreq;
int sock;
if(!mac || !dv)
return mac;
if((sock=socket(AF_INET,SOCK_STREAM,)) <)
{
perror( "socket ");
return mac;
}
strcpy(ifreq.ifr_name,dv);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq) <)
{
perror( "ioctl ");
return mac;
}
pHx((unsigned char*)ifreq.ifr_hwaddr.sa_data,sizeof(ifreq.ifr_hwaddr.sa_data));
sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", //以太网MAC地址的长度是48位
(unsigned char)ifreq.ifr_hwaddr.sa_data[],
(unsigned char)ifreq.ifr_hwaddr.sa_data[],
(unsigned char)ifreq.ifr_hwaddr.sa_data[],
(unsigned char)ifreq.ifr_hwaddr.sa_data[],
(unsigned char)ifreq.ifr_hwaddr.sa_data[],
(unsigned char)ifreq.ifr_hwaddr.sa_data[]);
return mac;
} int main (int argc, const char * argv[])
{
char hname[];
struct hostent *hent;
int i; gethostname(hname, sizeof(hname)); //hent = gethostent();
hent = gethostbyname(hname); printf("hostname: %s\n", hent->h_name);
//----------------------------------------------------------
printf("\n\n"); char mac[];
struct ifaddrs * ifap0=NULL,*ifap=NULL;
void * tmpAddrPtr=NULL; getifaddrs(&ifap0);
ifap=ifap0;
while (ifap!=NULL) {
if (ifap->ifa_addr->sa_family==AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifap->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
if(strcmp(addressBuffer,"127.0.0.1")!=){
printf("%s IPv4: %s\n", ifap->ifa_name, addressBuffer);
printf("MAC: %s\n\n",getMac(mac,ifap->ifa_name));
}
} else if (ifap->ifa_addr->sa_family==AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in *)ifap->ifa_addr)->sin_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
if(strcmp(addressBuffer,"::")!=){
printf("%s IPv6: %s\n", ifap->ifa_name, addressBuffer);
printf("MAC: %s\n\n",getMac(mac,ifap->ifa_name));
}
}
ifap=ifap->ifa_next;
}
if (ifap0) { freeifaddrs(ifap0); ifap0 = NULL; }
return ;
}

输出:

hostname: localhost

eth0 IPv4: 192.168.42.2
MAC: 02:42:c0:a8:2a:02 eth0 IPv6: 0:0:fe80::42:c0ff
MAC: 02:42:c0:a8:2a:02

(更多参考:Linux 获取本机IP、MAC地址用法大全)

  

windows下,详情见:http://www.cnblogs.com/lzpong/p/6137652.html

05-11 11:02