Oder du trackst /sys/class/net/<interface>/statistics/rx_bytes
über einen gewissen Zeitraum.
PS: Mir war langweilig:
#! /usr/bin/env python3
"""Measure network speed on interfaces."""
from argparse import ArgumentParser, Namespace
from functools import partial
from multiprocessing import Pool
from pathlib import Path
from time import sleep
INTERFACES = Path('/sys/class/net')
def measure_and_print(interface: str, *, seconds: int | float = 10) -> None:
"""Print measured interface speed."""
print(f'{interface}:', measure(interface, seconds=seconds), 'bytes/sec')
def measure(interface: str, *, seconds: int | float = 10) -> float:
"""Measure interface over the given time in bytes / sec."""
start = rx_bytes(interface)
sleep(seconds)
end = rx_bytes(interface)
return (end - start) / seconds
def rx_bytes(interface: str) -> int:
"""Return the recived bytes of the respective interface."""
with rx_bytes_file(interface).open('r', encoding='utf-8') as file:
return int(file.read())
def rx_bytes_file(interface: str) -> Path:
"""Return the path to the rx_bytes file of the given interface."""
return INTERFACES / interface / 'statistics/rx_bytes'
def get_args(description: str = __doc__) -> Namespace:
"""Return the parsed command line arguments."""
parser = ArgumentParser(description=description)
parser.add_argument('interface', nargs='+')
parser.add_argument(
'-i', '--interval', type=float, default=10,
help='tracking interval in seconds'
)
return parser.parse_args()
def main() -> None:
"""Run the script."""
args = get_args()
with Pool() as pool:
pool.map(partial(measure_and_print, seconds=args.interval), args.interface)
if __name__ == '__main__':
main()
/home/rne〉./speed.py wlo1 lo 2023-04-22 12:44:47
wlo1: 782.5 bytes/sec
lo: 0.0 bytes/sec
/home/rne〉 2023-04-22 12:44:58