From 4324abd5ea02a2bab69cf94d647bef01fb049013 Mon Sep 17 00:00:00 2001 From: talksik Date: Thu, 8 Feb 2024 16:42:33 -0800 Subject: [PATCH] decently working showing available network interfaces --- networkscanner.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/networkscanner.cpp b/networkscanner.cpp index 2c5edf6..3a2571c 100644 --- a/networkscanner.cpp +++ b/networkscanner.cpp @@ -1,6 +1,23 @@ #include "networkscanner.h" #include #include +#include +#include +#include +#include + +std::string exec(const char* cmd) { + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd, "r"), pclose); + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; +} QList NetworkScanner::scanForNetworks() { @@ -11,5 +28,20 @@ QList NetworkScanner::scanForNetworks() Network example1 = Network("Gita 2.4Ghz"); networks.append(example1); + std::string result = exec("nmcli -t -f ssid dev wifi"); + + std::vector ssids; + std::istringstream iss(result); + std::string line; + while (std::getline(iss, line)) { + ssids.push_back(line); + } + + for (const std::string &ssid : ssids) { + std::cout << ssid << std::endl; + Network newNetwork = Network(ssid.c_str()); + networks.append(newNetwork); + } + return networks; }