-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_sudo.py
More file actions
47 lines (39 loc) · 1.35 KB
/
no_sudo.py
File metadata and controls
47 lines (39 loc) · 1.35 KB
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
import socket
import struct
import time
def traceroute(host, max_hops=30, timeout=2):
try:
dest_addr = socket.gethostbyname(host)
except socket.gaierror:
print(f"Cannot resolve {host}")
return
port = 33434
print(f"Tracing route to {host} [{dest_addr}] with max {max_hops} hops:")
for ttl in range(1, max_hops + 1):
# Create receiver and sender sockets
recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Set TTL on sending socket
send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
recv_socket.settimeout(timeout)
recv_socket.bind(("", port))
# Send empty UDP packet
send_socket.sendto(b"", (host, port))
curr_addr = None
try:
_, curr_addr = recv_socket.recvfrom(512)
curr_addr = curr_addr[0]
except socket.timeout:
pass
finally:
send_socket.close()
recv_socket.close()
if curr_addr:
print(f"{ttl}\t{curr_addr}")
if curr_addr == dest_addr:
print("Trace complete.")
break
else:
print(f"{ttl}\t*")
if __name__ == "__main__":
traceroute("8.8.8.8")