Skip to content

Commit

Permalink
(part2)parse /proc/net/dev manually instead of using ifconfig because…
Browse files Browse the repository at this point in the history
… its not portable
  • Loading branch information
sigmaSd committed Jun 19, 2020
1 parent 6fabc56 commit b615463
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/eltrafico_main/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,20 @@ fn tifconfig() {
}
// ifconfig
pub fn ifconfig() -> CatchAll<Vec<Interface>> {
let output = run!("ifconfig -a")?;
let output = String::from_utf8(output.stdout)?;

// get the first line of each paragraph of the output then parse it
let output: Vec<&str> = output.lines().collect();
let interfaces = output
// split by paragraph
.split(|l| l.is_empty())
// get the first line of each paragraph
.filter_map(|p| p.iter().next())
// parse the interface name and status
.filter_map(|row| {
let status = if row.contains("UP") {
Status::Up
} else {
Status::Down
};
let name = match row.split(':').next() {
Some(name) => name,
None => return None,
};
Some(Interface {
name: name.to_string(),
status,
})
})
.collect();

Ok(interfaces)
let raw_data = std::fs::read_to_string("/proc/net/dev")?;

//TODO: actually parse statue
raw_data
.lines()
.skip(2)
.filter_map(|l| l.split(':').next())
.map(|name| {
Ok(Interface {
name: name.trim().to_string(),
status: Status::Down,
})
})
.collect()
}

#[derive(PartialEq, Eq, Debug)]
Expand Down

0 comments on commit b615463

Please sign in to comment.