1 个不稳定版本
0.1.0 | 2024 年 4 月 5 日 |
---|
#9 in #conf
17KB
370 行代码(不含注释)
从 Rust 中与 Linux 交互。
安装
cargo add unixism
内容
resolv.conf
解析 /etc/resolv.conf
文件。
use unixism::dns;
fn main() {
let config = dns::resolv::parse_default().unwrap();
for nameserver in config.nameservers {
println!("{}", nameserver.to_string());
}
for domain in config.search_domains {
println!("{domain}");
}
for option in config.options {
match option {
dns::resolv::ConfigOption::Timeout(timeout) => {
println!("timeout: {timeout}");
}
_ => {}
}
}
}
解析任何类型的 io::Read
类型。
use std::fs;
use unixism::dns;
fn main() {
let config = dns::resolv::parse(fs::File::open("local.conf").unwrap()).unwrap();
for nameserver in config.nameservers {
println!("{}", nameserver.to_string());
}
for domain in config.search_domains {
println!("{domain}");
}
for option in config.options {
match option {
dns::resolv::ConfigOption::Timeout(timeout) => {
println!("timeout: {timeout}");
}
_ => {}
}
}
}
hosts
解析 /etc/hosts
文件。
use unixism::hosts;
fn main() {
for host in hosts::parse_default().unwrap() {
println!("ip: {}, names: {:#?}", host.ip, host.names);
}
}
解析任何类型的 io::Read
类型。
use std::fs;
use unixism::hosts;
fn main() {
for host in hosts::parse(fs::File::open("local.conf").unwrap()).unwrap() {
println!("ip: {}, names: {:#?}", host.ip, host.names);
}
}