| Crates.io | single-ping |
| lib.rs | single-ping |
| version | 0.1.0 |
| created_at | 2025-10-10 01:33:45.852913+00 |
| updated_at | 2025-10-10 01:33:45.852913+00 |
| description | Send single pings written in pure Rust |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1876406 |
| size | 37,817 |
A simple, lightweight ICMP ping library for Rust that supports both IPv4 and IPv6.
Add this to your Cargo.toml:
[dependencies]
single-ping = "0.1.0"
use single_ping::ping;
fn main() {
// Ping with 1000ms timeout and 32 bytes of data
match ping("8.8.8.8", 1000, 32) {
Ok(result) => {
if result.dropped {
println!("Packet dropped!");
} else {
println!("Ping successful! Latency: {}ms", result.latency_ms);
}
}
Err(e) => eprintln!("Ping failed: {}", e),
}
}
use single_ping::ping;
fn main() {
// Ping google.com with 2 second timeout and 56 bytes of data
match ping("google.com", 2000, 56) {
Ok(result) => {
println!("Latency: {}ms", result.latency_ms);
}
Err(e) => eprintln!("Error: {}", e),
}
}
use single_ping::ping;
fn main() {
let result = ping("example.com", 1000, 32).unwrap();
if result.dropped {
println!("No response received within timeout period");
} else {
println!("Response received in {}ms", result.latency_ms);
}
}
ping(host: &str, timeout: u64, size: u64) -> Result<PingResult, Box<dyn std::error::Error>>Sends an ICMP echo request to the specified host.
Parameters:
host - Target host as an IP address or hostname (e.g., "8.8.8.8" or "google.com")timeout - Maximum time to wait for a response, in millisecondssize - Size of the data payload in bytesReturns:
Ok(PingResult) - Contains information about the ping resultErr(_) - If the host cannot be resolved or network errors occurPingResultThe result of a ping operation.
Fields:
dropped: bool - Whether the packet was dropped (no response or invalid response)latency_ms: u64 - Round-trip time in millisecondsCreating raw ICMP sockets requires elevated privileges:
Linux/macOS:
sudo cargo run
Or set capabilities on the binary:
sudo setcap cap_net_raw+ep target/release/your_binary
Windows: Run your terminal or IDE as Administrator.
Run the examples with elevated privileges:
# Basic ping example (if you create one)
sudo cargo run --example basic_ping
# Run tests
sudo cargo test
socket2 - Cross-platform socket operationsApache-2.0
Contributions are welcome! Please feel free to submit a Pull Request.
You need to run with elevated privileges (see Requirements).
ping command works)Some networks may not support IPv6. The library will automatically use IPv4 or IPv6 based on the resolved address.