From 755cc3c3b49f8c0b8b855a6b0c4c2cdc0dce121e Mon Sep 17 00:00:00 2001 From: Harm Verhagen Date: Thu, 12 Feb 2026 14:58:57 +0100 Subject: [PATCH 1/5] Fix probe_mtu() for macOS Fixes: https://github.com/netinvent/ofunctions/issues/6 --- ofunctions/network/__init__.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ofunctions/network/__init__.py b/ofunctions/network/__init__.py index a5c49ca..1cf332e 100644 --- a/ofunctions/network/__init__.py +++ b/ofunctions/network/__init__.py @@ -29,6 +29,7 @@ from ipaddress import IPv4Address, IPv6Address, AddressValueError import time import psutil +import platform from command_runner import command_runner from requests import get @@ -38,6 +39,8 @@ from ofunctions.threading import threaded, wait_for_threaded_result from ofunctions.misc import BytesConverter +is_macos = platform.system() == 'Darwin' + # python 2.7 compat fixes try: from typing import List, Tuple, Union, Iterable, Optional @@ -87,7 +90,20 @@ def ping( targets = ["1.1.1.1", "8.8.8.8", "208.67.222.222"] def _ping_host(target, retries, source_interface): - if os.name == "nt": + if is_macos: + # -c ...: number of packets to send + # -M do: do not fragment + # -s ...: packet size to send + # -i ...: interval (s), only root can set less than .2 seconds + # -W ...: timeous (s) + # -I ...: optional source interface name + # -D ...: do not fragment + if ip_type == 6: + command = "ping6 -c 1 -s {} -W {} -i {}".format( mtu_encapsulated, timeout, interval) + else: + command = "ping -c 1 -s {} -W {} -D -i {}".format( mtu_encapsulated, timeout, interval ) + encoding = "utf-8" + elif os.name == "nt": # -4/-6: IPType # -n ...: number of packets to send # -f: do not fragment @@ -118,7 +134,7 @@ def _ping_host(target, retries, source_interface): encoding = "utf-8" # Add ip_type if specified - if ip_type: + if ip_type and not is_macos: command += " -{}".format(ip_type) if source_interface: From 11cd010b4485738469b2c74edf3085744ba8f955 Mon Sep 17 00:00:00 2001 From: Harm Verhagen Date: Thu, 12 Feb 2026 15:16:38 +0100 Subject: [PATCH 2/5] No timeout on ping6 --- ofunctions/network/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ofunctions/network/__init__.py b/ofunctions/network/__init__.py index 1cf332e..2a395f5 100644 --- a/ofunctions/network/__init__.py +++ b/ofunctions/network/__init__.py @@ -99,7 +99,7 @@ def _ping_host(target, retries, source_interface): # -I ...: optional source interface name # -D ...: do not fragment if ip_type == 6: - command = "ping6 -c 1 -s {} -W {} -i {}".format( mtu_encapsulated, timeout, interval) + command = "ping6 -c 1 -s {} -i {}".format( mtu_encapsulated, interval) else: command = "ping -c 1 -s {} -W {} -D -i {}".format( mtu_encapsulated, timeout, interval ) encoding = "utf-8" From 175baf4c08ebb1bee96d3bd16bd12ede73274820 Mon Sep 17 00:00:00 2001 From: Harm Verhagen Date: Thu, 12 Feb 2026 15:19:46 +0100 Subject: [PATCH 3/5] Review comments pull request --- ofunctions/network/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ofunctions/network/__init__.py b/ofunctions/network/__init__.py index 2a395f5..ac81936 100644 --- a/ofunctions/network/__init__.py +++ b/ofunctions/network/__init__.py @@ -92,16 +92,18 @@ def ping( def _ping_host(target, retries, source_interface): if is_macos: # -c ...: number of packets to send - # -M do: do not fragment # -s ...: packet size to send # -i ...: interval (s), only root can set less than .2 seconds - # -W ...: timeous (s) + # -W ...: timeout (s) # -I ...: optional source interface name # -D ...: do not fragment if ip_type == 6: command = "ping6 -c 1 -s {} -i {}".format( mtu_encapsulated, interval) else: - command = "ping -c 1 -s {} -W {} -D -i {}".format( mtu_encapsulated, timeout, interval ) + command = "ping -c 1 -s {} -W {} -i {}".format( mtu_encapsulated, timeout, interval ) + if do_not_fragment: + command += " -D" + encoding = "utf-8" elif os.name == "nt": # -4/-6: IPType @@ -122,7 +124,7 @@ def _ping_host(target, retries, source_interface): # -M do: do not fragment # -s ...: packet size to send # -i ...: interval (s), only root can set less than .2 seconds - # -W ...: timeous (s) + # -W ...: timeout (s) # -I ...: optional source interface name command = "ping -c 1 -s {} -W {} -i {}".format( mtu_encapsulated, timeout, interval From 4e52dd6ed90b128159a28ead90a88b7ed43c8cb5 Mon Sep 17 00:00:00 2001 From: Harm Verhagen Date: Thu, 12 Feb 2026 15:22:13 +0100 Subject: [PATCH 4/5] Change default interval probe_mtu() to 0.2s greatly speeds up mtu scanning --- ofunctions/network/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ofunctions/network/__init__.py b/ofunctions/network/__init__.py index ac81936..5b41067 100644 --- a/ofunctions/network/__init__.py +++ b/ofunctions/network/__init__.py @@ -472,7 +472,7 @@ def probe_mtu(target, method="ICMP", min=1100, max=9000, source_interface=None): pass ping_args = [ - (target, mtu, 2, 4, 1, ip_type, True, False, source_interface) + (target, mtu, 2, 4, 0.2, ip_type, True, False, source_interface) for mtu in range(min, max + 1) ] From 4d4de81407dff296b95d95ae01687f0fb61debfa Mon Sep 17 00:00:00 2001 From: Harm Verhagen Date: Fri, 13 Feb 2026 09:40:26 +0100 Subject: [PATCH 5/5] Add interval param to probe_mtu() Add parameter interval. new defaul to 0.2s, speeds things up a lot. (was 1s before) I don't think there is any reason to do this slower. NB: 0.2 wors for non-root too. (only below 0.002 you require root) --- ofunctions/network/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ofunctions/network/__init__.py b/ofunctions/network/__init__.py index 5b41067..2d554c2 100644 --- a/ofunctions/network/__init__.py +++ b/ofunctions/network/__init__.py @@ -441,7 +441,7 @@ def get_public_hostname(ip=None): return None -def probe_mtu(target, method="ICMP", min=1100, max=9000, source_interface=None): +def probe_mtu(target, method="ICMP", min=1100, max=9000, source_interface=None, interval=0.2): # type: (Union[str, IPv4Address, IPv6Address], str, int, int, str) -> int """ Detects MTU to target @@ -472,7 +472,7 @@ def probe_mtu(target, method="ICMP", min=1100, max=9000, source_interface=None): pass ping_args = [ - (target, mtu, 2, 4, 0.2, ip_type, True, False, source_interface) + (target, mtu, 2, 4, interval, ip_type, True, False, source_interface) for mtu in range(min, max + 1) ]