Crates.io | interface-rs |
lib.rs | interface-rs |
version | 0.2.10 |
source | src |
created_at | 2024-08-24 04:36:01.528236 |
updated_at | 2024-11-20 07:16:42.487953 |
description | Library for reading and writing Linux interfaces(5) files |
homepage | https://interface-rs.com |
repository | https://github.com/wiggels/interface-rs |
max_upload_size | |
id | 1349811 |
size | 66,447 |
A Rust library for parsing and manipulating an interfaces(5)
file.
This library provides structs and functions to load, parse, modify, and save network interface configurations in the Debian-style interfaces(5)
file format. The interfaces(5)
file is typically found at /etc/network/interfaces
on Debian-based systems but may be located elsewhere depending on your system configuration.
interfaces(5)
file.interfaces(5)
file format.Add interface-rs
to your Cargo.toml
:
[dependencies]
interface-rs = "0.2.0"
Then run:
cargo build
use interface_rs::NetworkInterfaces;
fn main() -> std::io::Result<()> {
// Load the interfaces file
let net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Iterate over interfaces
for iface in net_ifaces.interfaces.values() {
println!("Found interface: {}", iface.name);
}
Ok(())
}
use interface_rs::NetworkInterfaces;
fn main() -> std::io::Result<()> {
let net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Get a specific interface
if let Some(iface) = net_ifaces.get_interface("eth0") {
println!("Interface {} found.", iface.name);
println!("Auto: {}", iface.auto);
println!("Method: {:?}", iface.method);
} else {
println!("Interface eth0 not found.");
}
Ok(())
}
use interface_rs::NetworkInterfaces;
use interface_rs::interface::{Interface, Family};
fn main() -> std::io::Result<()> {
let mut net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Create a new interface using the builder pattern
let new_iface = Interface::builder("swp1")
.with_auto(true)
.with_allow("hotplug")
.with_family(Family::Inet)
.with_method("static")
.with_option("address", "192.168.100.1")
.with_option("netmask", "255.255.255.0")
.build();
// Add the new interface to the collection
net_ifaces.add_interface(new_iface);
// Save changes back to the file
net_ifaces.save()?;
Ok(())
}
use interface_rs::NetworkInterfaces;
use interface_rs::interface::{Interface, Family};
fn main() -> std::io::Result<()> {
let mut net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Retrieve and modify an existing interface using the builder pattern
if let Some(iface) = net_ifaces.get_interface("eth0") {
let modified_iface = iface.edit()
.with_method("static")
.with_option("address", "192.168.1.50")
.with_option("netmask", "255.255.255.0")
.build();
// Replace the existing interface with the modified one
net_ifaces.add_interface(modified_iface);
// Save changes back to the file
net_ifaces.save()?;
} else {
println!("Interface eth0 not found.");
}
Ok(())
}
use interface_rs::NetworkInterfaces;
fn main() -> std::io::Result<()> {
let mut net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Delete an interface by name
net_ifaces.delete_interface("eth0");
// Save changes back to the file
net_ifaces.save()?;
println!("Interface eth0 has been deleted.");
Ok(())
}
use interface_rs::NetworkInterfaces;
fn main() -> std::io::Result<()> {
let mut net_ifaces = NetworkInterfaces::load("/path/to/interfaces")?;
// Make changes to interfaces...
// Save changes back to the file
net_ifaces.save()?;
println!("Changes saved successfully.");
Ok(())
}
For more detailed information, please refer to the API documentation.
Contributions are welcome! If you'd like to contribute to interface-rs
, please follow these steps:
main
branch of the original repository.Please ensure your code adheres to the project's coding standards and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.
Note: This library is intended for use on systems that utilize the Debian-style interfaces(5)
file format. It may not be compatible with other network configuration systems.
For any issues or feature requests, please open an issue on the GitHub repository.