-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_mtu_probe.sh
More file actions
executable file
·94 lines (79 loc) · 2.18 KB
/
path_mtu_probe.sh
File metadata and controls
executable file
·94 lines (79 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
set -euo pipefail
# path_mtu_probe.sh
# Finds the largest ICMP payload that reaches a host without fragmentation.
usage() {
cat <<'EOF'
Usage: ./path_mtu_probe.sh [options]
Options:
--host HOST Destination host/IP (default: 1.1.1.1)
--min-payload N Minimum payload size to test (default: 1200)
--max-payload N Maximum payload size to test (default: 1472)
--count N Ping count per test (default: 1)
--timeout N Timeout seconds per ping (default: 1)
-h, --help Show help
Notes:
- Uses ping with DF bit (`-M do`) on Linux.
- Estimated path MTU is payload + 28 bytes (IPv4 + ICMP headers).
EOF
}
HOST="1.1.1.1"
MIN_PAYLOAD=1200
MAX_PAYLOAD=1472
COUNT=1
TIMEOUT=1
while [[ $# -gt 0 ]]; do
case "$1" in
--host) HOST="$2"; shift 2 ;;
--min-payload) MIN_PAYLOAD="$2"; shift 2 ;;
--max-payload) MAX_PAYLOAD="$2"; shift 2 ;;
--count) COUNT="$2"; shift 2 ;;
--timeout) TIMEOUT="$2"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
done
if ! command -v ping >/dev/null 2>&1; then
echo "ping command not found"
exit 1
fi
if [[ "$MIN_PAYLOAD" -gt "$MAX_PAYLOAD" ]]; then
echo "Invalid range: --min-payload must be <= --max-payload"
exit 1
fi
probe_payload() {
local payload="$1"
if ping -M do -s "$payload" -c "$COUNT" -W "$TIMEOUT" "$HOST" >/dev/null 2>&1; then
return 0
fi
return 1
}
echo "Path MTU probe timestamp: $(date -Iseconds)"
echo "host=$HOST"
echo "payload_range=${MIN_PAYLOAD}-${MAX_PAYLOAD}"
echo
low="$MIN_PAYLOAD"
high="$MAX_PAYLOAD"
best=-1
while [[ "$low" -le "$high" ]]; do
mid=$(( (low + high) / 2 ))
if probe_payload "$mid"; then
best="$mid"
low=$(( mid + 1 ))
echo "[OK] payload=$mid"
else
high=$(( mid - 1 ))
echo "[FRAG/FAIL] payload=$mid"
fi
done
echo
if [[ "$best" -lt 0 ]]; then
echo "No successful payload found in range ${MIN_PAYLOAD}-${MAX_PAYLOAD}."
echo "Summary: status=FAIL"
exit 2
fi
path_mtu=$(( best + 28 ))
echo "Largest payload without fragmentation: $best"
echo "Estimated path MTU: $path_mtu"
echo "Summary: status=OK payload=$best path_mtu=$path_mtu"
exit 0