28

In a case I can use only UDP and ICMP protocols, how can I discover, in bytes, the path MTU for packet transfer from my computer to a destination IP?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
URL87
  • 393
  • 1
  • 3
  • 7
  • Do you want to solve this programmatically or with ping? Ping and either shell programming or combined with traceroute are options for example. Also do you want ipv4 or ipv6? – Max Dec 30 '12 at 17:46
  • @Max: shell programming it sounds good , have an idea for that ? – URL87 Dec 30 '12 at 17:58
  • 2
    I thought about using `ping -s $SIZE -c 1 -M dont` and lowering that `SIZE` value till it works, or just having a look at the `ICMP` returns via `tcpdump` if we are talking ipv6 but `traceroute` has this built in I just realized. – Max Dec 30 '12 at 18:06

3 Answers3

29

I believe what you are looking for, is easiest gotten via traceroute --mtu <target>; maybe with a -6 switch thrown in for good measure depending on your interests. Linux traceroute uses UDP as a default, if you believe your luck is better with ICMP try also -I.

Max
  • 957
  • 6
  • 14
24

A quick search with apropos mtu on CentOS5 turned out:

tracepath traces path to a network host discovering MTU along this path

This command even works unprivileged in user space.

Nils
  • 18,202
  • 11
  • 46
  • 82
  • I prefer this over `traceroute` since it returns a simple result at the end and I don't have to look for the last hop that returned a different MTU. – comfreak Aug 14 '17 at 17:22
  • Alas, `tracepath` doesn't let you choose which network interface to use. You can choose with `traceroute` (using `-i`). – Ian D. Allen Feb 14 '20 at 19:35
3

Under Python, you are just one install away:

pip install ofunctions.network

Then you can run the following python code

from ofunctions.network probe_mtu

print(probe_mtu(target_IP))

This is the simplest syntax, but you may also force ICMPv6 or ICMPv4 discovery, and limit the minimum and maximum probe settings in order for the probing to happen faster.

Disclaimer: I am the author of ofunctions ;)

Orsiris de Jong
  • 393
  • 2
  • 10
  • I guessed that you meant `print(probe_mtu("142.250.187.206"))`, but that just gave me `ValueError: Both sides of the argument list are unexpected results`. Did I misunderstand your instructions? – Robin Green Dec 12 '21 at 21:57
  • I just tried that code again. Works for me. What version of python and ofunctions.network do you use ? – Orsiris de Jong Dec 17 '21 at 10:04
  • Python 3.9.9 and ofunctions.network-1.0.0 – Robin Green Dec 17 '21 at 16:48
  • I've just tried with Python 3.9.9 x64 on Win10 x64 without any trouble. There may be two solutions, or you don't have internet while trying this command (or at least ECHO request filtering somewhere), or your ping command is not standard. Can you give more details on your environment in order to replicate the issue ? – Orsiris de Jong Dec 20 '21 at 08:26
  • Made an improvement of ofunctions.network in order to print an obvious message when ICMP request could not be done. See https://github.com/netinvent/ofunctions/commit/15dcdeb8fbf4f1a49cfbd1581081b378dbe47fd5 – Orsiris de Jong Dec 22 '21 at 11:02
  • 1
    It's pretty slow, but it works. If you want to see some progress, run tcpdump along side and see it binary searching (i believe) the value for the path mtu. Rough command: `sudo tcpdump -i any icmp -n` – Valer Feb 07 '22 at 15:39