From eb0b6c87d610d222903d8c1b9a0523474e9176ff Mon Sep 17 00:00:00 2001 From: Ryan Faulhaber Date: Tue, 19 Nov 2024 17:20:52 -0500 Subject: [PATCH] Add mac and IP address entries to `sys net` (#14389) # Description What it says on the tin, this change adds the `mac` and `ip` columns to the `sys net` command, where `mac` is the interface mac address and `ip` is a record containing ipv4 and ipv6 addresses as well as whether or not the address is loopback and multicast. I thought it might be useful to have this information available in Nushell. This change basically just pulls extra information out of the underlying structs in the `sysinfo::Networks` struct. Here's a screenshot from my system: ![Screenshot from 2024-11-19 11-59-54](https://github.com/user-attachments/assets/92c2d72c-b0d0-49c0-8167-9e1ce853acf1) # User-Facing Changes - Adds `mac` and `ip` columns to the `sys net` command, where `mac` contains the interface's mac address and `ip` contains information extracted from the `std::net::IpAddr` struct, including address, protocol, whether or not the address is loopback, and whether or not it's multicast # Tests + Formatting Didn't add any tests specifically, didn't seem like there were any relevant tests. Ran existing tests and formatting. --- crates/nu-command/src/system/sys/net.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/nu-command/src/system/sys/net.rs b/crates/nu-command/src/system/sys/net.rs index ee82f73c61..8e2ac19bf5 100644 --- a/crates/nu-command/src/system/sys/net.rs +++ b/crates/nu-command/src/system/sys/net.rs @@ -44,8 +44,29 @@ fn net(span: Span) -> Value { let networks = Networks::new_with_refreshed_list() .iter() .map(|(iface, data)| { + let ip_addresses = data + .ip_networks() + .iter() + .map(|ip| { + let protocol = match ip.addr { + std::net::IpAddr::V4(_) => "ipv4", + std::net::IpAddr::V6(_) => "ipv6", + }; + Value::record( + record! { + "address" => Value::string(ip.addr.to_string(), span), + "protocol" => Value::string(protocol, span), + "loop" => Value::bool(ip.addr.is_loopback(), span), + "multicast" => Value::bool(ip.addr.is_multicast(), span), + }, + span, + ) + }) + .collect(); let record = record! { "name" => Value::string(trim_cstyle_null(iface), span), + "mac" => Value::string(data.mac_address().to_string(), span), + "ip" => Value::list(ip_addresses, span), "sent" => Value::filesize(data.total_transmitted() as i64, span), "recv" => Value::filesize(data.total_received() as i64, span), };