Skip to content

Instantly share code, notes, and snippets.

@xanarin
Created April 25, 2025 16:15
Show Gist options
  • Save xanarin/41971f713dcbeb8f33e8f13c7eb97207 to your computer and use it in GitHub Desktop.
Save xanarin/41971f713dcbeb8f33e8f13c7eb97207 to your computer and use it in GitHub Desktop.
Linux Packets Per Second (PPS) Utility
#!/usr/bin/python3
from typing import Tuple
import sys
import time
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("INTERFACE_NAME", help="Network interface to collect packet statistics for")
parser.add_argument("-i", "--interval", type=int, default=1, help="Interval for calculations in secods")
return parser.parse_args()
"""Returns a tuple of (count of rx packets, count of tx packets)"""
def get_packet_counts(iface: str) -> Tuple[int, int]:
with open(f"/proc/net/dev", "rb") as file:
data = file.read().decode()
try:
net_stat = next((l for l in data.splitlines() if l.startswith(iface)))
except StopIteration:
raise AssertionError(f"Failed to find statistics for interface {iface}")
net_field = net_stat.split()
return (int(net_field[2]), int(net_field[10]))
def main():
args = parse_args()
print(f"Packet statistics for {args.INTERFACE_NAME}:")
while True:
prev_rx, prev_tx = get_packet_counts(args.INTERFACE_NAME)
time.sleep(args.interval)
new_rx, new_tx = get_packet_counts(args.INTERFACE_NAME)
print(f"TX: {float(new_rx - prev_rx) / 1000:.1f} kpps RX {float(new_tx - prev_tx) / 1000:.1f} kpps")
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment