Traceroute Logging Tool is a Python-based utility designed to perform traceroute operations to a specified host. It provides tabular results, highlights route changes, and logs output to a file if desired. The tool supports ICMP, TCP, and UDP protocols.
- Automatically selects the best network interface based on MAC and IP.
- Option to specify a network interface and gateway manually.
- Logs traceroute results with RTT (Round Trip Time) and packet loss details.
- Outputs changes in routes in a highlighted format using `colorama`.
- Supports ICMP, TCP, and UDP protocols.
- Python 3.6+
- [Scapy](https://scapy.net/\) library
- [colorama](https://pypi.org/project/colorama/\)
- Python standard asynchronous libraries (`asyncio`)
-
Clone the repository or download the source code:
git clone https://github.com/yourusername/traceroute-logging-tool.git cd traceroute-logging-tool -
Create and activate a virtual environment:
python3 -m venv env source env/bin/activate # On Windows: env\\Scripts\\activate
-
Install dependencies:
pip install -r requirements.txt
Run the script with the required arguments:
python main.py <endpoint> [options]
<endpoint>: Target host (IP address or domain name).
-i, --interval: Interval between iterations in seconds (default: 1.0).-t, --timeout: Timeout for probe in seconds (default: 1.0).-n, --max_hops: Maximum number of hops (default: 32).-c, --count: Number of iterations (default: infinite).-p, --protocol: Protocol to use (tcp, udp, icmp) (default: icmp).-o, --output: File path to log the output (default: print to console).--port: Port to use for TCP/UDP (optional).--packet_size: Size of the packet in bytes (default: 64).--interface: Specify a network interface to use (optional). If not provided, the tool selects the default interface based on MAC and IP.
-
Perform traceroute to
8.8.8.8using ICMP:python main.py 8.8.8.8 -p icmp
-
Perform traceroute to
google.comwith a 2-second interval and log the results to a file:python main.py google.com -i 2 -o /var/log/tracelog
-
Perform traceroute to
example.comusing TCP protocol on port 80 with a maximum of 16 hops:python main.py example.com -p tcp --port 80 -n 16
-
Perform traceroute to
example.comusing UDP protocol with a packet size of 128 bytes and log the results to a file:python main.py example.com -p udp --packet_size 128 -o /var/log/tracelog
-
Perform traceroute to
example.comwith a 1-second interval, a timeout of 2 seconds, and log the results to a file:python main.py example.com -i 1 -t 2 -o /var/log/tracelog
-
Perform traceroute to
example.comusing a specific network interface:python main.py example.com --interface eth0
create_packet(host, ttl, protocol, port, packet_size): Creates a packet based on the protocol and IP version.send_probe(host, ttl, timeout, protocol, port, packet_size): Sends a packet with the specified TTL and returns the response.send_probe_with_semaphore(host, ttl, timeout, protocol, port, packet_size, semaphore): Sends a packet with a semaphore to limit concurrency.traceroute(host, timeout, max_hops, protocol, port, packet_size, semaphore): Performs traceroute to the specified host and returns the result.pad_string(string, width): Pads string to ensure it has the correct width, considering color codes.print_row(values, file=None): Prints a row of the table.remove_previous_duplicates(lst): Removes previous duplicates, keeping only the last occurrence.compare_routes(old_route, new_route): Compares the old route with the new route and highlights changes.main(endpoint, interval, timeout, max_hops, count, protocol, output_file, port, packet_size, interface): Main function to execute traceroute and print/log the results.
- The script resolves the domain name to an IP address.
- It performs traceroute by sending packets with increasing TTL values.
- It records the IP addresses and RTT (Round Trip Time) for each hop.
- It outputs the results to the console or logs them to a file.
- It highlights packet losses and changes in the route using the `colorama` library for console output.
Here is an example of the output produced by the script:
python main.py 93.170.220.1:
2024-07-27 17:56:39 | 10.32.57.129 | 10.32.1.1 | 10.254.0.1 | 10.255.2.1 | 93.170.220.1
2024-07-27 17:56:39 | 80.5 | 54.1 | 49.9 | 46.2 | 54.1
2024-07-27 17:56:41 | 44.1 | 106.0 | 54.2 | 52.7 | 74.1
2024-07-27 17:56:42 | 49.3 | 51.6 | 39.2 | 43.2 | 43.4
2024-07-27 17:56:43 | 41.0 | 46.1 | 38.2 | 42.2 | 46.0
2024-07-27 17:56:45 | 40.1 | 354.1 | 38.1 | 38.1 | 38.1
2024-07-27 17:56:46 | 53.0 | 38.1 | 38.1 | 38.1 | 38.1
2024-07-27 17:56:47 | 40.3 | 38.1 | 38.1 | 38.1 | 38.1
2024-07-27 17:56:49 | Loss | 103.3 | 38.3 | 38.3 | 38.3
2024-07-27 17:56:52 | Loss | 283.7 | 34.4 | 42.3 | 46.3
2024-07-27 17:56:53 | 64.6 | 450.3 | 50.0 | 42.2 | 58.4
2024-07-27 17:56:55 | 53.4 | 46.1 | 38.1 | 38.2 | 42.1
2024-07-27 17:56:56 | 29.2 | 23.5 | 23.6 | 31.9 | 31.5
2024-07-27 17:56:57 | 44.6 | 38.1 | 38.1 | 49.9 | 46.3
2024-07-27 17:56:58 | 48.2 | 42.1 | 38.1 | 38.1 | 38.1
- The first row shows the IP addresses of each hop.
- The second row shows the RTT (Round Trip Time) in milliseconds for each corresponding hop.
- Each subsequent iteration adds a new set of measurements.
- "Loss" indicates a packet loss at that specific hop.
This project is licensed under the [MIT License](MIT-LICENSE.txt).