温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux 获取本机MAC/IP地址的方法

发布时间:2020-06-01 22:38:20 来源:网络 阅读:1177 作者:lsnpurdue 栏目:软件技术

功能:查询本机IP/MAC地址,过滤掉127.0.0.1 loop-back 地址

适用:linux, ubuntu 16.04 调试通过


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>



void getLocalMacIp()

{

    int i;
    int sock_mac;  
    char hname[128];
    struct hostent *hent;
    struct in_addr *paddr = 0;
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifreq ifr_mac;
    void * tmpAddrPtr=NULL;

    struct ifreq buf[16];
    struct ifconf ifc;
    int interface_num;
    char nif_name[128] = {0};
    char addressBuffer[128];

    unsigned char macaddr[6] = {0};
    unsigned char *pFirstByte;
   
    //---------------------------------------------
    //get device IP address, discard loopback
    //---------------------------------------------
    gethostname(hname, sizeof(hname));

    hent = gethostbyname(hname);

    printf("[%s][%d] host: %s\n", __FILE__, __LINE__, hent->h_name);

    getifaddrs(&ifAddrStruct);

    while (ifAddrStruct!=NULL)
    {
        if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
        {
            tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
            pFirstByte = (unsigned char*)tmpAddrPtr;

            if(*pFirstByte != 127)
            {
                paddr = tmpAddrPtr;
                inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 128);
                printf("[%s][%d] NIF %s address %s\n", __FILE__, __LINE__,
                    ifAddrStruct->ifa_name, addressBuffer);
                //---------------------------------------------
                //remember NIF name for future use
                //---------------------------------------------
                strcpy(nif_name, ifAddrStruct->ifa_name);
            }
            
        }
        
        ifAddrStruct=ifAddrStruct->ifa_next;
    }
    

    //---------------------------------------------
    //get device MAC address, discard loopback
    //---------------------------------------------
    sock_mac = socket( AF_INET, SOCK_DGRAM, 0 );

    if(sock_mac > 0)
    {
        ifc.ifc_len = sizeof(buf);
        ifc.ifc_req = buf;
        ioctl(sock_mac, SIOCGIFCONF, (char *)&ifc);

        interface_num = ifc.ifc_len / sizeof(struct ifreq);

        while(interface_num--)
        {
            if(nif_name[0] && strcmp(nif_name, buf[interface_num].ifr_name))
            {
                continue;
            }
            
            ioctl(sock_mac, SIOCGIFHWADDR, (char *)&buf[interface_num]);
            
            memcpy(macaddr, &buf[interface_num].ifr_hwaddr.sa_data[0], 6);
            
            //printf("NIF %s ", buf[interface_num].ifr_name);
            //printf("mac %02x:%02x:%02x:%02x:%02x:%02x \n",
            //    macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
        }        
        
        close( sock_mac );   
    }
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI