Qt获取本机网卡信息

//
//  netinfo.h
//

#include 
#include 

static QString interfaceTypeToString(QNetworkInterface::InterfaceType t)
{
    switch (t)
    {
    case QNetworkInterface::Unknown:
        return "Unknown";
    	break;
    case QNetworkInterface::Loopback:
        return "Loopback";
        break;
    case QNetworkInterface::Virtual:
        return "Virtual";
        break;    
    case QNetworkInterface::Ethernet:
        return "Ethernet";
        break;
    case QNetworkInterface::Wifi:
        return "Wifi/Ieee80211";
        break;
    case QNetworkInterface::CanBus:
        return "CanBus";
        break;
    case QNetworkInterface::Fddi:
        return "Fddi";
        break;
    case QNetworkInterface::Ppp:
        return "Ppp";
        break;
    case QNetworkInterface::Slip:
        return "Slip";
        break;
    case QNetworkInterface::Phonet:
        return "Phonet";
        break;
    case QNetworkInterface::Ieee802154:
        return "Ieee802154";
        break;
    case QNetworkInterface::SixLoWPAN:
        return "SixLoWPAN";
        break;
    case QNetworkInterface::Ieee80216:
        return "Ieee80216/WiMAX";
        break;
    case QNetworkInterface::Ieee1394:
        return "Ieee1394/FireWire";
        break;
    }
    return "";
}

static QString interfaceFlagsToString(QNetworkInterface::InterfaceFlags f)
{
    QString ret;
    if (QNetworkInterface::IsUp & f/* == QNetworkInterface::IsUp*/) {
        ret += "Up ";
    }
    if (QNetworkInterface::IsRunning & f/* == QNetworkInterface::IsRunning*/) {
        ret += "Running ";
    }
    if (QNetworkInterface::CanBroadcast & f/* == QNetworkInterface::CanBroadcast*/) {
        ret += "Broadcast ";
    }
    if (QNetworkInterface::IsLoopBack & f) {
        ret += "LoopBack ";
    }
    if (QNetworkInterface::IsPointToPoint & f) {
        ret += "PointToPoint ";
    }
    if (QNetworkInterface::CanMulticast & f) {
        ret += "Multicast ";
    }

    return ret.trimmed();
}

struct NetCardInfo 
{
    QString interfaceName;
    QNetworkInterface::InterfaceType interfaceType;
    QNetworkInterface::InterfaceFlags interfaceFlags;
    QString macAddress;
    QString IPv4;
    QString IPv6;
    QString netmask;
    QString broadcast;
};

struct NetInfo 
{
    QString localHostName;
    QList<NetCardInfo> netcardInfo;

    void print() 
    {
        qDebug() << "localHostName:  " << localHostName;
        qDebug() << "\n";
        for (const auto& cardInfo : netcardInfo) {
            qDebug() << "interfaceName:  " << cardInfo.interfaceName;
            qDebug() << "interfaceType:  " << interfaceTypeToString(cardInfo.interfaceType);
            qDebug() << "interfaceFlags: " << interfaceFlagsToString(cardInfo.interfaceFlags);
            qDebug() << "macAddress:     " << cardInfo.macAddress;
            qDebug() << "IPv4:           " << cardInfo.IPv4;
            qDebug() << "IPv6:           " << cardInfo.IPv6;
            qDebug() << "netmask:        " << cardInfo.netmask;
            qDebug() << "broadcast:      " << cardInfo.broadcast;
            qDebug() << "\n";
        }
    }
};

static NetInfo getNetInfo()
{
    NetInfo info;

    info.localHostName = QHostInfo::localHostName();
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    for (const QNetworkInterface& interface : list) {

        if (interface.type() == QNetworkInterface::Loopback) {
            continue;
        }

        NetCardInfo cardInfo;
        cardInfo.interfaceName = interface.name();
        cardInfo.interfaceType = interface.type();
        cardInfo.interfaceFlags = interface.flags();
        cardInfo.macAddress = interface.hardwareAddress();

        QList<QNetworkAddressEntry> entryList = interface.addressEntries();
        for (const QNetworkAddressEntry& entry : entryList) {

            if (entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
                cardInfo.IPv6 = entry.ip().toString();
            } else if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
                cardInfo.IPv4 = entry.ip().toString();
                cardInfo.netmask = entry.netmask().toString();
                cardInfo.broadcast = entry.broadcast().toString();
            }
        }

        info.netcardInfo.append(cardInfo);
    }
    return info;
}
//
// main.cpp
//

#include 

#include "netinfo.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    NetInfo arg = getNetInfo();
    arg.print();

    return a.exec();
}

Qt获取本机网卡信息_第1张图片

你可能感兴趣的:(Qt)