Ref: Passt die Funktion an, sodass sie das aktive Netzwerkinterface zurückgibt.

This commit is contained in:
2025-08-18 14:43:25 +02:00
parent 0a35f201d2
commit f9eb47fe27
3 changed files with 32 additions and 27 deletions

View File

@@ -3,7 +3,7 @@ use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use crate::log::LogLevel; use crate::log::LogLevel;
use crate::log::log; use crate::log::log;
use crate::network::network_card::is_network_card_up; use crate::network::network_interface::get_active_network_interface;
mod log; mod log;
mod network; mod network;
@@ -11,20 +11,27 @@ mod network;
fn main() { fn main() {
log("main", "========== PROGRAMM START ==========", LogLevel::Info); log("main", "========== PROGRAMM START ==========", LogLevel::Info);
let mut network_interface = String::new();
let mut count = 0; let mut count = 0;
loop { loop {
if count >= 10 { let interface_str = get_active_network_interface().unwrap().trim().to_string();
if !interface_str.is_empty() {
network_interface = interface_str;
break;
} else if count >= 10 {
log("main", "Couldn't find active network card, exiting.", LogLevel::Error); log("main", "Couldn't find active network card, exiting.", LogLevel::Error);
exit(1); exit(1);
} }
if is_network_card_up() {
break; log("main", "No active network card found, waiting 1 second.", LogLevel::Warn);
count = count+1;
sleep(Duration::from_secs(1));
} }
log("main", "No active network card found, waiting 10 seconds.", LogLevel::Warn); log("main", &format!("Active network interface found: {}", network_interface), LogLevel::Info);
count = count+1;
sleep(Duration::from_secs(10));
}
log("main", "========== PROGRAMM END ==========", LogLevel::Info); log("main", "========== PROGRAMM END ==========", LogLevel::Info);
} }

View File

@@ -1 +1,2 @@
pub mod network_card; pub mod network_card;
pub mod network_interface;

View File

@@ -6,9 +6,8 @@ use std::process::Command;
/// und nicht nur die Loopback-Schnittstelle. /// und nicht nur die Loopback-Schnittstelle.
/// ///
/// Rückgabe: /// Rückgabe:
/// - true: Mindestens eine NIC ist aktiv /// - Option<String>: Name der aktiven Netzwerkkarte oder None wenn keine gefunden
/// - false: Keine aktive NIC gefunden oder Fehler beim Prüfen pub fn get_active_network_interface() -> Option<String> {
pub fn is_network_card_up() -> bool {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
use std::fs; use std::fs;
@@ -59,7 +58,7 @@ pub fn is_network_card_up() -> bool {
continue; continue;
} }
// Optional: bekannte „logische Typen via Verzeichnis-Erkennung ausschließen // Optional: bekannte „logische" Typen via Verzeichnis-Erkennung ausschließen
// (Bridges, VLANs, Macvlan etc.) // (Bridges, VLANs, Macvlan etc.)
if iface_path.join("bridge").exists() if iface_path.join("bridge").exists()
|| iface_path.join("bonding").exists() || iface_path.join("bonding").exists()
@@ -72,7 +71,7 @@ pub fn is_network_card_up() -> bool {
// Wenn wir hier sind, haben wir eine aktive physische NIC // Wenn wir hier sind, haben wir eine aktive physische NIC
log::log("network_status", "Found active network interface.", LogLevel::Info); log::log("network_status", "Found active network interface.", LogLevel::Info);
return true; return Some(ifname);
} }
} }
@@ -82,7 +81,7 @@ pub fn is_network_card_up() -> bool {
if let Ok(out) = output { if let Ok(out) = output {
if !out.status.success() { if !out.status.success() {
return false; return None;
} }
let stdout = String::from_utf8_lossy(&out.stdout); let stdout = String::from_utf8_lossy(&out.stdout);
for line in stdout.lines() { for line in stdout.lines() {
@@ -91,13 +90,13 @@ pub fn is_network_card_up() -> bool {
let name = ifname.trim(); let name = ifname.trim();
if name != "lo" { if name != "lo" {
log::log("network_status", "Found active network interface.", LogLevel::Info); log::log("network_status", "Found active network interface.", LogLevel::Info);
return true; return Some(name.to_string());
} }
} }
} }
} }
} }
false None
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@@ -105,8 +104,8 @@ pub fn is_network_card_up() -> bool {
// Windows: PowerShell verwenden, um aktive Adapter zu finden (Status = Up). // Windows: PowerShell verwenden, um aktive Adapter zu finden (Status = Up).
// Wir ignorieren Loopback/Pseudo-Interfaces, indem wir Physical=true filtern. // Wir ignorieren Loopback/Pseudo-Interfaces, indem wir Physical=true filtern.
let ps_cmd = r#" let ps_cmd = r#"
$adapters = Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'} $adapters = Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'} | Select-Object -First 1
if ($adapters -and $adapters.Count -ge 1) { 'UP' } else { 'DOWN' } if ($adapters) { $adapters.Name } else { 'NONE' }
"#; "#;
let output = Command::new("powershell") let output = Command::new("powershell")
.args(["-NoProfile", "-NonInteractive", "-Command", ps_cmd]) .args(["-NoProfile", "-NonInteractive", "-Command", ps_cmd])
@@ -114,23 +113,21 @@ if ($adapters -and $adapters.Count -ge 1) { 'UP' } else { 'DOWN' }
if let Ok(out) = output { if let Ok(out) = output {
if !out.status.success() { if !out.status.success() {
return false; return None;
} }
let stdout = String::from_utf8_lossy(&out.stdout).to_ascii_uppercase(); let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
return if stdout.contains("UP") { if stdout != "NONE" {
log::log("network_status", "Found active network interface.", LogLevel::Info); log::log("network_status", "Found active network interface.", LogLevel::Info);
true return Some(stdout);
} else {
false
} }
} }
false None
} }
#[cfg(not(any(target_os = "linux", target_os = "windows")))] #[cfg(not(any(target_os = "linux", target_os = "windows")))]
{ {
log::log("network_status", "OS not supported.", LogLevel::Error); log::log("network_status", "OS not supported.", LogLevel::Error);
false None
} }
} }