I am trying to get MAC address for all physical ports available on my laptop for Windows/Linux/MAC OS. I also want to check if I can detect physical connection/disconnection of the network cable.
I tried using QNetworkInterface APIs but it is providing me all physical and logical interfaces. I am unable to get how I can differentiate the physical ports. Do we have any flags to do so?
I have tried some code:
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if (!(netInterface.flags() & QNetworkInterface::Ethernet))
{
return netInterface.hardwareAddress();
}
}
Following is the result of Network Interface, name, type and hardware address from my MAC:
(IsUp|IsRunning|CanBroadcast|CanMulticast) "en0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"<br/> (IsUp|IsRunning|CanBroadcast|CanMulticast) "p2p0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"<br/> (IsUp|IsRunning|CanBroadcast|CanMulticast) "awdl0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"<br/> (IsUp|IsRunning|CanBroadcast|CanMulticast) "en1" QNetworkInterface::Ethernet "XX:XX:XX:XX:XX:XX"<br/> (IsUp|IsRunning|CanBroadcast|CanMulticast) "en2" QNetworkInterface::Ethernet "XX:XX:XX:XX:XX:XX"<br/> (IsUp|IsRunning|CanBroadcast|CanMulticast) "bridge0" QNetworkInterface::Unknown "XX:XX:XX:XX:XX:XX"
PS: "XX:XX:XX:XX:XX:XX
" is their MAC address
I come to the conclusion that Qt cannot get deeper OS details. We need to touch the OS layer to achieve the same.
For Windows:
MIB_IF_ROW2 ifRow;
ifRow.InterfaceAndOperStatusFlags.ConnectorPresent
Check this flag to achieve the same.
For macOS:
static_cast<SCNetworkInterfaceRef>(CFArrayGetValueAtIndex(physicalVLANInterfaces, i)) for all CFIndex
For Linux:
File *fp = popen("lshw -class network | grep -A 1 \"bus info\" | grep name | awk -F': ' '{print $2}'", "r");