-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute_gui.py
60 lines (47 loc) · 1.53 KB
/
traceroute_gui.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
#!/usr/bin/env python3
# coding=utf-8
import os
import platform
import re
import subprocess
from tkinter import *
from tkinter.messagebox import *
from traceroute import traceroute
if platform.system() != 'Windows':
if os.geteuid() != 0:
print('root permission needed')
exit(1)
else:
print('STOP: This program is incompatible with Microsoft Windows.')
exit(1)
ttl = 1
root = Tk()
root.geometry('{}x{}'.format(470, 400))
dest_ip_label = Label(root, text='ip address')
dest_ip_label.grid(row=0, column=0, sticky='W')
dest_ip_entry = Entry(root)
dest_ip_entry.grid(row=0, column=1)
result_text = Text(root, width=65)
result_text.grid(row=1, columnspan=4)
next_svar = StringVar(value='traceroute')
def go():
global ttl
ip_pattern = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
if re.match(ip_pattern, dest_ip_entry.get()) is None:
showerror(title='error', message='invaild ip address')
else:
r = traceroute(dest_ip_entry.get(), ttl)
if ttl == 1:
result_text.delete(1.0, END)
result_text.insert(END, 'ttl\taddress\t\ttype\tcode\t1\t2\t3\n')
result_text.insert(END, r)
if '\t3\t3' in r:
ttl = 1
next_svar.set('traceroute')
showinfo(title='done', message='traceroute finished')
else:
ttl += 1
next_svar.set('next')
next_button = Button(root, textvariable=next_svar, command=go)
next_button.grid(row=0, column=2)
root.mainloop()