diff --git a/src/network/utils.rs b/src/network/utils.rs index 2f8b9de..e3d2453 100644 --- a/src/network/utils.rs +++ b/src/network/utils.rs @@ -86,4 +86,49 @@ pub fn get_network_address(address: &str) -> Option { None } +/// Ermittelt die IP-Adresse zu einer gegebenen MAC-Adresse im lokalen Netzwerk. +/// Verwendet einen nmap-Scan, um das Gerät zu finden. +/// +/// # Parameter +/// - `mac`: MAC-Adresse im Format "xx:xx:xx:xx:xx:xx" +/// +/// # Rückgabe +/// - Option: Die IP-Adresse des Geräts oder None wenn nicht gefunden +pub fn get_ip_from_mac(mac: &str) -> Option { + #[cfg(any(target_os = "linux", target_os = "windows"))] + { + let output = Command::new("nmap") + .args(["-sn", "-n", "--system-dns", "-PR", "-PS22,80,443,445", "-PA80,443", "-PU161", mac]) + .output(); + match output { + Ok(out) => { + if !out.status.success() { + return None; + } + + let stdout = String::from_utf8_lossy(&out.stdout); + for line in stdout.lines() { + if line.contains("Nmap scan report for") { + if let Some(ip) = line.split_whitespace().last() { + return Some(ip.to_string()); + } + } + } + None + } + Err(e) => { + if e.kind() == std::io::ErrorKind::NotFound { + log::log("network_utils", "nmap is not installed on the system!", LogLevel::Error); + } + None + } + } + } + + #[cfg(not(any(target_os = "linux", target_os = "windows")))] + { + log::log("network_utils", "OS not supported.", LogLevel::Error); + None + } +} \ No newline at end of file