打印出本机所有网卡的IP地址

  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. #include <arpa/inet.h>
  10. #define MAXINTERFACES 16
  11. int main(int argc, char **argv)
  12. {
  13.     register int fd, interface, retn = 0;
  14.     struct ifreq buf[MAXINTERFACES];
  15.     struct arpreq arp;
  16.     struct ifconf ifc;
  17.     char mac[32] = "";
  18.     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  19.         ifc.ifc_len = sizeof buf;
  20.         ifc.ifc_buf = (caddr_t) buf;
  21.         if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
  22.             interface = ifc.ifc_len / sizeof(struct ifreq);
  23.             printf("interface num is interface=%d/n/n"interface);
  24.             while (interface-- > 0) {
  25.                 printf("net device %s/n", buf[interface].ifr_name);
  26. /*Jugde whether the net card status is promisc */
  27.                 if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) {
  28.                     if (buf[interface].ifr_flags & IFF_PROMISC) {
  29.                         printf("the interface is PROMISC");
  30.                         retn ;
  31.                     }
  32.                 } else {
  33.                     char str[256] = "";
  34.                     sprintf(str, "cpm: ioctl device %s",
  35.                             buf[interface].ifr_name);
  36.                     perror(str);
  37.                 }
  38. /*Jugde whether the net card status is up */
  39.                 if (buf[interface].ifr_flags & IFF_UP) {
  40.                     printf("the interface status is UP/n");
  41.                 } else {
  42.                     printf("the interface status is DOWN/n");
  43.                 }
  44. /*Get IP of the net card */
  45.                 if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
  46.                     printf("IP address is:");
  47.                     printf("%s/n",
  48.                            inet_ntoa(((struct sockaddr_in
  49.                                        *) (&buf[interface].ifr_addr))->
  50.                                      sin_addr));
  51.                 } else {
  52.                     char str[256] = "";
  53.                     sprintf(str, "cpm: ioctl device %s",
  54.                             buf[interface].ifr_name);
  55.                     perror(str);
  56.                 }
  57. /*Get HW ADDRESS of the net card */
  58.                 if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
  59.                     printf("HW address is:");
  60.                     sprintf(mac, "%02x%02x%02x%02x%02x%02x",
  61.                             (unsigned char) buf[interface].ifr_hwaddr.
  62.                             sa_data[0],
  63.                             (unsigned char) buf[interface].ifr_hwaddr.
  64.                             sa_data[1],
  65.                             (unsigned char) buf[interface].ifr_hwaddr.
  66.                             sa_data[2],
  67.                             (unsigned char) buf[interface].ifr_hwaddr.
  68.                             sa_data[3],
  69.                             (unsigned char) buf[interface].ifr_hwaddr.
  70.                             sa_data[4],
  71.                             (unsigned char) buf[interface].ifr_hwaddr.
  72.                             sa_data[5]); // 利用sprintf转换成char *
  73.                     printf("%s/n", mac);
  74.                     printf("/n");
  75.                 }
  76.                 else {
  77.                     char str[256];
  78.                     sprintf(str, "cpm: ioctl device %s",
  79.                             buf[interface].ifr_name);
  80.                     perror(str);
  81.                 }
  82.             }                   //end of while
  83.         } else
  84.             perror("cpm: ioctl");
  85.     } else
  86.         perror("cpm: socket");
  87.     close(fd);
  88.     return retn;
  89. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值