linux ip mac,LINUX下获取IP地址和MAC地址

#include

#include

#include

#include

#include

#include

#include

#include

#ifdef SOLARIS

#include

#endif

#define MAXINTERFACES 16

main (argc, argv)

register int argc;

register char *argv[];

{

register int fd, intrface, retn = 0;

struct ifreq buf[MAXINTERFACES];

struct arpreq arp;

struct ifconf ifc;

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);

//根据借口信息循环获取设备IP和MAC地址

while (intrface-- > 0)

{

//获取设备名称

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

//判断网卡类型

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);

}

//判断网卡状态

if (buf[intrface].ifr_flags & IFF_UP)

{

puts("the interface status is UP");

}

else

{

puts("the interface status is DOWN");

}

//获取当前网卡的IP地址

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);

}

/* this section can't get Hardware Address,I don't know whether the reason is module driver*/

#ifdef SOLARIS

//获取MAC地址

arp.arp_pa.sa_family = AF_INET;

arp.arp_ha.sa_family = AF_INET;

((struct

sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct

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

if (!(ioctl (fd, SIOCGARP, (char *) &arp)))

{

puts ("HW address is:");

//以十六进制显示MAC地址

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

(unsigned char)arp.arp_ha.sa_data[0],

(unsigned char)arp.arp_ha.sa_data[1],

(unsigned char)arp.arp_ha.sa_data[2],

(unsigned char)arp.arp_ha.sa_data[3],

(unsigned char)arp.arp_ha.sa_data[4],

(unsigned char)arp.arp_ha.sa_data[5]);

puts("");

puts("");

}

#else

#if 0

/*Get HW ADDRESS of the net card */

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

{

puts ("HW address is:");

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

(unsigned char)buf[intrface].ifr_enaddr[0],

(unsigned char)buf[intrface].ifr_enaddr[1],

(unsigned char)buf[intrface].ifr_enaddr[2],

(unsigned char)buf[intrface].ifr_enaddr[3],

(unsigned char)buf[intrface].ifr_enaddr[4],

(unsigned char)buf[intrface].ifr_enaddr[5]);

puts("");

puts("");

}

#endif

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("");

}

#endif

else

{

char str[256];

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

perror (str);

}

} //while

} else

perror ("cpm: ioctl");

} else

perror ("cpm: socket");

close (fd);

return retn;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值