-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping_traceroute.py
49 lines (39 loc) · 1.58 KB
/
ping_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
import time
class Router:
def __init__(self, ip_address, name):
self.ip_address = ip_address
self.name = name
def ping(self, target_ip):
print(f"PING {target_ip} from {self.name} ({self.ip_address})")
time.sleep(0.5) # Simulated delay
print(f"Reply from {target_ip}")
def traceroute(self, target_ip):
print(f"TRACEROUTE to {target_ip} from {self.name} ({self.ip_address})")
hops = []
current_router = self
while current_router.ip_address != target_ip:
hops.append(current_router.ip_address)
time.sleep(0.5) # Simulated delay
# Simulate routing logic
if current_router.ip_address == "192.168.1.1":
next_router = Router("192.168.1.2", "Router 1")
elif current_router.ip_address == "192.168.1.2":
next_router = Router("192.168.1.3", "Router 2")
elif current_router.ip_address == "192.168.1.3":
next_router = Router("192.168.1.4", "Router 3")
else:
next_router = Router(target_ip, "Destination")
current_router = next_router
hops.append(current_router.ip_address)
print("Trace complete.")
print("Hops:")
print(" -> ".join(hops))
# Create routers
router1 = Router("192.168.1.1", "Router 1")
router2 = Router("192.168.1.2", "Router 2")
router3 = Router("192.168.1.3", "Router 3")
destination_router = Router("192.168.1.4", "Destination")
# Perform PING
router1.ping("192.168.1.1")
# Perform TRACEROUTE
router1.traceroute("192.168.1.4")