LINUX 下获取IP地址、MAC地址和广播地址、子网掩码等信息
在linux 下面通过C语言来获取 平台的 ip、mac、boardcast addr 、 network mask等信息;
建立 socket 网络接口,通过ioctl 来获取 网络参数信息。
参考代码如下:
- /*
- Copyright (C) 2015 xubinbin 徐彬彬 (Beijing China)
- filename: get_ip_info.c
- version : v1.1.0
- time : 2015/07/14 10:20
- author : xubinbin ( ternence.hsu@foxmail.com )
- root@ubuntu-virtual-machine:# uname -a
- Linux ubuntu-virtual-machine 3.13.0-34-generic #60-Ubuntu SMP Wed Aug 13 15:49:09 UTC 2014 i686 i686 i686 GNU/Linux
- */
- #include <stdio.h>
- #include <unistd.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>
- #define MAX_INTERFACES (3) // max interfaces
- int main(int argc, char **argv)
- {
- int fd, intrface, ret = 0;
- struct ifreq buf[MAX_INTERFACES]; /* ifreq Array of structures */
- 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))
- {
- //get the number of interfaces information
- intrface = ifc.ifc_len / sizeof (struct ifreq);
- printf("interface num is intrface=%d\n",intrface);
- puts("");
- //get Device IP and MAC address based interface information circular
- while ( (intrface--) > 0)
- {
- //get the device name
- printf ("net device %s\n", buf[intrface].ifr_name);
- //Determine the type of network card
- if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
- {
- if (buf[intrface].ifr_flags & IFF_PROMISC)
- {
- printf("the interface is PROMISC\n");
- ret++;
- }
- }
- else
- {
- char str[256];
- sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
- perror (str);
- }
- //determine NIC status
- if (buf[intrface].ifr_flags & IFF_UP)
- {
- printf("the interface status is up\n");
- }
- else
- {
- printf("the interface status is down\n");
- }
- //get the current NIC IP address
- if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
- {
- printf("IP address is:\t");
- printf("%s\n",(char *)inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
- }
- 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*/
- if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
- {
- printf("HW address is:\t");
- 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]);
- }
- else
- {
- char str[256];
- sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
- perror (str);
- }
- //sub network mask
- if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[intrface])))
- {
- printf("NETWORK MASK :\t%s",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));
- puts("");
- }
- else
- {
- char str[256];
- sprintf(str, "SIOCGIFADDR ioctl %s", buf[intrface].ifr_name);
- perror(str);
- }
- //broadcast address
- if (!(ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[intrface])))
- printf("Broadcast Address:\t%s\n",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));
- puts("");
- }
- }else
- perror ("cpm: ioctl");
- }else
- perror ("cpm: socket");
- close (fd);
- return ret;
- }
复制代码
调试效果如下:
|
|