-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracert.py
44 lines (32 loc) · 867 Bytes
/
tracert.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
import sys
from time import time
from scapy.layers.inet import IP, ICMP
from scapy.sendrecv import sr1
def tracert(dst):
p = IP(dst=dst) / ICMP(seq=1, id=1)
finish = False
i = 1
print()
while not finish:
print(str(i).ljust(5), end=' ')
p[IP].ttl = i
a = time()
ans = sr1(p, timeout=3, verbose=False)
b = time()
if ans is not None:
print(ans[IP].src.ljust(15), end='\t')
print('RTT ', str(round((b - a) * (10 ** 3), 3)) + 'ms')
finish = ans[ICMP].type == 0
else:
print("Unknown".ljust(15), "\tTimeout")
i += 1
print('\nTrace complete.')
def main():
try:
tracert(sys.argv[1])
except OSError:
print(
'illegal arguments, or no connection'
)
if __name__ == "__main__":
main()