-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute.py
executable file
·75 lines (53 loc) · 1.81 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# A Traceroute Implementation
from sys import stdout
from time import sleep
from struct import pack
from random import randrange
from socket import AF_INET, IPPROTO_ICMP, IPPROTO_UDP, IP_TTL, SO_RCVTIMEO
from socket import SOCK_RAW, SOCK_DGRAM, SOL_IP, SOL_SOCKET, gethostbyname, socket
class Traceroute:
def __init__(self, destination='www.ashenm.ml', file=stdout, callback=lambda: None):
self.COGNITO = '*'
self.SEPERATOR = '\n'
self.DESTINATION = gethostbyname(destination)
self.CALLBACK = callback
self.OUTPUT = file
def __cast(self, hop, host):
if self.OUTPUT is stdout:
print(f'{str(hop).rjust(2)} {host}', file=self.OUTPUT, flush=True)
return
self.OUTPUT.write(bytes(host + self.SEPERATOR, 'ascii'))
def trace(self, ceiling=30, timeout=2500):
port = randrange(33434, 33534)
timeout = pack('ll', timeout // 1000, timeout % 1000)
for hop in range(1, ceiling + 1):
host = None
receiver = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
sender = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
sender.setsockopt(SOL_IP, IP_TTL, hop)
receiver.setsockopt(SOL_SOCKET, SO_RCVTIMEO, timeout)
receiver.bind(('', port))
sender.sendto(b'', (self.DESTINATION, port))
try:
data, (host, _) = receiver.recvfrom(32)
except:
self.__cast(hop, self.COGNITO)
else:
self.__cast(hop, host)
finally:
receiver.close()
sender.close()
if host == self.DESTINATION:
self.CALLBACK()
break;
sleep(0.25)
return self
if __name__ == '__main__':
from os import getuid
from signal import SIGINT, signal
if getuid() != 0:
print('Missing Administrative Access!')
exit(2)
signal(SIGINT, lambda signum, frame: exit(130))
Traceroute().trace()