# ip-tools
[](https://crates.io/crates/ip-tools)
[](https://docs.rs/ip-tools)
[](https://github.com/pplmx/ip-tools/actions)
[](https://coveralls.io/github/pplmx/ip-tools?branch=main)
[](https://rust-github.github.io/)
A small CLI tool to list network interfaces and retrieve the local IP address.
## Installation
### Cargo
* Install the rust toolchain in order to have cargo installed by following
[this](https://www.rust-lang.org/tools/install) guide.
* run `cargo install ip-tools`
## Usage
### Get the local IP address
```shell
ip-tools get
```
Example output:
```
192.168.1.100
```
### List all network interfaces
```shell
ip-tools list
```
Example output:
```
lo: 127.0.0.1
eth0: 192.168.1.100
wlan0: 10.0.0.5
```
### JSON output
Pass `--json` for machine-readable output (useful for scripting and automation):
```shell
ip-tools get --json
ip-tools list --json
```
Example output for `get --json`:
```json
{"ip":"192.168.1.100"}
```
Example output for `list --json`:
```json
[{"name":"lo","ip":"127.0.0.1"},{"name":"eth0","ip":"192.168.1.100"}]
```
### Show help
```shell
ip-tools --help
```
## Library usage
`ip-tools` is also published as a library crate (`ip_tools`). See the
[API documentation](https://docs.rs/ip-tools) for details.
```rust
use ip_tools::{get_local_ip, list_net_ifs};
// Retrieve the local IP address
if let Ok(ip) = get_local_ip() {
println!("local IP: {ip}");
}
// List all network interfaces
if let Ok(interfaces) = list_net_ifs() {
for (name, ip) in &interfaces {
println!("{name}: {ip}");
}
}
```
A runnable example is provided in [`examples/ip_info.rs`](examples/ip_info.rs):
```shell
cargo run --example ip_info
```
## License
Licensed under either of
* Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
See [CONTRIBUTING.md](CONTRIBUTING.md).