-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtr.py
77 lines (58 loc) · 2.09 KB
/
tr.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
76
77
#!/usr/bin/python
import poetry
import socket
import argparse
import time
import pygeoip
import random
gi = pygeoip.GeoIP('geo/GeoLiteCity.dat')
my_quatren = poetry.numerated_quatrens[random.choice(range(len(poetry.numerated_quatrens)))]
def main():
parser = argparse.ArgumentParser(description='traceroute')
parser.add_argument('-host', help = 'specify hostname', type = str, dest = 'host')
args = parser.parse_args()
host = args.host
if host == None:
print 'Specify hostname'
exit(0)
traceroute(host)
def traceroute(host):
roundtrips = []
query = 3
ttl = 1
while True:
listen = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
while query != 0:
conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
conn.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
conn.connect((socket.gethostbyname(host), 33434))
conn.send('happy')
conn.close()
send_time = time.time()
address = listen.recvfrom(4096)[1][0]
recv_time = time.time()
roundtrip = str(round((recv_time - send_time) * 1000, 3)) + ' ms'
roundtrips.append(roundtrip)
query -= 1
if address != socket.gethostbyname(host):
rec = gi.record_by_name(address)
if rec:
city = rec['city']
country = rec['country_name']
else:
city = 'unknown city'
country = 'unknown country'
print ttl, address, city, country, roundtrips
elif address == socket.gethostbyname(host):
print str(ttl) + ' Arrived to ' + host
print ''
for line in my_quatren:
print line
print ''
break
listen.close()
ttl += 1
query = 3
roundtrips = []
if __name__ == '__main__':
main()