fix: sub distros use ping6 instead of ping -6
This commit is contained in:
@@ -4,13 +4,30 @@ import sys
|
||||
import subprocess
|
||||
|
||||
|
||||
def ping(host, packet_size, is_ipv6=False):
|
||||
def has_ping6() -> bool:
|
||||
try:
|
||||
result = subprocess.run(['ping6', '-c', '1', '::1'],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL)
|
||||
return result.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def ping(host: str, packet_size: int, is_ipv6: bool = False) -> bool:
|
||||
ping6 = has_ping6()
|
||||
if is_ipv6:
|
||||
header_size = 48 # 40 bytes IPv6 header + 8 bytes ICMPv6 header
|
||||
cmd = ['ping', '-6', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
if ping6:
|
||||
cmd = ['ping6', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
else:
|
||||
cmd = ['ping', '-6', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
else:
|
||||
header_size = 28 # 20 bytes IP header + 8 bytes ICMP header
|
||||
cmd = ['ping', '-4', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
if ping6:
|
||||
cmd = ['ping', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
else:
|
||||
cmd = ['ping', '-4', '-c', '2', '-M', 'do', '-s', str(packet_size - header_size), host]
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
@@ -19,10 +36,10 @@ def ping(host, packet_size, is_ipv6=False):
|
||||
return False
|
||||
|
||||
|
||||
def bin_search_mtu(host, min_mtu, max_mtu, is_ipv6=False):
|
||||
def bin_search_mtu(host: str, min_mtu: int, max_mtu: int, is_ipv6: bool = False) -> int | None:
|
||||
l = min_mtu
|
||||
h = max_mtu
|
||||
ret = 0
|
||||
ret: int = 0
|
||||
|
||||
print(f"Testing {'IPv6' if is_ipv6 else 'IPv4'} MTU for {host}...")
|
||||
|
||||
@@ -48,9 +65,9 @@ def main():
|
||||
print(f"Usage: {sys.argv[0]} <domain> [min_mtu max_mtu]")
|
||||
sys.exit(1)
|
||||
|
||||
domain = sys.argv[1]
|
||||
min_search = sys.argv[2] if len(sys.argv) >= 4 else 1000
|
||||
max_search = sys.argv[3] if len(sys.argv) >= 4 else 1500
|
||||
domain: str = sys.argv[1]
|
||||
min_search: int = int(sys.argv[2]) if len(sys.argv) >= 4 else 1000
|
||||
max_search: int = int(sys.argv[3]) if len(sys.argv) >= 4 else 1500
|
||||
|
||||
ipv4_mtu = bin_search_mtu(domain, min_search, max_search, is_ipv6=False)
|
||||
if ipv4_mtu:
|
||||
|
||||
Reference in New Issue
Block a user