Skip to content

Instantly share code, notes, and snippets.

@nathan-osman
Last active March 19, 2022 23:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-osman/4a2774927da6b72ca174c2fe14eab8a8 to your computer and use it in GitHub Desktop.
Save nathan-osman/4a2774927da6b72ca174c2fe14eab8a8 to your computer and use it in GitHub Desktop.
Enumerate network interfaces and addresses assigned to them
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)
#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