Project 2 - Traceroute

Project Objectives

In this course project, you will develop a Python script that probes the route from the local machine to a given host address.


The traceroute program identifies the IP addresses of all intermediary machines in between the source and destination. A UDP-based traceroute program is discussed in the class. In this project, you will implement such a tool with the following features.

  • The tool must be implemented in python3 and utilize scapy for custom packet generation. Aside from the socket library, no additional libraries are allowed to be used.
  • The first line of the .py script must be the location of the Python3 interpreter: #!/bin/env python3
  • Input argument: The tool should accept a single argument - the hostname or IP address of the destination host.
  • Max hops: Your tool must be able to identify all IP addresses in the path with the distance less that or equal to 20 hops. (In contrast, the default limit for traceroute in Linux is 30 hops.)
  • Max probes: Your tool must attempt for only one probe for a node at a certain distance. (In contrast, the traceroute tool in Linux sends three probes for a specific distance by default.)
  • Destination port number: Your tool should pick a random destination port number in the range [33434,33464] for each probe. For example, the first probe may have the destination port number 33458, the second probe may have the destination port number 33442, etc.
    • This is different from traceroute in Linux, which begins all traceroutes with destination port number 33434 and increments by 1 for each probe. That is, the first probe has destination port number 33434, the second probe has destination port number 33435, etc.
  • Source port number: Your tool should pick a random high number for the source UDP port for each probe. (The port fields are two bytes each in size, so don't pick a random number higher than 216 or 65535!)
  • The tool must report each discovered IP address and hostname in the path along with the distance from the source. If the address is unknown for a distance, use *, similar to the Linux tool. For hostname, you can use the socket.gethostbyaddr() function in Python to do the translation from IP address to hostname, and catch any socket.herror exceptions and ignore them for machines that do not have a discoverable hostname.
    • The Linux-based tool reports the host name in addition to the discovered IP address whenever possible. This is out of scope of this project.
  • Your tool must stop sending probes as soon as it receives an ICMP Port Unreachable message from the target.
  • It is desirable to eliminate verbosity of Scapy within the tool. You may use conf.verb = 0 for this purpose.
  • It is desirable to report the discovered IP addresses along the path to the target in real-time fashion (similar to Linux-based tool), rather than computing a whole list of addresses up to the target and reporting them in a single burst.
  • At the top of the script file, briefly describe how your tool works.

As a rule of thumb, if your traceroute program output differs from the Linux-based tool to a great extent, then you are probably doing something wrong. Compare the traffic generated by your tool versus the traffic from the Linux-based tool. That may help you to tune your probing packets appropriately.

Getting Started

First, ensure traceroute is installed in your Linux VM:

sudo apt install traceroute

Then, start Wireshark, capture a traceroute sequence to (Google public DNS, a good example target), and stop capture.

wireshark &
# Start capture
# Stop capture

Model your code against the traceroute behavior you see in Wireshark and follow the project requirements above.

Note that your traceroute from the standard Linux utility should look something like this. If you don't see network hops, and instead see line after line of * * *, then you have a networking problem in your VM that should be solved before writing your Python code. (Switching from NAT to Bridged mode in your virtual machine may help).

$ traceroute
traceroute to (, 30 hops max, 60 byte packets
 1  _gateway (  2.277 ms  2.158 ms  2.052 ms
 2  unifi (  1.973 ms  1.760 ms  0.383 ms
 3 (  27.174 ms  27.114 ms  26.950 ms
 4 (  24.998 ms  24.955 ms  29.011 ms
 5 (  37.799 ms  37.757 ms  37.697 ms
 6 (  39.278 ms  33.564 ms  34.166 ms
 7 (  39.221 ms  41.085 ms  41.049 ms
 8 (  33.763 ms  33.119 ms  34.002 ms
 9 (  34.262 ms (  34.607 ms (  36.855 ms
10 (  36.859 ms (  37.488 ms (  37.745 ms
11 (  37.083 ms (  37.862 ms (  33.201 ms



Submit a single Python script that implements traceroute to the Canvas CMS Project 2 assignment.

If using PyCharm, do not submit your venv environment. Before submitting, test to ensure you can run your program outside of PyCharm on the command line.

Sample Output

Running the tool may generate an output similar to the following in a terminal in Ubuntu Linux.

$ sudo ./
traceroute to (
1.  _gateway (
2.  unifi (
3. (
4.  (
5.  (
6.  (
7. (
8.  (
9.  (
10. *
11. (
12. (
Reached target, ending traceroute
$ sudo ./
traceroute to (
1.  _gateway (
2.  unifi (
3. (
4.  (
5.  (
6.  (
7.  (
8. (
9.  (
10. *
11. *
12. *
13. *
14. *
15. *
16. *
17. *
18. *
19. *
20. *
$ sudo ./ bogus.fakewebsite.fake
Error: Invalid hostname or IP address