linux获取网卡的信息,c++

使用c++代码获取:
网卡的字典消息:型号、传输速度、接口类型(RJ-45/FDDI/ATM等)、ip(多个)、子网掩码、网关、mac地址
网卡的性能指标:上行kb/s、下行kb/s、上行package/s、下行package/s、丢包/s、丢包率、错包/s、错包率

这个算是比较详细的方法了,希望能帮到你,解决问题望采纳。:
在Linux上,可以使用C++代码来获取网卡的信息。以下是一个示例代码,使用Linux系统调用和网络工具来获取网卡的信息:

#include <iostream>
#include <fstream>
#include <sstream>

// 获取网卡型号信息
std::string getNicModel(const std::string& interfaceName) {
    std::ifstream file("/sys/class/net/" + interfaceName + "/device/model");
    std::string model;
    std::getline(file, model);
    return model;
}

// 获取网卡传输速度信息
std::string getNicSpeed(const std::string& interfaceName) {
    std::ifstream file("/sys/class/net/" + interfaceName + "/speed");
    std::string speed;
    std::getline(file, speed);
    return speed + " Mbps";
}

// 获取网卡接口类型信息
std::string getNicInterfaceType(const std::string& interfaceName) {
    std::ifstream file("/sys/class/net/" + interfaceName + "/type");
    std::string type;
    std::getline(file, type);
    if (type == "1") {
        return "Ethernet";
    } else if (type == "32") {
        return "Wireless";
    } else {
        return "Unknown";
    }
}

// 获取网卡IP地址信息
std::string getNicIP(const std::string& interfaceName) {
    std::ifstream file("/sbin/ifconfig " + interfaceName + " | grep 'inet '");
    std::string line;
    std::getline(file, line);
    std::istringstream iss(line);
    std::string word;
    std::getline(iss, word, ' ');
    std::getline(iss, word, ' ');
    return word;
}

// 获取网卡子网掩码信息
std::string getNicSubnetMask(const std::string& interfaceName) {
    std::ifstream file("/sbin/ifconfig " + interfaceName + " | grep 'netmask '");
    std::string line;
    std::getline(file, line);
    std::istringstream iss(line);
    std::string word;
    std::getline(iss, word, ' ');
    std::getline(iss, word, ' ');
    return word;
}

// 获取网卡网关信息
std::string getNicGateway(const std::string& interfaceName) {
    std::ifstream file("/sbin/ip route show | grep 'default via' | grep " + interfaceName + " | awk '{print $3}'");
    std::string gateway;
    std::getline(file, gateway);
    return gateway;
}

// 获取网卡MAC地址信息
std::string getNicMAC(const std::string& interfaceName) {
    std::ifstream file("/sys/class/net/" + interfaceName + "/address");
    std::string mac;
    std::getline(file, mac);
    return mac;
}

int main() {
    std::string interfaceName = "eth0";  // 请根据实际网卡名称进行修改

    std::cout << "Interface Name: " << interfaceName << std::endl;
    std::cout << "Interface Model: " << getNicModel(interfaceName) << std::endl;
    std::cout << "Interface Speed: " << getNicSpeed(interfaceName) << std::endl;
    std::cout << "Interface Type: " << getNicInterfaceType(interfaceName) << std::endl;
    std::cout << "IP Address: " << getNicIP(interfaceName) << std::endl;
    std::cout << "Subnet Mask: " << getNicSubnetMask(interfaceName) << std::endl;
    std::cout << "Gateway: " << getNicGateway(interfaceName) << std::endl;
    std::cout << "MAC Address: " << getNicMAC(interfaceName) << std::endl;

    return 0;
}


请根据您的实际网卡名称将interfaceName变量设置为您要查询的网卡名称。代码将打印出网卡的型号、传输速度、接口类型、IP地址、子网掩码、网关和MAC地址等信息。

请注意,在Linux系统上,ifconfig和ip命令可能需要使用root权限才能执行。此外,您需要根据您的操作系统和开发环境设置适当的编译选项和库依赖关系。

看看是不是你要的结果,如有帮助,麻烦点个采纳

#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

// 获取网卡的字典消息:型号、传输速度、接口类型、IP、子网掩码、网关地址、MAC地址
void getNetworkInterfaceInfo(const char* interfaceName) {
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    struct ifreq ifr;

    strncpy(ifr.ifr_name, interfaceName, IFNAMSIZ-1);

    // 获取型号
    if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
        std::cout << "MAC地址:" << (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] << std::endl;
    }

    if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) {
        struct sockaddr_in* addr = (struct sockaddr_in*)&(ifr.ifr_addr);
        std::cout << "IP地址:" << inet_ntoa(addr->sin_addr) << std::endl;
    }

    if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) {
        struct sockaddr_in* addr = (struct sockaddr_in*)&(ifr.ifr_netmask);
        std::cout << "子网掩码:" << inet_ntoa(addr->sin_addr) << std::endl;
    }

    if (ioctl(sock, SIOCGIFDSTADDR, &ifr) == 0) {
        struct sockaddr_in* addr = (struct sockaddr_in*)&(ifr.ifr_dstaddr);
        std::cout << "网关地址:" << inet_ntoa(addr->sin_addr) << std::endl;
    }

    // 其他字典消息的获取,例如接口类型、传输速度等,根据具体需求使用 ioctl(sock, ...) 来获取

    close(sock);
}

// 获取网卡的性能指标:上行速度、下行速度、上行包速率、下行包速率、丢包率、错包率等
void getNetworkInterfaceStats(const char* interfaceName) {
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    struct ifreq ifr;

    strncpy(ifr.ifr_name, interfaceName, IFNAMSIZ-1);

    if (ioctl(sock, SIOCGIFINDEX, &ifr) == 0) {
        struct rtnl_link_stats stats;
        memset(&stats, 0, sizeof(stats));

        stats.rtnl_link_stats_version = 1;
        if (ioctl(sock, SIOCETHTOOL, &ifr) >= 0) {
            // 获取网络接口的性能指标
            // 例如 stats.tx_bytes, stats.rx_bytes, stats.tx_packets, stats.rx_packets, stats.tx_dropped, 
            // stats.rx_dropped, stats.tx_errors, stats.rx_errors 等

            std::cout << "上行速度:" << stats.tx_bytes / 1024 << "kb/s" << std::endl;
            std::cout << "下行速度:" << stats.rx_bytes / 1024 << "kb/s" << std::endl;
            // 其他性能指标的打印输出,根据具体需求使用 stats.xx 来获取
        }
    }

    close(sock);
}

int main() {
    const char* interfaceName = "eth0"; // 网卡接口名称

    // 获取网卡的字典消息
    getNetworkInterfaceInfo(interfaceName);

    // 获取网卡的性能指标
    getNetworkInterfaceStats(interfaceName);

    return 0;
}

凑巧了,之前刚有一个人问过类似问题


参考我的回答。

参考:

你问了一个月了,磁盘,网卡,硬件,cpu什么的,你在公司是做啥的呀


#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <linux/if.h>
 
#define BUF_SIZE 1024
using namespace std;
 
static int getIP(string net_name, string &strIP)
{
    int sock_fd;
    struct ifconf conf;
    struct ifreq *ifr;
    char buff[BUF_SIZE] = {0};
    int num;
    int i;
 
    sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
    if ( sock_fd < 0 )     
        return -1;
 
    conf.ifc_len = BUF_SIZE;
    conf.ifc_buf = buff;
 
    if ( ioctl(sock_fd, SIOCGIFCONF, &conf) < 0 )
    {
        close(sock_fd);
        return -1;
    }
 
    num = conf.ifc_len / sizeof(struct ifreq);
    ifr = conf.ifc_req;
 
    for(i = 0; i < num; i++)
    {
        struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);
 
        if ( ioctl(sock_fd, SIOCGIFFLAGS, ifr) < 0 )
        {
                close(sock_fd);
                return -1;
        }
 
        if ( (ifr->ifr_flags & IFF_UP) && strcmp(net_name.c_str(),ifr->ifr_name) == 0 )
        {
                strIP = inet_ntoa(sin->sin_addr);
                close(sock_fd);
 
                return 0;
        }
        
        ifr++;
    }
    
    close(sock_fd);
 
    return -1;
}
 
 
int main()
{
    string strIP;
    getIP("eth1", strIP);
    cout << strIP << endl;
    return 0;
}

要获取Linux网卡的信息,您可以使用Socket编程和ioctl调用来访问网络接口。以下是一个基本示例,可获取有关网卡的字典消息和性能指标的信息:


#include <iostream>  
#include <sys/socket.h>  
#include <net/if.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <sys/ioctl.h>  
#include <linux/ethtool.h>  
#include <linux/sockios.h>  
  
int main() {  
    int sock = socket(AF_INET, SOCK_DGRAM, 0);  
    if (sock < 0) {  
        std::cerr << "Error creating socket\n";  
        return -1;  
    }  
  
    struct ifreq ifr;  
    if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {  
        std::cerr << "Error getting MAC address\n";  
        return -1;  
    }  
  
    char mac_address[6];  
    std::memcpy(mac_address, &ifr.ifr_hwaddr.sa_data, 6);  
    std::printf("MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",  
                mac_address[0], mac_address[1], mac_address[2],  
                mac_address[3], mac_address[4], mac_address[5]);  
  
    struct ethtool_cmd ecmd;  
    if (ioctl(sock, SIOCGETHTOOL, &ecmd) < 0) {  
        std::cerr << "Error getting ethtool data\n";  
        return -1;  
    }  
  
    std::printf("Model: %s\n", ecmd.driver);  
    std::printf("Speed: %u Mbps\n", ecmd.speed);  
    std::printf("Type: %s\n", ecmd.net_type);  
  
    struct ifconf ifc;  
    ifc.ifc_buf = NULL;  
    ifc.ifc_len = 0;  
    if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {  
        std::cerr << "Error getting interface configuration\n";  
        return -1;  
    }  
  
    char ip_address[16];  
    for (int i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++) {  
        if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {  
            std::cerr << "Error getting IP address\n";  
            return -1;  
        }  
        std::printf("IP Address: %s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));  
    }  
  
    close(sock);  
    return 0;  
}

此代码将获取网卡的MAC地址、型号、传输速度和接口类型。要获取性能指标,您可以使用相同的方法,通过SIOCGETHTOOL ioctl命令获取更多ethtool数据。

linux下获取网卡IP的C/C++代码
可以参考下

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <linux/if.h>
 
#define BUF_SIZE 1024
using namespace std;
 
static int getIP(string net_name, string &strIP)
{
    int sock_fd;
    struct ifconf conf;
    struct ifreq *ifr;
    char buff[BUF_SIZE] = {0};
    int num;
    int i;
 
    sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
    if ( sock_fd < 0 )     
        return -1;
 
    conf.ifc_len = BUF_SIZE;
    conf.ifc_buf = buff;
 
    if ( ioctl(sock_fd, SIOCGIFCONF, &conf) < 0 )
    {
        close(sock_fd);
        return -1;
    }
 
    num = conf.ifc_len / sizeof(struct ifreq);
    ifr = conf.ifc_req;
 
    for(i = 0; i < num; i++)
    {
        struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);
 
        if ( ioctl(sock_fd, SIOCGIFFLAGS, ifr) < 0 )
        {
                close(sock_fd);
                return -1;
        }
 
        if ( (ifr->ifr_flags & IFF_UP) && strcmp(net_name.c_str(),ifr->ifr_name) == 0 )
        {
                strIP = inet_ntoa(sin->sin_addr);
                close(sock_fd);
 
                return 0;
        }
        
        ifr++;
    }
    
    close(sock_fd);
 
    return -1;
}
 
 
int main()
{
    string strIP;
    getIP("eth1", strIP);
    cout << strIP << endl;
    return 0;
}