LINUX下擷取IP地址和MAC地址__LINUX

來源:互聯網
上載者:User

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h> #include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h> #ifdef SOLARIS
#include <sys/sockio.h>
#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;
} 編譯:gcc 檔案名稱即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.