forked from SSB-054/pye
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtraceroute.py
More file actions
60 lines (43 loc) · 1.42 KB
/
traceroute.py
File metadata and controls
60 lines (43 loc) · 1.42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
import ping
import socket
import time
def main():
ttl = 1
tries = 3
arrived = False
ipfound = False
# create socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
# take Input
addr = input("Enter Domain Name : ") or "www.sustc.edu.cn"
print('traceroute {0} ({1})'.format(addr, socket.gethostbyname(addr)))
print("No. Address rtts")
while not arrived:
s.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
for i in range(0, tries):
# Request sent
ID = ping.single_ping_request(s, addr)
rtt, reply, icmp_reply = ping.catch_ping_reply(s, ID, time.time())
#print(rtt)
if reply:
reply['length'] = reply['Total Length'] - 20 # sub header
if i == 0:
print(str(ttl) , end=" ")
if reply == None:
print('* ', end=" ")
continue
if socket.gethostbyname(addr) == reply["Source Address"]:
#print("arrived")
arrived = True
if i == 0 or ipfound == False:
print(str(reply["Source Address"]), end=" ")
ipfound = True
print(('{0:.2f} ms '.format(rtt*1000)), end=" ")
print("")
ttl += 1
ipfound = False
# close socket
s.close()
return
if __name__ == '__main__':
main()