IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置: 首頁操作系統(tǒng)LINUX → LINUX下獲取IP地址和MAC地址

LINUX下獲取IP地址和MAC地址

時間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評論(1)

  #include

  #include

  #include #include

  #include

  #include

  #include

  #include #ifdef SOLARIS

  #include

  #endif#define MAXINTERFACES?? 16main (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);

  //根據(jù)借口信息循環(huán)獲取設(shè)備IP和MAC地址

  while (intrface-- > 0)

  {

  //獲取設(shè)備名稱

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

  //判斷網(wǎng)卡類型

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

  }??? //判斷網(wǎng)卡狀態(tài)

  if (buf[intrface].ifr_flags & IFF_UP)

  {

  puts("the interface status is UP");

  }

  else

  {

  puts("the interface status is DOWN");

  }??? //獲取當(dāng)前網(wǎng)卡的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 文件名即可

關(guān)鍵詞標(biāo)簽:LINUX

相關(guān)閱讀

文章評論
發(fā)表評論

熱門文章 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法 Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法 多種操作系統(tǒng)NTP客戶端配置 多種操作系統(tǒng)NTP客戶端配置 Linux操作系統(tǒng)修改IP Linux操作系統(tǒng)修改IP

相關(guān)下載

    人氣排行 Linux下獲取CPUID、硬盤序列號與MAC地址 dmidecode命令查看內(nèi)存型號 linux tc實現(xiàn)ip流量限制 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 linux下解壓rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 關(guān)機、重啟、注銷 命令 查看linux服務(wù)器硬盤IO讀寫負載