欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

linux如何得到本机地址,Linux下如何得到本机所有IP地址及MAC地址

发布时间:2025/3/15 linux 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux如何得到本机地址,Linux下如何得到本机所有IP地址及MAC地址 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

已知:

gethost and gethostbyname,测试只能得到一个IP地址。

---------------------------------------------------------------

原文:http://www.chinaunix.net/jh/23/161236.html

---------------------------------------------------------------

整理后的代码(在fc6下经过本人编译测试通过):

/*

cc getmac.c -o getmac

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include //for close()

#define MAXINTERFACES      16

int main(int argc, char** argv)

{

int fd, intrface, retn = 0;

struct ifreq buf[MAXINTERFACES];

struct arpreq arp;

struct ifconf ifc;

/*

if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[intrface])))

*/

if((fd = socket(AF_INET,SOCK_DGRAM,0)) >= 0)

{

ifc.ifc_len = sizeof(buf);

ifc.ifc_buf = (caddr_t) buf;

if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc))

{

intrface = ifc.ifc_len / sizeof(struct ifreq);

printf("interface num is intrface=%d\n\n\n", intrface);

while (intrface-- > 0)

{

printf("====================interface:%d====================\n",intrface);

printf("net device %s\n", buf[intrface].ifr_name);

/*Jugde whether the net card status is promisc    */

if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[intrface])))

{

if (buf[intrface].ifr_flags & IFF_PROMISC)

{

puts("the interface is PROMISC");

retn++;

}

}

else

{

char str[256];

sprintf(str, "cpm: ioctl device %s",

buf[intrface].ifr_name);

perror(str);

}

/*Jugde whether the net card status is up                  */

if (buf[intrface].ifr_flags & IFF_UP)

{

puts("the interface status is UP");

}

else

{

puts("the interface status is DOWN");

}

/*Get IP of the net card */

if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[intrface])))

{

puts("IP address is:");

puts(inet_ntoa(((struct sockaddr_in *)

(&buf[intrface].ifr_addr))->sin_addr));

puts("");

//puts (buf[intrface].ifr_addr.sa_data);

}

else

{

char str[256];

sprintf(str, "cpm: ioctl device %s",

buf[intrface].ifr_name);

perror(str);

}

/*Get HW ADDRESS of the net card */

if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[intrface])))

{

puts("HW address is:");

printf("%02x:%02x:%02x:%02x:%02x:%02x\n",

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[0],

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[1],

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[2],

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[3],

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[4],

(unsigned char) buf[intrface].ifr_hwaddr.sa_data[5]);

puts("");

puts("");

}

else

{

char str[256];

sprintf(str, "cpm: ioctl device %s", buf[intrface].ifr_name);

perror(str);

}

}

}

else

perror("cpm: ioctl");

}

else

perror("cpm: socket");

close(fd);

return retn;

}

另外一个整理好的:

/*

cc getmac.c -o getmac1

*/

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

#include ;

struct in_addr myself,mymask;

int fd_arp;

struct ifreq ifr;

main(int argc,char* argv[]) {

char device[32];

struct sockaddr from,to;

int fromlen;

struct sockaddr_in *sin_ptr;

u_char *ptr;

int n;

strcpy(device,"lo");

if((fd_arp=socket(AF_INET,SOCK_PACKET,htons(0x0806)))<0) {

perror("arp socket error");

exit(-1);

}

strcpy(ifr.ifr_name,device);

if(ioctl(fd_arp,SIOCGIFADDR,&ifr)<0) {

perror("ioctl SIOCGIFADDR error");

exit(-1);

}

sin_ptr=(struct sockaddr_in *)&ifr.ifr_addr;

myself=sin_ptr->sin_addr;

// get network mask

if(ioctl(fd_arp,SIOCGIFNETMASK,&ifr)<0) {

perror("ioctl SIOCGIFNETMASK error");

exit(-1);

}

sin_ptr=(struct sockaddr_in *)&ifr.ifr_addr;

mymask=sin_ptr->sin_addr;

//if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[intrface])))

// get mac address

if(ioctl(fd_arp,SIOCGIFHWADDR,&ifr)<0) {

perror("ioctl SIOCGIFHWADDR error");

exit(-1);

}

ptr=(u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];

printf("\nrequest mac %02x:%02x:%02x:%02x:%02x:%02x,",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));

printf("\nrequest netmask %s",inet_ntoa(mymask));

printf("\nrequest IP %s\n",inet_ntoa(myself));

}

总结

以上是生活随笔为你收集整理的linux如何得到本机地址,Linux下如何得到本机所有IP地址及MAC地址的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。