| 299 | } |
| 300 | |
| 301 | std::string get_gateway_ip() { |
| 302 | std::string cmd = |
| 303 | "ip route show default | awk '/default/ {print $3}' | grep " |
| 304 | "'^192\\.168\\.168'"; |
| 305 | FILE* pipe = popen(cmd.c_str(), "r"); |
| 306 | if (!pipe) { |
| 307 | openhd::log::get_default()->warn("Failed to run command: {}", cmd.c_str()); |
| 308 | throw std::runtime_error("Failed to run command."); |
| 309 | } |
| 310 | |
| 311 | char buffer[128]; |
| 312 | std::string result; |
| 313 | if (fgets(buffer, sizeof(buffer), pipe) != nullptr) { |
| 314 | result = buffer; |
| 315 | } |
| 316 | pclose(pipe); |
| 317 | |
| 318 | // Trim trailing newline |
| 319 | if (!result.empty()) { |
| 320 | result.erase(result.find_last_not_of("\n\r") + 1); |
| 321 | } |
| 322 | |
| 323 | openhd::log::get_default()->warn("Filtered Gateway IP: {}", result.c_str()); |
| 324 | |
| 325 | return result; |
| 326 | } |
| 327 | |
| 328 | bool check_ip_alive(const std::string& ip, int port = 23) { |
| 329 | openhd::log::get_default()->warn("Checking if IP {} is alive on port {}", ip, |
no test coverage detected