Skip to content

Commit

Permalink
Added ping-isp utility to auto-detect the ISP first hop.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Feb 18, 2024
1 parent de653a2 commit ba267bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions jaraco/net/ping-isp.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions newsfragments/+9574cd42.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ping-isp utility to auto-detect the ISP first hop.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ install_requires =
ifconfig-parser; sys_platform == 'darwin'
jsonpickle != 3.0.0
icmplib
autocommand

[options.packages.find]
exclude =
Expand Down

0 comments on commit ba267bb

Please sign in to comment.