mirror of
https://github.com/Atsukoro1/ponyfetch.git
synced 2024-11-27 06:17:59 +01:00
test: 💍 Ipv6
This commit is contained in:
parent
9e0f431026
commit
3ab8ca5ec8
1 changed files with 94 additions and 49 deletions
|
@ -28,59 +28,104 @@ pub fn get_ipaddr() -> String {
|
|||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn get_ipaddr() -> String {
|
||||
let final_str: Mutex<String> = Mutex::new(String::new());
|
||||
let intr = file_open("/proc/net/route");
|
||||
let mut final_str = String::new();
|
||||
let mut selected_intr = String::new();
|
||||
const KNOWN_INTERFACES: [&str; 10] = [
|
||||
"enp0s", "eth", "wlan", "wlp",
|
||||
"enx", "en", "wl", "wwp",
|
||||
"wwan", "wwn",
|
||||
];
|
||||
|
||||
let lines: &Vec<&str> = &intr.lines().collect();
|
||||
let mut interface = String::new();
|
||||
let intr_tree: String = file_open("/proc/net/dev");
|
||||
|
||||
lines.into_iter().for_each(|line| {
|
||||
if line.contains("00000000") {
|
||||
interface = line.split("\t").collect::<Vec<&str>>()[0].to_string();
|
||||
let lines = intr_tree.lines()
|
||||
.collect::<Vec<&str>>();
|
||||
|
||||
for line in lines.iter().skip(2) {
|
||||
let mut line = line.split_whitespace();
|
||||
let interface = line.next().unwrap();
|
||||
|
||||
if KNOWN_INTERFACES.iter().any(|&x| interface.contains(x)) {
|
||||
selected_intr = interface.to_string().replace(":", "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let output = Command::new("ifconfig")
|
||||
.arg(interface.clone())
|
||||
.output()
|
||||
.expect("Failed to execute ifconfig");
|
||||
// Get ip for the selected interface
|
||||
let connect_tree: String = file_open("/proc/net/if_inet6");
|
||||
|
||||
let output = String::from_utf8(output.stdout).unwrap();
|
||||
let lines = connect_tree.lines()
|
||||
.collect::<Vec<&str>>();
|
||||
|
||||
let lines: &Vec<&str> = &output.lines().clone().collect();
|
||||
for line in lines.iter() {
|
||||
let mut line = line.split_whitespace();
|
||||
|
||||
let mut next: bool = false;
|
||||
let hexa = line.next().unwrap().trim();
|
||||
let interface = line.nth(4).unwrap().trim();
|
||||
|
||||
let process_ip = |line: &str| {
|
||||
let ip = line.split(" ").collect::<Vec<&str>>()[1].to_string();
|
||||
final_str.lock().unwrap().push_str(&ip);
|
||||
if interface == selected_intr {
|
||||
final_str = format!("{} ({})", hexa, interface);
|
||||
};
|
||||
|
||||
lines.into_iter().for_each(|line| {
|
||||
if next {
|
||||
line.replace("\t", "")
|
||||
.split(" ")
|
||||
.collect::<Vec<&str>>()
|
||||
.into_iter()
|
||||
.for_each(|item| {
|
||||
if item.contains("inet") {
|
||||
process_ip(item);
|
||||
}
|
||||
});
|
||||
|
||||
next = false;
|
||||
}
|
||||
|
||||
if line.contains(&interface) {
|
||||
next = !next;
|
||||
final_str
|
||||
}
|
||||
});
|
||||
|
||||
let x = final_str
|
||||
.lock()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.add(format!(" ({})", interface).as_str());
|
||||
// #[cfg(target_os = "linux")]
|
||||
// pub fn get_ipaddr() -> String {
|
||||
// let final_str: Mutex<String> = Mutex::new(String::new());
|
||||
// let intr = file_open("/proc/net/route");
|
||||
|
||||
x
|
||||
}
|
||||
// let lines: &Vec<&str> = &intr.lines().collect();
|
||||
// let mut interface = String::new();
|
||||
|
||||
// lines.into_iter().for_each(|line| {
|
||||
// if line.contains("00000000") {
|
||||
// interface = line.split("\t").collect::<Vec<&str>>()[0].to_string();
|
||||
// }
|
||||
// });
|
||||
|
||||
// let output = Command::new("ifconfig")
|
||||
// .arg(interface.clone())
|
||||
// .output()
|
||||
// .expect("Failed to execute ifconfig");
|
||||
|
||||
// let output = String::from_utf8(output.stdout).unwrap();
|
||||
|
||||
// let lines: &Vec<&str> = &output.lines().clone().collect();
|
||||
|
||||
// let mut next: bool = false;
|
||||
|
||||
// let process_ip = |line: &str| {
|
||||
// let ip = line.split(" ").collect::<Vec<&str>>()[1].to_string();
|
||||
// final_str.lock().unwrap().push_str(&ip);
|
||||
// };
|
||||
|
||||
// lines.into_iter().for_each(|line| {
|
||||
// if next {
|
||||
// line.replace("\t", "")
|
||||
// .split(" ")
|
||||
// .collect::<Vec<&str>>()
|
||||
// .into_iter()
|
||||
// .for_each(|item| {
|
||||
// if item.contains("inet") {
|
||||
// process_ip(item);
|
||||
// }
|
||||
// });
|
||||
|
||||
// next = false;
|
||||
// }
|
||||
|
||||
// if line.contains(&interface) {
|
||||
// next = !next;
|
||||
// }
|
||||
// });
|
||||
|
||||
// let x = final_str
|
||||
// .lock()
|
||||
// .unwrap()
|
||||
// .to_string()
|
||||
// .add(format!(" ({})", interface).as_str());
|
||||
|
||||
// x
|
||||
// }
|
Loading…
Reference in a new issue