美文网首页
c++获取所有网卡mac和ip

c++获取所有网卡mac和ip

作者: 一路向后 | 来源:发表于2021-09-23 21:02 被阅读0次

1.源码实现

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

int getEthNames(set<string> &ethName)
{
    FILE *fp = NULL;
    char *p = NULL;
    char linebuf[512];
    char devname[128];
    string tmp;

    fp = fopen("/proc/net/dev", "r");
    if(fp == NULL)
    {
        return -1;
    }

    memset(linebuf, 0x00, sizeof(linebuf));
    memset(devname, 0x00, sizeof(devname));

    while(fgets(linebuf, 511, fp) != NULL)
    {
        p = strstr(linebuf, ":");
        if(p == NULL)
        {
            memset(linebuf, 0x00, sizeof(linebuf));
            continue;
        }

        p[0] = 0x00;

        memset(devname, 0x00, sizeof(devname));

        strncpy(devname, linebuf, 127);

        tmp = string(devname);
        tmp.erase(0, tmp.find_first_not_of(" "));
        tmp.erase(tmp.find_last_not_of(" ")+1);

        if(strncmp(tmp.c_str(), "lo", 2) != 0 )
        {
            if(strncmp(tmp.c_str(), "eth", 3) == 0 || strncmp(tmp.c_str(), "ens", 3) == 0 || \
                strncmp(tmp.c_str(), "enp", 3) == 0 || strncmp(tmp.c_str(), "en", 2) == 0)
            {
                ethName.insert(tmp);
            }
        }

        memset(linebuf, 0x00, sizeof(linebuf));
    }

    fclose(fp);

    return 0;
}

int getIPMACs(set<string> &ethName, map<string, pair<string, string>> &macs)
{
    set<string>::iterator it;
    struct ifreq ifr;
    struct sockaddr_in *sin;
    char ip_addr[30];
    char mac_addr[30];
    int sockfd = -1;
    int nRes = -1;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        return -1;
    }

    for(it=ethName.begin(); it!=ethName.end(); it++)
    {
        nRes = -1;

        memset(ip_addr, 0x00, sizeof(ip_addr));
        memset(mac_addr, 0x00, sizeof(mac_addr));

        memset(&ifr, 0x00, sizeof(ifr));

        strcpy(ifr.ifr_name, (*it).c_str());

        nRes = ioctl(sockfd, SIOCGIFADDR, &ifr);
        if(nRes < 0)
        {
            strcpy(ip_addr, "");
        }
        else
        {
            sin = (struct sockaddr_in *)&ifr.ifr_addr;
            strcpy(ip_addr, inet_ntoa(sin->sin_addr));
        }

        nRes = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
        if(nRes < 0)
        {
            strcpy(mac_addr, "00:00:00:00:00:00");
        }
        else
        {
            sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
                (unsigned char)ifr.ifr_hwaddr.sa_data[0],
                (unsigned char)ifr.ifr_hwaddr.sa_data[1],
                (unsigned char)ifr.ifr_hwaddr.sa_data[2],
                (unsigned char)ifr.ifr_hwaddr.sa_data[3],
                (unsigned char)ifr.ifr_hwaddr.sa_data[4],
                (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
        }

        macs.insert(make_pair(string(mac_addr), make_pair(string(ifr.ifr_name), string(ip_addr))));
    }

    close(sockfd);

    return 0;
}

int main()
{
    set<string> eth;
    map<string, pair<string, string>> macs;
    map<string, pair<string, string>>::iterator it;

    getEthNames(eth);
    getIPMACs(eth, macs);

    //cout << eth.size() << endl;

    for(it=macs.begin(); it!=macs.end(); it++)
    //it=macs.begin();
    {
        cout << (*it).first << endl;
        cout << (*it).second.first << endl;
        cout << (*it).second.second << endl;
    }

    return 0;
}

2.编译源码

$ g++ -o test test.cpp -std=c++11

3.运行及其结果

$ ./test
00:0c:29:c7:f7:30
ens33
192.168.137.122

相关文章

网友评论

      本文标题:c++获取所有网卡mac和ip

      本文链接:https://www.haomeiwen.com/subject/bwoogltx.html