diff --git a/jaraco/net/ping-isp.py b/jaraco/net/ping-isp.py new file mode 100644 index 0000000..580f934 --- /dev/null +++ b/jaraco/net/ping-isp.py @@ -0,0 +1,34 @@ +import itertools +import socket +import importlib +import functools + +import autocommand +import icmplib + + +def is_router(addr): + return addr.startswith('192.168') or 'amplifi' in socket.gethostbyaddr(addr) + + +def _patch_traceroute_privilege(): + """ + Force the socket objects to disable privilege. + """ + module = importlib.import_module('icmplib.traceroute') + module.ICMPv4Socket = functools.partial(module.ICMPv4Socket, privileged=False) + module.ICMPv6Socket = functools.partial(module.ICMPv6Socket, privileged=False) + + +@autocommand.autocommand(__name__) +def main(): + """ + Detect the ISP and ping it for some stats. + """ + _patch_traceroute_privilege() + trace = icmplib.traceroute('1.1.1.1', max_hops=3, fast=True) + addresses = (hop.address for hop in trace) + ISP = next(itertools.filterfalse(is_router, addresses)) + print('pinging', ISP, f'({socket.gethostbyaddr(ISP)[0]})') + host = icmplib.ping(ISP, count=30, privileged=False) + print(host) diff --git a/newsfragments/+9574cd42.feature.rst b/newsfragments/+9574cd42.feature.rst new file mode 100644 index 0000000..2472a88 --- /dev/null +++ b/newsfragments/+9574cd42.feature.rst @@ -0,0 +1 @@ +Added ping-isp utility to auto-detect the ISP first hop. \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 5e75805..1bda697 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,6 +34,7 @@ install_requires = ifconfig-parser; sys_platform == 'darwin' jsonpickle != 3.0.0 icmplib + autocommand [options.packages.find] exclude =