Last active
March 19, 2022 23:43
-
-
Save nathan-osman/4a2774927da6b72ca174c2fe14eab8a8 to your computer and use it in GitHub Desktop.
Enumerate network interfaces and addresses assigned to them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR) | |
project(netenum) | |
find_package(Qt5Network 5.4 REQUIRED) | |
add_executable(netenum main.cpp) | |
target_link_libraries(netenum Qt5::Network) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <QHostAddress> | |
#include <QNetworkAddressEntry> | |
#include <QNetworkInterface> | |
int main(int argc, char **argv) | |
{ | |
foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) { | |
std::cout | |
<< interface.humanReadableName().toStdString() | |
<< std::endl | |
<< " - " | |
<< ((interface.flags() & QNetworkInterface::IsLoopBack) ? "is" : "is not") | |
<< " loopback" | |
<< std::endl | |
<< " - " | |
<< ((interface.flags() & QNetworkInterface::CanMulticast) ? "can" : "cannot") | |
<< " multicast" | |
<< std::endl | |
<< " - addresses:" | |
<< std::endl; | |
foreach (QNetworkAddressEntry entry, interface.addressEntries()) { | |
std::cout | |
<< " - " | |
<< entry.ip().toString().toStdString() | |
<< std::endl | |
<< " - scope: " | |
<< entry.ip().scopeId().toStdString() | |
<< std::endl | |
<< " - " | |
<< (entry.ip().isLoopback() ? "is" : "is not") | |
<< " loopback" | |
<< std::endl; | |
} | |
std::cout << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment