Chinaunix首页 | 论坛 | 博客
  • 博客访问: 663629
  • 博文数量: 160
  • 博客积分: 2384
  • 博客等级: 大尉
  • 技术积分: 1366
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 11:35
文章分类
文章存档

2015年(45)

2014年(36)

2012年(28)

2011年(37)

2010年(2)

2009年(10)

2008年(2)

分类: LINUX

2011-05-23 15:31:19

  1. #include <stdio.h>

  2. #include <sys/types.h>

  3. #include <sys/param.h>

  4. #include <sys/ioctl.h>

  5. #include <sys/socket.h>

  6. #include <net/if.h>

  7. #include <netinet/in.h>

  8. #include <net/if_arp.h>

  9.  

  10. #define MAXINTERFACES 16

  11.  

  12. int main(int argc, char **argv)

  13. {

  14.     register int fd, interface, retn = 0;

  15.     struct ifreq buf[MAXINTERFACES];

  16.     struct arpreq arp;

  17.     struct ifconf ifc;

  18.     char mac[32] = "";

  19.  

  20.     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {

  21.         ifc.ifc_len = sizeof buf;

  22.         ifc.ifc_buf = (caddr_t) buf;

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

  24.             interface = ifc.ifc_len / sizeof(struct ifreq);

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

  26.             while (interface-- > 0) {

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

  28.  

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

  30.                 if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) {

  31.                     if (buf[interface].ifr_flags & IFF_PROMISC) {

  32.                         printf("the interface is PROMISC");

  33.                         retn ;

  34.                     }

  35.                 } else {

  36.                     char str[256] = "";

  37.  

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

  39.                             buf[interface].ifr_name);

  40.                     perror(str);

  41.                 }

  42.  

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

  44.                 if (buf[interface].ifr_flags & IFF_UP) {

  45.                     printf("the interface status is UP\n");

  46.                 } else {

  47.                     printf("the interface status is DOWN\n");

  48.                 }

  49.  

  50. /*Get IP of the net card */

  51.                 if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {

  52.                     printf("IP address is:");

  53.                     printf("%s\n",

  54.                            inet_ntoa(((struct sockaddr_in

  55.                                        *) (&buf[interface].ifr_addr))->

  56.                                      sin_addr));

  57.                 } else {

  58.                     char str[256] = "";

  59.  

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

  61.                             buf[interface].ifr_name);

  62.                     perror(str);

  63.                 }

  64.  

  65. /*Get HW ADDRESS of the net card */

  66.                 if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {

  67.                     printf("HW address is:");

  68.  

  69.                     sprintf(mac, "%02x%02x%02x%02x%02x%02x",

  70.                             (unsigned char) buf[interface].ifr_hwaddr.

  71.                             sa_data[0],

  72.                             (unsigned char) buf[interface].ifr_hwaddr.

  73.                             sa_data[1],

  74.                             (unsigned char) buf[interface].ifr_hwaddr.

  75.                             sa_data[2],

  76.                             (unsigned char) buf[interface].ifr_hwaddr.

  77.                             sa_data[3],

  78.                             (unsigned char) buf[interface].ifr_hwaddr.

  79.                             sa_data[4],

  80.                             (unsigned char) buf[interface].ifr_hwaddr.

  81.                             sa_data[5]); // sprintf×char *

  82.                     printf("%s\n", mac);

  83.  

  84.                     printf("\n");

  85.                 }

  86.  

  87.                 else {

  88.                     char str[256];

  89.  

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

  91.                             buf[interface].ifr_name);

  92.                     perror(str);

  93.                 }

  94.             } //end of while

  95.         } else

  96.             perror("cpm: ioctl");

  97.  

  98.     } else

  99.         perror("cpm: socket");

  100.  

  101.     close(fd);

  102.     return retn;

  103. }



  104. /********************************end********************************/



  105. /*±à: gcc get_ipmac.c



  106. : ./a.out



  107. á:



  108. interface num is interface=4

  109.  

  110. net device eth2

  111. the interface status is UP

  112. IP address is:192.168.100.48

  113. HW address is:00e04cd803ae

  114.  

  115. net device eth1

  116. the interface status is UP

  117. IP address is:192.168.60.48

  118. HW address is:000aeb5f92ce

  119.  

  120. net device eth0

  121. the interface status is UP

  122. IP address is:192.168.80.48

  123. HW address is:00d0b7b8b22e

  124.  

  125. net device lo

  126. the interface status is UP

  127. IP address is:127.0.0.1

  128. HW address is:000000000000*/
阅读(1003) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~