nu_command/system/sys/
net.rs1use super::trim_cstyle_null;
2use nu_engine::command_prelude::*;
3use sysinfo::Networks;
4
5#[derive(Clone)]
6pub struct SysNet;
7
8impl Command for SysNet {
9 fn name(&self) -> &str {
10 "sys net"
11 }
12
13 fn signature(&self) -> Signature {
14 Signature::build("sys net")
15 .filter()
16 .category(Category::System)
17 .input_output_types(vec![(Type::Nothing, Type::table())])
18 }
19
20 fn description(&self) -> &str {
21 "View information about the system network interfaces."
22 }
23
24 fn run(
25 &self,
26 _engine_state: &EngineState,
27 _stack: &mut Stack,
28 call: &Call,
29 _input: PipelineData,
30 ) -> Result<PipelineData, ShellError> {
31 Ok(net(call.head).into_pipeline_data())
32 }
33
34 fn examples(&self) -> Vec<Example> {
35 vec![Example {
36 description: "Show info about the system network",
37 example: "sys net",
38 result: None,
39 }]
40 }
41}
42
43fn net(span: Span) -> Value {
44 let networks = Networks::new_with_refreshed_list()
45 .iter()
46 .map(|(iface, data)| {
47 let ip_addresses = data
48 .ip_networks()
49 .iter()
50 .map(|ip| {
51 let protocol = match ip.addr {
52 std::net::IpAddr::V4(_) => "ipv4",
53 std::net::IpAddr::V6(_) => "ipv6",
54 };
55 Value::record(
56 record! {
57 "address" => Value::string(ip.addr.to_string(), span),
58 "protocol" => Value::string(protocol, span),
59 "loop" => Value::bool(ip.addr.is_loopback(), span),
60 "multicast" => Value::bool(ip.addr.is_multicast(), span),
61 },
62 span,
63 )
64 })
65 .collect();
66 let record = record! {
67 "name" => Value::string(trim_cstyle_null(iface), span),
68 "mac" => Value::string(data.mac_address().to_string(), span),
69 "ip" => Value::list(ip_addresses, span),
70 "sent" => Value::filesize(data.total_transmitted() as i64, span),
71 "recv" => Value::filesize(data.total_received() as i64, span),
72 };
73
74 Value::record(record, span)
75 })
76 .collect();
77
78 Value::list(networks, span)
79}