加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Linux获取IP,子网掩码,网关等

(2019-10-17 17:24:33)
#include【stdio.h】  // 请自行改为尖括号,这里尖括号不能显示
#include 【cstdlib】
#include【string.h】
#include【unistd.h】
#include 【fstream】
#include【iostream】
#include 【net/if.h】
#include 【sys/ioctl.h】
#include 【arpa/inet.h】
#include 【errno.h】
#include 【linux/rtnetlink.h】
#include 【stdlib.h】
#include 【netinet/in.h】
#include 【arpa/inet.h】
#include 【sys/utsname.h】
#include 【limits.h】
#include 【ctype.h】
#include 【linux/sockios.h】
#include 【stdio.h】
 #include 【string.h】
 #include 【net/if.h】
 #include 【sys/ioctl.h】
 #include 【arpa/inet.h】
 #include 【errno.h】

 int getInterfaceInfo(void)
 {
     int fd;
     int interfaceNum = 0;
     struct ifreq buf[16];
     struct ifconf ifc;
     struct ifreq ifrcopy;
     char mac[16] = {0};
     char ip[32] = {0};
     char broadAddr[32] = {0};
     char subnetMask[32] = {0};

     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) 【 0)
     {
         perror("socket");

         close(fd);
         return -1;
     }

     ifc.ifc_len = sizeof(buf);
     ifc.ifc_buf = (caddr_t)buf;
     if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))//获取所有网口信息
     {
         interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
         printf("interface num = %dn", interfaceNum);
         while (interfaceNum-- 】 0) //逐个提取
         {
             printf("ndevice name: %sn", buf[interfaceNum].ifr_name);

             //ignore the interface that not up or not runing
             ifrcopy = buf[interfaceNum];
             if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy))//设置网口标记
             {
                 printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
                 close(fd);
                 return -1;
             }

             //get the mac of this interface
             if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
             {
                 memset(mac, 0, sizeof(mac));
                 snprintf(mac, sizeof(mac), "xxxxxx",
                     (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[0],
                     (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[1],
                     (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[2],
                     (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[3],
                     (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[4],
                    (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[5]);
                 printf("device mac: %sn", mac);
             }
             else
             {
                 printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
                 close(fd);
                 return -1;
             }

             //get the IP of this interface
             if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
             {
                 snprintf(ip, sizeof(ip), "%s",(char *)inet_ntoa(   ((struct sockaddr_in * )&(buf[interfaceNum].ifr_addr)  )-】sin_addr)     );
                 printf("device ip: %sn", ip);
             }
             else
             {
                 printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
                 close(fd);
                 return -1;
             }

             //get the broad address of this interface
             if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum]))
             {
                 snprintf(broadAddr, sizeof(broadAddr), "%s",
                     (char *)inet_ntoa(((struct sockaddr_in *)&
                       (buf[interfaceNum].ifr_broadaddr))-】sin_addr));
                 printf("device broadAddr: %sn", broadAddr);
             }
             else
             {
                 printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
                 close(fd);
                 return -1;
             }

            //get the subnet mask of this interface
            if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum]))
            {
                snprintf(subnetMask, sizeof(subnetMask), "%s", \
                    (char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))-】sin_addr));
                printf("device subnetMask: %sn", subnetMask);
            }
            else
            {
                printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
                close(fd);
                return -1;
            }
        }
    }

    else
    {
        printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
        close(fd);
        return -1;
    }

    close(fd);
    return 0;
}

int main(void)
{
    getInterfaceInfo();
    return 0;
}


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有