Feat: Fügt eine Funktion hinzu, die die IP-Adresse eines Interface zurückgibt.
This commit is contained in:
13
src/main.rs
13
src/main.rs
@@ -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_interface::get_active_network_interface;
|
use crate::network::network_interface::{get_active_network_interface, get_interface_ip_address};
|
||||||
|
|
||||||
mod log;
|
mod log;
|
||||||
mod network;
|
mod network;
|
||||||
@@ -11,11 +11,11 @@ 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 network_interface : String;
|
||||||
|
|
||||||
let mut count = 0;
|
let mut count : i32 = 0;
|
||||||
loop {
|
loop {
|
||||||
let interface_str = get_active_network_interface().unwrap().trim().to_string();
|
let interface_str : String = get_active_network_interface().unwrap().trim().to_string();
|
||||||
|
|
||||||
if !interface_str.is_empty() {
|
if !interface_str.is_empty() {
|
||||||
network_interface = interface_str;
|
network_interface = interface_str;
|
||||||
@@ -30,8 +30,11 @@ fn main() {
|
|||||||
sleep(Duration::from_secs(1));
|
sleep(Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
log("main", &format!("Active network interface found: {}", network_interface), LogLevel::Info);
|
log("main", &*format!("Active network interface found: {}", network_interface), LogLevel::Info);
|
||||||
|
|
||||||
|
let interface_address = get_interface_ip_address(network_interface.as_str()).unwrap().trim().to_string();
|
||||||
|
|
||||||
|
log("main", &*format!("Interface address: {}", interface_address), LogLevel::Info);
|
||||||
|
|
||||||
log("main", "========== PROGRAMM END ==========", LogLevel::Info);
|
log("main", "========== PROGRAMM END ==========", LogLevel::Info);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ pub fn get_active_network_interface() -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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_interface", "Found active network interface.", LogLevel::Info);
|
||||||
return Some(ifname);
|
return Some(ifname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +89,7 @@ pub fn get_active_network_interface() -> Option<String> {
|
|||||||
if let Some(ifname) = rest.split(':').next() {
|
if let Some(ifname) = rest.split(':').next() {
|
||||||
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_interface", "Found active network interface.", LogLevel::Debug);
|
||||||
return Some(name.to_string());
|
return Some(name.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ if ($adapters) { $adapters.Name } else { 'NONE' }
|
|||||||
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
||||||
|
|
||||||
if stdout != "NONE" {
|
if stdout != "NONE" {
|
||||||
log::log("network_status", "Found active network interface.", LogLevel::Info);
|
log::log("network_interface", "Found active network interface.", LogLevel::Debug);
|
||||||
return Some(stdout);
|
return Some(stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,66 @@ if ($adapters) { $adapters.Name } else { '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_interface", "OS not supported.", LogLevel::Error);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ermittelt die IP-Adresse (mit Netzmaske) für eine angegebene Netzwerkschnittstelle
|
||||||
|
///
|
||||||
|
/// # Parameter
|
||||||
|
/// - `interface`: Name der Netzwerkschnittstelle
|
||||||
|
///
|
||||||
|
/// # Rückgabe
|
||||||
|
/// - Option<String>: IP-Adresse mit Netzmaske (z.B. "192.168.1.100/24") oder None wenn keine gefunden
|
||||||
|
pub fn get_interface_ip_address(interface: &str) -> Option<String> {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
|
let output = Command::new("ip")
|
||||||
|
.args(["addr", "show", interface])
|
||||||
|
.output();
|
||||||
|
|
||||||
|
if let Ok(out) = output {
|
||||||
|
if !out.status.success() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let stdout = String::from_utf8_lossy(&out.stdout);
|
||||||
|
for line in stdout.lines() {
|
||||||
|
if line.contains("inet ") {
|
||||||
|
if let Some(ip) = line.split_whitespace().nth(1) {
|
||||||
|
return Some(ip.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
let ps_cmd = format!(
|
||||||
|
r#"Get-NetIPAddress -InterfaceAlias '{}' -AddressFamily IPv4 | Select-Object IPAddress,PrefixLength | ForEach-Object {{ "{0}/{1}" -f $_.IPAddress,$_.PrefixLength }}"#,
|
||||||
|
interface
|
||||||
|
);
|
||||||
|
let output = Command::new("powershell")
|
||||||
|
.args(["-NoProfile", "-NonInteractive", "-Command", &ps_cmd])
|
||||||
|
.output();
|
||||||
|
|
||||||
|
if let Ok(out) = output {
|
||||||
|
if !out.status.success() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
||||||
|
if !stdout.is_empty() {
|
||||||
|
return Some(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||||
|
{
|
||||||
|
log::log("network_interface", "OS not supported.", LogLevel::Error);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user