diff --git a/src/network/utils.rs b/src/network/utils.rs index e3d2453..30ccff0 100644 --- a/src/network/utils.rs +++ b/src/network/utils.rs @@ -1,7 +1,7 @@ -use std::process::Command; -use std::net::Ipv4Addr; use crate::log; use crate::log::LogLevel; +use std::net::Ipv4Addr; +use std::process::Command; /// Prüft, ob eine angegebene IP-Adresse oder URL erreichbar ist /// @@ -91,16 +91,19 @@ pub fn get_network_address(address: &str) -> Option { /// /// # Parameter /// - `mac`: MAC-Adresse im Format "xx:xx:xx:xx:xx:xx" +/// - `network`: Netzwerkadresse im Format "xxx.xxx.xxx.xxx/yy" /// /// # Rückgabe /// - Option: Die IP-Adresse des Geräts oder None wenn nicht gefunden -pub fn get_ip_from_mac(mac: &str) -> Option { +pub fn get_ip_from_mac(mac: &str, network: &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]) + .args(["-sn", "-n", "--system-dns", "-PR", network]) .output(); + log("network_utils", &*format!("{:?}", output), LogLevel::Debug); + match output { Ok(out) => { if !out.status.success() { @@ -108,10 +111,14 @@ pub fn get_ip_from_mac(mac: &str) -> Option { } let stdout = String::from_utf8_lossy(&out.stdout); + log("network_utils", &*stdout, LogLevel::Debug); + 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()); + if line.contains(mac) { + if let Some(ip_line) = line.split('\n').find(|l| l.contains("Nmap scan report for")) { + if let Some(ip) = ip_line.split_whitespace().last() { + return Some(ip.to_string()); + } } } }