-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute.py
51 lines (41 loc) · 1.93 KB
/
traceroute.py
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
import argparse
from scapy.all import IP, ICMP, UDP, sr1
import time
def traceroute(target_ip, max_hops=30, timeout=2, protocol="ICMP"):
ttl = 1
print(f"Traceroute to {target_ip}, max {max_hops} hops:\n")
while ttl <= max_hops:
# Prepare the packet with the given TTL
ip_layer = IP(dst=target_ip, ttl=ttl)
# Use ICMP or UDP based on user input
if protocol.upper() == "ICMP":
pkt = ip_layer / ICMP()
else:
pkt = ip_layer / UDP(dport=33434) # Using a high UDP port
# Send the packet and wait for a response
start_time = time.time()
response = sr1(pkt, timeout=timeout, verbose=False)
round_trip_time = (time.time() - start_time) * 1000 # Convert to milliseconds
if response is None:
print(f"{ttl}\t*\tRequest timed out")
else:
if response.haslayer(ICMP):
src_ip = response[IP].src
print(f"{ttl}\t{src_ip}\t{round_trip_time:.2f} ms")
# If we reach the target, exit
if src_ip == target_ip:
print("Reached the destination.")
break
else:
print(f"{ttl}\t*\tUnexpected response")
ttl += 1
def main():
parser = argparse.ArgumentParser(description="Traceroute Tool using Scapy")
parser.add_argument("-t", "--target", type=str, help="Target IP address or domain", required=True)
parser.add_argument("-p", "--protocol", type=str, choices=["ICMP", "UDP"], default="ICMP", help="Protocol to use (ICMP or UDP)")
parser.add_argument("-m", "--max-hops", type=int, default=30, help="Maximum number of hops")
parser.add_argument("-o", "--timeout", type=int, default=2, help="Timeout for each hop (seconds)")
args = parser.parse_args()
traceroute(args.target, max_hops=args.max_hops, timeout=args.timeout, protocol=args.protocol)
if __name__ == "__main__":
main()