Lightweight Python ICMP ping library for monitoring, SRE and networking tools statistics and hop count
Project description
pyminiping
Lightweight Python ICMP ping library for monitoring, SRE and networking tools.
pyminiping is a pure Python ICMP ping library designed for Linux servers and monitoring tools.
It sends ICMP echo packets and returns detailed latency statistics such as RTT metrics, packet loss, TTL information and estimated hop count.
The library supports IPv4, IPv6, DSCP, custom TTL, and includes a command-line interface.
⭐ If you find this project useful, please consider starring the repository.
Quick Start
Install:
pip install pyminiping
Simple example:
from pyminiping import ping
result = ping("8.8.8.8")
print(result.mean)
Example output:
0.0042
CLI usage:
sudo pyminiping 8.8.8.8
Features
- Pure Python ICMP Echo (RAW sockets)
- IPv4 and IPv6 support
- Detailed latency statistics
- Packet loss calculation
- TTL detection
- Estimated hop count
- OS guess based on TTL
- Customizable parameters:
- packet count
- timeout
- interval
- payload size
- TTL
- DSCP
- Percentile metrics (p95)
- CLI tool included
- No runtime dependencies
- Designed for monitoring systems
Installation
From PyPI
pip install pyminiping
From source
git clone https://github.com/roxy-wi/pyminiping.git
cd pyminiping
python3 -m pip install .
Example Usage
from pyminiping import ping, DestinationUnreachable
try:
result = ping("8.8.8.8", count=5, timeout=1, interval=0.2)
print(result)
print(result.as_dict())
except DestinationUnreachable as e:
print(f"Unreachable: {e.message} (code {e.code})")
except Exception as e:
print(f"Ping failed: {e}")
Example Output
PingResult(
sent=5,
received=5,
loss=0.0,
min=0.0044,
max=0.0062,
mean=0.0048,
median=0.0045,
jitter=0.0007,
rtt_list=[0.0062, 0.0044, 0.0045, 0.0044, 0.0045],
ttl=110,
hops=19,
os_guess='Windows',
p95=0.0061
)
Or as dictionary:
{
"sent": 5,
"received": 5,
"loss": 0.0,
"min": 0.0044,
"max": 0.0062,
"mean": 0.0048,
"median": 0.0045,
"jitter": 0.0007,
"rtt_list": [0.0062, 0.0044, 0.0045, 0.0044, 0.0045],
"ttl": 110,
"hops": 19,
"os_guess": "Windows",
"p95": 0.0061
}
PingResult Object
The ping() function returns a PingResult dataclass.
| Field | Description |
|---|---|
| sent | Number of packets sent |
| received | Number of packets received |
| loss | Packet loss percentage |
| min | Minimum RTT |
| max | Maximum RTT |
| mean | Average RTT |
| median | Median RTT |
| jitter | Standard deviation of RTT |
| p95 | 95th percentile |
| ttl | TTL from response |
| hops | Estimated hop count |
| os_guess | OS guess based on TTL |
| rtt_list | List of RTT values |
Example:
result = ping("1.1.1.1")
print(result.mean)
print(result.packet_loss)
print(result.success)
Latency Metrics Explained
pyminiping returns several latency metrics commonly used in networking.
| Metric | Description |
|---|---|
| min | Fastest response |
| max | Slowest response |
| mean | Average latency |
| median | Middle value |
| jitter | Standard deviation of latency |
| p95 | 95th percentile latency |
| loss | Packet loss percentage |
Example:
min=4ms
avg=5ms
max=30ms
p95=25ms
Meaning:
- most packets arrive in ~5ms
- occasional spikes reach 30ms
- 95% of packets arrive under 25ms
Command-Line Interface (CLI)
After installing pyminiping, the CLI tool becomes available.
pyminiping <host>
Example:
sudo pyminiping 8.8.8.8
CLI Options
| Option | Description | Default |
|---|---|---|
| host | Hostname or IP address | required |
| -c, --count | Number of packets | 4 |
| -t, --timeout | Timeout per packet (seconds) | 1 |
| -i, --interval | Interval between packets | 0.1 |
| -s, --size | Payload size in bytes | 8 |
| --ttl | Outgoing TTL / hop limit | system default |
| --dscp | DSCP value (0–63) | not set |
| --show-rtts | Print per-packet RTT | disabled |
| -j, --json | Output JSON | disabled |
CLI Examples
Basic ping:
sudo pyminiping 8.8.8.8
Send multiple packets:
sudo pyminiping 8.8.8.8 -c 10
Custom interval:
sudo pyminiping 8.8.8.8 -i 0.5
Set TTL:
sudo pyminiping 8.8.8.8 --ttl 32
Show packet RTTs:
sudo pyminiping 8.8.8.8 --show-rtts
Example output:
PING 8.8.8.8 with 8 bytes of data:
8 bytes from 8.8.8.8: seq=1 ttl=117 time=0.0042 sec
8 bytes from 8.8.8.8: seq=2 ttl=117 time=0.0041 sec
JSON output:
sudo pyminiping 8.8.8.8 -j
Common DSCP values
| Name | DSCP | Use Case |
|---|---|---|
| Default | 0 | Best effort |
| CS1 | 8 | Background traffic |
| AF11 | 10 | Low priority |
| AF21 | 18 | Standard |
| AF41 | 34 | High priority |
| EF | 46 | Voice / real-time |
Comparison with system ping
| Feature | system ping | pyminiping |
|---|---|---|
| ICMP Echo | ✔ | ✔ |
| IPv4 | ✔ | ✔ |
| IPv6 | ✔ | ✔ |
| RTT statistics | ✔ | ✔ |
| Jitter calculation | ✖ | ✔ |
| Percentiles | ✖ | ✔ |
| Python API | ✖ | ✔ |
| JSON output | ✖ | ✔ |
| DSCP support | Limited | ✔ |
Use Cases
Typical applications:
- monitoring systems
- SRE tooling
- network diagnostics
- infrastructure automation
Example monitoring check:
result = ping("1.1.1.1", count=3)
if not result.success:
alert("Connectivity issue")
Performance Notes
pyminiping is designed to be lightweight:
- minimal memory usage
- no subprocess calls
- no external dependencies
- suitable for monitoring agents
Security
pyminiping uses RAW sockets and requires elevated privileges.
Run with sudo:
sudo pyminiping 8.8.8.8
Allow Python RAW sockets:
sudo setcap cap_net_raw+ep $(readlink -f $(which python3))
More details:
https://man7.org/linux/man-pages/man7/capabilities.7.html
Troubleshooting
Error:
Root privileges or CAP_NET_RAW are required to create RAW socket
Solution:
Run the script with root privileges or allow RAW socket capability.
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyminiping-2.0.0.tar.gz.
File metadata
- Download URL: pyminiping-2.0.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2374d5396c2e040a5fce3658fa373bad4db9e383bb3aba53ed76da263de59852
|
|
| MD5 |
2a5a01ac151ea614eb9871c758a347e0
|
|
| BLAKE2b-256 |
4bcf22affffa75eb9977ce58da85217147b83a1a8d1ed1b513a55d58ff6a3b88
|
File details
Details for the file pyminiping-2.0.0-py3-none-any.whl.
File metadata
- Download URL: pyminiping-2.0.0-py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45f3245d9421c3c5f79220d7b06c8d6ba643f704f66f1165ae8624e2a0d5a6d8
|
|
| MD5 |
c252971beb78efc9c671114597fb0809
|
|
| BLAKE2b-256 |
e8b1d748d6a070a1afbb174399bd2b3e9d3c355143beef65ee221ba1364d67e8
|