-
Notifications
You must be signed in to change notification settings - Fork 2
/
iptool.py
1229 lines (1168 loc) · 75.5 KB
/
iptool.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# coding=utf-8
# module name: iptool
# author: Cof-Lee <[email protected]>
# this module uses the GPL-3.0 open source protocol
# update: 2024-11-23
"""
pyinstaller打包为.exe程序:
cmd> cd 项目名称/venv/Scripts
cmd> pyinstaller.exe ../../iptool.py -F -w -n iptool-v241123.exe
"""
import time
import tkinter
from tkinter import messagebox
from tkinter import font
import threading
import ctypes
import cofnet
import cofping
PAGE_IPV4 = 0
PAGE_IPV6 = 1
PAGE_PING = 2
def stop_thread_silently(thread):
"""
结束线程,如果线程里有time.sleep(n)之类的操作,则需要等待这个时长之后,才会结束此线程
即此方法无法立即结束sleep及其他阻塞函数导致的休眼线程,得等线程获得响应时才结束它
本函数不会抛出异常
"""
if thread is None:
print("iptool.stop_thread_silently: thread obj is None")
return
thread_id = ctypes.c_long(thread.ident)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(SystemExit))
# 正常结束线程时会返回数值1
if res == 0:
print("iptool.stop_thread_silently: invalid thread id")
elif res == 1:
print("iptool.stop_thread_silently: thread stopped")
else:
# 如果返回的值不为0,也不为1,则:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, None)
print("iptool.stop_thread_silently: PyThreadState_SetAsyncExc failed")
class MainWindow:
def __init__(self, width=800, height=480, title=''):
self.about_info_list = ["ipTool,开源的ip计算工具",
"版本: v241123",
"本软件使用GPL-v3.0协议开源",
"作者: 李茂福(Cof-Lee)",
"更新时间: 2024-11-23",
"开源地址: https://github.com/limaofu/iptool"]
self.title = title # 主程序标题
self.width = width # 主程序界面宽度(单位:像素)
self.height = height # 主程序界面高度(单位:像素)
self.resizable = False # False 表示宽度和高度不可由用户手动调整
self.minsize = (480, 320) # 主程序界面最小尺寸
self.maxsize = (1920, 1080) # 主程序界面最大尺寸
self.background = "#3A3A3A" # 主窗口背景色,RGB
self.window_obj = None # 在 MainWindow.show()里创建
self.menu_bar = None # 在 MainWindow.create_menu_bar_init()里创建
self.frame_nav = None # 导航栏frame
self.frame_nav_height = 30 # 导航栏frame高度(单位:像素)
self.frame_nav_button_width = 12 # 导航栏frame里的button宽度(单位:字符)
self.frame_main_func = None # 功能界面主frame
self.frame_main_func_height = self.height - self.frame_nav_height # 功能界面主frame高度(单位:像素)
self.frame_main_func_ipv4_page = None # 功能界面主frame里的ipv4窗口
self.frame_main_func_ipv6_page = None # 功能界面主frame里的ipv6窗口
self.frame_main_func_ping_page = None # 功能界面主frame里的ping窗口
self.screen_width = 0 # 在 MainWindow.show()里赋值
self.screen_height = 0 # 在 MainWindow.show()里赋值
self.padx = 2
self.pady = 2
self.view_width = 20
self.text_font = None # 全局字体
self.font_family = "" # 全局字体
self.font_size = 18 # 全局字体大小
self.widget_dict_ipv4 = {} # ipv4界面的控制对象字典
self.widget_dict_ipv6 = {} # ipv6界面的控制对象字典
self.widget_dict_ping = {} # ping界面的控制对象字典
self.bottom_frame_of_ping_page_widget_dict = {}
self.current_netseg = "0.0.0.0"
self.current_maskint = 32
self.is_calculated = False # 是否已计算过,ipv4
self.is_calculated6 = False # 是否已计算过,ipv6
self.current_ipv6_prefix_cidrv6 = "::/128"
self.current_ipv6_prefix_len = 128
self.detect_count_default = 3
self.detect_count_min = 1
self.detect_count_max = 1000000
self.detect_interval_default = 1 # 单位:秒
self.detect_interval_min = 0
self.detect_interval_max = 120
self.detect_timeout_default = 2 # 单位:秒
self.detect_timeout_min = 1
self.detect_timeout_max = 120
self.detect_pkg_size_default = 1 # 单位:字节
self.detect_pkg_size_min = 1
self.detect_pkg_size_max = 65535
self.detect_ip_ttl_default = 128
self.detect_ip_ttl_min = 0
self.detect_ip_ttl_max = 255
self.detect_ip_dont_frag_default = False # False表示允许分片
self.thread_start_ping_detect_list = []
self.is_quit = False # False表示未退出主程序
self.is_stopped_all_ping_detect = False # False表示未停止检测
self.counter_stopped_all_ping_detect = 0 # 每按一次“全部停止”键,此计数器就加1
self.target_ip_list = [] # 要检测的ipv4地址
self.target_ipv6_list = [] # 要检测的ipv6地址
self.current_ping_detect_obj_list = []
def show(self):
self.window_obj = tkinter.Tk() # ★★★创建主窗口对象★★★
self.screen_width = self.window_obj.winfo_screenwidth()
self.screen_height = self.window_obj.winfo_screenheight()
self.window_obj.title(self.title) # 设置窗口标题
# self.window_obj.iconbitmap(bitmap="D:\\test.ico") # 设置窗口图标,默认为羽毛图标
win_pos_x = self.screen_width // 2 - self.width // 2
win_pos_y = self.screen_height // 2 - self.height // 2
win_pos = f"{self.width}x{self.height}+{win_pos_x}+{win_pos_y}"
self.window_obj.geometry(win_pos) # 设置窗口大小及位置,居中
self.window_obj.resizable(width=self.resizable, height=self.resizable) # True 表示宽度和高度可由用户手动调整
self.window_obj.minsize(*self.minsize) # 可调整的最小宽度及高度
self.window_obj.maxsize(*self.maxsize) # 可调整的最大宽度及高度
self.window_obj.pack_propagate(True) # True表示窗口内的控件大小自适应
self.window_obj.configure(bg=self.background) # 设置主窗口背景色,RGB
# 加载初始化界面控件
self.load_main_window_init_widget() # ★★★ 接下来,所有的事情都在此界面操作 ★★★
# 主窗口点击右上角的关闭按钮后,触发此函数
self.window_obj.protocol("WM_DELETE_WINDOW", self.on_closing_main_window)
# 运行窗口主循环
self.window_obj.mainloop()
def load_main_window_init_widget(self):
"""
加载程序初始化界面控件
"""
# 首先清空主window
self.clear_tkinter_widget(self.window_obj)
# 设置全局字体
self.text_font = font.Font(size=self.font_size, family=self.font_family)
# 加载菜单栏
self.create_menu_bar_init()
# 加载导航栏
self.create_frame_nav()
# 添加功能窗口(默认为ipv4界面)
self.create_frame_main_func()
def create_frame_nav(self):
# 创建导航栏frame
self.frame_nav = tkinter.Frame(self.window_obj, bg="green", borderwidth=1, width=self.width, height=self.frame_nav_height,
relief='groove')
self.frame_nav.grid_propagate(False)
self.frame_nav.pack_propagate(False)
self.frame_nav.grid(row=0, column=0)
# ipv4-界面按钮
menu_button_ipv4 = tkinter.Button(self.frame_nav, text="IPv4计算", width=self.frame_nav_button_width, height=1, bg="white",
command=self.frame_main_func_ipv4_page_display)
menu_button_ipv4.pack(side=tkinter.LEFT, padx=self.padx)
# ipv6-界面按钮
menu_button_ipv6 = tkinter.Button(self.frame_nav, text="IPv6计算", width=self.frame_nav_button_width, height=1,
bg="white",
command=self.frame_main_func_ipv6_page_display)
menu_button_ipv6.pack(side=tkinter.LEFT, padx=self.padx)
# ping-界面按钮
menu_button_ping = tkinter.Button(self.frame_nav, text="Ping检测", width=self.frame_nav_button_width, height=1, bg="white",
command=self.frame_main_func_ping_page_display)
menu_button_ping.pack(side=tkinter.LEFT, padx=self.padx)
def create_frame_main_func(self):
# 创建功能界面主frame
self.frame_main_func = tkinter.Frame(self.window_obj, bg="pink", borderwidth=0, width=self.width,
height=self.frame_main_func_height)
self.frame_main_func.grid_propagate(False)
self.frame_main_func.pack_propagate(False)
self.frame_main_func.grid(row=1, column=0)
# 创建3个功能界面子frame
self.frame_main_func_ipv4_page = tkinter.Frame(self.frame_main_func, bg="#222222", borderwidth=0, width=self.width,
height=self.frame_main_func_height)
self.frame_main_func_ipv6_page = tkinter.Frame(self.frame_main_func, bg="#333333", borderwidth=0, width=self.width,
height=self.frame_main_func_height)
self.frame_main_func_ping_page = tkinter.Frame(self.frame_main_func, bg="#444444", borderwidth=0, width=self.width,
height=self.frame_main_func_height)
# 初始化3个功能界面
self.init_ipv4_page()
self.init_ipv6_page()
self.init_ping_page()
# 首次打开程序,显示的是ipv4界面
widget_index = 0
for widget in self.frame_nav.winfo_children():
if widget_index == PAGE_IPV4:
widget.config(bg="pink")
else:
widget.config(bg="white")
widget_index += 1
self.frame_main_func_ipv4_page.place(x=0, y=0, width=self.width, height=self.frame_main_func_height)
self.widget_dict_ipv4["entry_input_ip"].focus_force()
def frame_main_func_ipv4_page_display(self):
# 更新导航框架 self.frame_nav 的当前选项卡背景色
widget_index = 0
for widget in self.frame_nav.winfo_children():
if widget_index == PAGE_IPV4:
widget.config(bg="pink")
else:
widget.config(bg="white")
widget_index += 1
# 显示ipv4页面
self.frame_main_func_ipv6_page.place_forget()
self.frame_main_func_ping_page.place_forget()
self.frame_main_func_ipv4_page.place(x=0, y=0, width=self.width, height=self.frame_main_func_height)
self.widget_dict_ipv4["entry_input_ip"].focus_force()
def frame_main_func_ipv6_page_display(self):
# 更新导航框架 self.frame_nav 的当前选项卡背景色
widget_index = 0
for widget in self.frame_nav.winfo_children():
if widget_index == PAGE_IPV6:
widget.config(bg="pink")
else:
widget.config(bg="white")
widget_index += 1
# 显示ipv6页面
self.frame_main_func_ipv4_page.place_forget()
self.frame_main_func_ping_page.place_forget()
self.frame_main_func_ipv6_page.place(x=0, y=0, width=self.width, height=self.frame_main_func_height)
self.widget_dict_ipv6["entry_input_ipv6"].focus_force()
def frame_main_func_ping_page_display(self):
# 更新导航框架 self.frame_nav 的当前选项卡背景色
widget_index = 0
for widget in self.frame_nav.winfo_children():
if widget_index == PAGE_PING:
widget.config(bg="pink")
else:
widget.config(bg="white")
widget_index += 1
# 显示ping页面
self.frame_main_func_ipv4_page.place_forget()
self.frame_main_func_ipv6_page.place_forget()
self.frame_main_func_ping_page.place(x=0, y=0, width=self.width, height=self.frame_main_func_height)
self.widget_dict_ping["text_input_ip"].focus_force()
def init_ipv4_page(self):
# 添加控件
# 输入ip信息
label_input_ip = tkinter.Label(self.frame_main_func_ipv4_page, text="输入ip信息★:") # ip信息为【必填】
label_input_ip.grid(row=0, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["sv_input_ip"] = tkinter.StringVar()
self.widget_dict_ipv4["entry_input_ip"] = tkinter.Entry(self.frame_main_func_ipv4_page,
textvariable=self.widget_dict_ipv4["sv_input_ip"], width=48,
bg="#e2deff")
self.widget_dict_ipv4["entry_input_ip"].grid(row=0, column=1, columnspan=2, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["entry_input_ip"].bind("<KeyPress>", self.front_end_input_func_printable_char_ipv4) # 监听键盘输入的字符
# 掩码位数
label_netmask_int = tkinter.Label(self.frame_main_func_ipv4_page, text="掩码位数:") # 掩码位数为【选填】,在点击“计算”按钮后,会自动调到匹配值
label_netmask_int.grid(row=1, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["sv_netmask_int"] = tkinter.StringVar()
self.widget_dict_ipv4["spinbox_netmask_int"] = tkinter.Spinbox(self.frame_main_func_ipv4_page, from_=0, to=32, increment=1,
textvariable=self.widget_dict_ipv4["sv_netmask_int"],
width=3, command=self.set_netmask_scale_on_spinbox_change)
self.widget_dict_ipv4["spinbox_netmask_int"].grid(row=1, column=1, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["netmask_scale"] = tkinter.Scale(self.frame_main_func_ipv4_page, from_=0, to=32, resolution=1,
orient="horizontal",
showvalue=False, length=200, sliderlength=50)
self.widget_dict_ipv4["netmask_scale"].configure(command=self.set_sv_netmask_int)
self.widget_dict_ipv4["netmask_scale"].grid(row=1, column=2, padx=self.padx, pady=self.pady)
# 功能按钮组
calc_btn_frame = tkinter.Frame(self.frame_main_func_ipv4_page, width=self.width - 25, height=30, bg=self.background)
calc_btn_frame.grid(row=2, column=0, columnspan=3)
button_calculate = tkinter.Button(calc_btn_frame, text="计算", command=self.calculate)
button_calculate.pack(side=tkinter.LEFT, padx=self.padx)
button_last_netseg = tkinter.Button(calc_btn_frame, text="↑上一网段", command=self.calculate_last_netseg)
button_last_netseg.pack(side=tkinter.LEFT, padx=self.padx)
button_next_netseg = tkinter.Button(calc_btn_frame, text="↓下一网段", command=self.calculate_next_netseg)
button_next_netseg.pack(side=tkinter.LEFT, padx=self.padx)
button_clear = tkinter.Button(calc_btn_frame, text="清空", command=self.clear)
button_clear.pack(side=tkinter.LEFT, padx=self.padx)
button_exit = tkinter.Button(calc_btn_frame, text="退出", command=self.on_closing_main_window)
button_exit.pack(side=tkinter.LEFT, padx=self.padx)
# ip基础信息显示文本框
self.widget_dict_ipv4["text_ip_base_info"] = tkinter.Text(self.frame_main_func_ipv4_page, width=64, height=7,
font=self.text_font, bg="black", fg="white")
self.widget_dict_ipv4["text_ip_base_info"].grid(row=3, column=0, columnspan=3, padx=self.padx, pady=self.pady)
# ip同网段信息显示文本框
label_other_hostseg = tkinter.Label(self.frame_main_func_ipv4_page, text="本网段其他主机ip:")
label_other_hostseg.grid(row=4, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["scrollbar_other_hostseg"] = tkinter.Scrollbar(self.frame_main_func_ipv4_page)
self.widget_dict_ipv4["text_other_hostseg"] = tkinter.Text(self.frame_main_func_ipv4_page, width=64, height=9,
font=self.text_font, bg="black", fg="white",
yscrollcommand=self.widget_dict_ipv4["scrollbar_other_hostseg"].set)
self.widget_dict_ipv4["text_other_hostseg"].grid(row=5, column=0, columnspan=3, padx=self.padx, pady=self.pady)
self.widget_dict_ipv4["scrollbar_other_hostseg"].config(command=self.widget_dict_ipv4["text_other_hostseg"].yview)
self.widget_dict_ipv4["scrollbar_other_hostseg"].grid(row=5, column=3, padx=self.padx, pady=self.pady, sticky="NS")
# 设置Text文本框的前景色tag_config
self.widget_dict_ipv4["text_ip_base_info"].tag_config("ip_address_fg", foreground="#deef5a")
self.widget_dict_ipv4["text_ip_base_info"].tag_config("maskint_fg", foreground="green")
self.widget_dict_ipv4["text_ip_base_info"].tag_config("maskbyte_fg", foreground="#0d64c0")
self.widget_dict_ipv4["text_ip_base_info"].tag_config("hostseg_fg", foreground="pink")
self.widget_dict_ipv4["text_ip_base_info"].tag_config("hostseg_num_fg", foreground="red")
self.widget_dict_ipv4["text_other_hostseg"].tag_config("ip_address_fg", foreground="#deef5a")
def init_ipv6_page(self):
# 添加控件
# 输入ipv6信息
label_input_ip = tkinter.Label(self.frame_main_func_ipv6_page, text="输入ipv6信息★:") # ip信息为【必填】
label_input_ip.grid(row=0, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ipv6["sv_input_ipv6"] = tkinter.StringVar()
self.widget_dict_ipv6["entry_input_ipv6"] = tkinter.Entry(self.frame_main_func_ipv6_page,
textvariable=self.widget_dict_ipv6["sv_input_ipv6"], width=48,
bg="#e2deff")
self.widget_dict_ipv6["entry_input_ipv6"].grid(row=0, column=1, columnspan=2, padx=self.padx, pady=self.pady)
self.widget_dict_ipv6["entry_input_ipv6"].bind("<KeyPress>", self.front_end_input_func_printable_char_ipv6) # 监听键盘输入的字符
# 掩码位数
label_netmask_int = tkinter.Label(self.frame_main_func_ipv6_page, text="地址前缀长度:") # 【选填】,在点击“计算”按钮后,会自动调到匹配值
label_netmask_int.grid(row=1, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ipv6["sv_ipv6_prefix_len_int"] = tkinter.StringVar()
self.widget_dict_ipv6["spinbox_ipv6_prefix_len_int"] = tkinter.Spinbox(self.frame_main_func_ipv6_page, from_=0, to=128, increment=1,
textvariable=self.widget_dict_ipv6["sv_ipv6_prefix_len_int"],
width=3,
command=self.set_netmask_scale_on_spinbox_change_ipv6)
self.widget_dict_ipv6["spinbox_ipv6_prefix_len_int"].grid(row=1, column=1, padx=self.padx, pady=self.pady)
self.widget_dict_ipv6["ipv6_prefix_len_scale"] = tkinter.Scale(self.frame_main_func_ipv6_page, from_=0, to=128, resolution=1,
orient="horizontal",
showvalue=False, length=200, sliderlength=50)
self.widget_dict_ipv6["ipv6_prefix_len_scale"].configure(command=self.set_sv_netmask_int_ipv6)
self.widget_dict_ipv6["ipv6_prefix_len_scale"].grid(row=1, column=2, padx=self.padx, pady=self.pady)
# 功能按钮组
calc_btn_frame = tkinter.Frame(self.frame_main_func_ipv6_page, width=self.width - 25, height=30, bg=self.background)
calc_btn_frame.grid(row=2, column=0, columnspan=3)
button_calculate = tkinter.Button(calc_btn_frame, text="计算", command=self.calculate6)
button_calculate.pack(side=tkinter.LEFT, padx=self.padx)
# button_last_netseg = tkinter.Button(calc_btn_frame, text="↑上一地址块", command=self.calculate_last_cidrv6)
# button_last_netseg.pack(side=tkinter.LEFT, padx=self.padx)
# button_next_netseg = tkinter.Button(calc_btn_frame, text="↓下一地址块", command=self.calculate_next_cidrv6)
# button_next_netseg.pack(side=tkinter.LEFT, padx=self.padx)
button_clear = tkinter.Button(calc_btn_frame, text="清空", command=self.clear6)
button_clear.pack(side=tkinter.LEFT, padx=self.padx)
button_exit = tkinter.Button(calc_btn_frame, text="退出", command=self.on_closing_main_window)
button_exit.pack(side=tkinter.LEFT, padx=self.padx)
# ip基础信息显示文本框
self.widget_dict_ipv6["text_ipv6_base_info"] = tkinter.Text(self.frame_main_func_ipv6_page, width=64, height=12,
font=self.text_font, bg="black", fg="white")
self.widget_dict_ipv6["text_ipv6_base_info"].grid(row=3, column=0, columnspan=3, padx=self.padx, pady=self.pady)
# 设置Text文本框的前景色tag_config
self.widget_dict_ipv6["text_ipv6_base_info"].tag_config("ipv6_address_fg", foreground="#deef5a")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_config("ipv6_prefix_fg", foreground="green")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_config("maskbyte_fg", foreground="#0d64c0")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_config("hostseg_fg", foreground="pink")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_config("hostseg_num_fg", foreground="red")
def init_ping_page(self):
top_frame_height = 130
bottom_frame_height = self.frame_main_func_height - top_frame_height
top_frame = tkinter.Frame(self.frame_main_func_ping_page, bg="#71a861", borderwidth=0, width=self.width,
height=top_frame_height, relief='groove')
top_frame.grid_propagate(False)
top_frame.pack_propagate(False)
top_frame.grid(row=0, column=0)
bottom_frame = tkinter.Frame(self.frame_main_func_ping_page, bg="black", borderwidth=0, width=self.width,
height=bottom_frame_height, relief='groove')
bottom_frame.grid_propagate(False)
bottom_frame.pack_propagate(False)
bottom_frame.grid(row=1, column=0)
# 在 top_frame 添加控件
label_input_ip = tkinter.Label(top_frame, text="输入目标ip(1行1个ip):") # ip信息为【必填】
label_input_ip.grid(row=0, column=0, padx=self.padx, pady=self.pady)
self.widget_dict_ping["text_input_ip"] = tkinter.Text(top_frame, width=60, height=5)
self.widget_dict_ping["text_input_ip"].grid(row=0, column=1, padx=self.padx, pady=self.pady)
button_start_ping = tkinter.Button(top_frame, text="开始检测", command=self.start_ping) # ★★开始检测
button_start_ping.grid(row=0, column=2, padx=self.padx, pady=self.pady)
# ping参数设置
parameter_frame = tkinter.Frame(top_frame, width=self.width - 25, height=30, bg=self.background)
parameter_frame.grid(row=2, column=0, columnspan=3)
ctrl_frame = tkinter.Frame(top_frame, width=self.width - 25, height=30, bg=self.background)
ctrl_frame.grid(row=3, column=0, columnspan=3)
# 在 parameter_frame 添加参数输入控件
label_count = tkinter.Label(parameter_frame, text="发包数:")
label_count.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["sv_count"] = tkinter.StringVar()
self.widget_dict_ping["sv_count"].set(self.detect_count_default)
self.widget_dict_ping["spinbox_count"] = tkinter.Spinbox(parameter_frame, from_=self.detect_count_min, to=self.detect_count_max,
increment=1,
textvariable=self.widget_dict_ping["sv_count"],
width=7, bg="#e2deff")
self.widget_dict_ping["spinbox_count"].pack(side=tkinter.LEFT, padx=self.padx)
label_interval = tkinter.Label(parameter_frame, text="发包间隔(s):")
label_interval.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["sv_interval"] = tkinter.StringVar()
self.widget_dict_ping["sv_interval"].set(self.detect_interval_default)
self.widget_dict_ping["spinbox_interval"] = tkinter.Spinbox(parameter_frame, from_=self.detect_interval_min,
to=self.detect_interval_max, increment=1,
textvariable=self.widget_dict_ping["sv_interval"],
width=3, bg="#e2deff")
self.widget_dict_ping["spinbox_interval"].pack(side=tkinter.LEFT, padx=self.padx)
label_timeout = tkinter.Label(parameter_frame, text="超时(s):")
label_timeout.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["sv_timeout"] = tkinter.StringVar()
self.widget_dict_ping["sv_timeout"].set(self.detect_timeout_default)
self.widget_dict_ping["spinbox_timeout"] = tkinter.Spinbox(parameter_frame, from_=self.detect_timeout_min,
to=self.detect_timeout_max, increment=1,
textvariable=self.widget_dict_ping["sv_timeout"],
width=3, bg="#e2deff")
self.widget_dict_ping["spinbox_timeout"].pack(side=tkinter.LEFT, padx=self.padx)
label_size = tkinter.Label(parameter_frame, text="数据大小(byte):")
label_size.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["sv_size"] = tkinter.StringVar()
self.widget_dict_ping["sv_size"].set(self.detect_pkg_size_default)
self.widget_dict_ping["spinbox_size"] = tkinter.Spinbox(parameter_frame, from_=self.detect_pkg_size_min,
to=self.detect_pkg_size_max, increment=1,
textvariable=self.widget_dict_ping["sv_size"],
width=5, bg="#e2deff")
self.widget_dict_ping["spinbox_size"].pack(side=tkinter.LEFT, padx=self.padx)
label_ttl = tkinter.Label(parameter_frame, text="TTL:")
label_ttl.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["sv_ttl"] = tkinter.StringVar()
self.widget_dict_ping["sv_ttl"].set(self.detect_ip_ttl_default)
self.widget_dict_ping["spinbox_ttl"] = tkinter.Spinbox(parameter_frame, from_=self.detect_ip_ttl_min,
to=self.detect_ip_ttl_max, increment=1,
textvariable=self.widget_dict_ping["sv_ttl"],
width=5, bg="#e2deff")
self.widget_dict_ping["spinbox_ttl"].pack(side=tkinter.LEFT, padx=self.padx)
label_dont_frag = tkinter.Label(parameter_frame, text="报文不分片:")
label_dont_frag.pack(side=tkinter.LEFT, padx=self.padx)
self.widget_dict_ping["bool_dont_frag"] = tkinter.BooleanVar()
self.widget_dict_ping["bool_dont_frag"].set(self.detect_ip_dont_frag_default) # 默认是False,不勾选
self.widget_dict_ping["check_btn_dont_frag"] = tkinter.Checkbutton(parameter_frame, text=" ",
variable=self.widget_dict_ping["bool_dont_frag"])
self.widget_dict_ping["check_btn_dont_frag"].pack(side=tkinter.LEFT, padx=self.padx)
# 功能按钮
button_clear = tkinter.Button(parameter_frame, text="重置参数", command=self.reset_ping_parameter)
button_clear.pack(side=tkinter.LEFT, padx=self.padx)
button_stop_all = tkinter.Button(ctrl_frame, text="全部停止", bg="#c94f4f", command=self.stop_ping_detect)
button_stop_all.pack(side=tkinter.LEFT, padx=self.padx)
button_restart_all = tkinter.Button(ctrl_frame, text="全部重新开始", bg="#7cb2f2", command=self.restart_ping_detect)
button_restart_all.pack(side=tkinter.LEFT, padx=self.padx)
button_clear = tkinter.Button(ctrl_frame, text="移除所有检测对项", command=self.clear_ping_target)
button_clear.pack(side=tkinter.LEFT, padx=self.padx)
button_exit = tkinter.Button(ctrl_frame, text="退出", command=self.on_closing_main_window)
button_exit.pack(side=tkinter.LEFT, padx=self.padx)
# 在 bottom_frame 添加输出信息控件,添加canvas-frame滚动框
self.bottom_frame_of_ping_page_widget_dict["scrollbar_normal"] = tkinter.Scrollbar(bottom_frame, width=15)
self.bottom_frame_of_ping_page_widget_dict["scrollbar_normal"].pack(side=tkinter.RIGHT, fill=tkinter.Y)
self.bottom_frame_of_ping_page_widget_dict["canvas"] = tkinter.Canvas(bottom_frame, bg="#dddddd",
yscrollcommand=self.bottom_frame_of_ping_page_widget_dict[
"scrollbar_normal"].set)
self.bottom_frame_of_ping_page_widget_dict["canvas"].place(x=0, y=0, width=int(self.width - 20),
height=bottom_frame_height)
self.bottom_frame_of_ping_page_widget_dict["scrollbar_normal"].config(
command=self.bottom_frame_of_ping_page_widget_dict["canvas"].yview)
self.bottom_frame_of_ping_page_widget_dict["frame"] = tkinter.Frame(self.bottom_frame_of_ping_page_widget_dict["canvas"],
bg="#dddddd")
self.bottom_frame_of_ping_page_widget_dict["frame"].pack(fill=tkinter.X, expand=tkinter.TRUE)
self.bottom_frame_of_ping_page_widget_dict["canvas"].create_window((0, 0),
window=self.bottom_frame_of_ping_page_widget_dict["frame"],
anchor='nw')
# self.update_canvas_of_bottom_frame_of_ping_page()
def proces_mouse_scroll_of_bottom_frame_of_ping_page(self, event):
if event.delta > 0:
self.bottom_frame_of_ping_page_widget_dict["canvas"].yview_scroll(-1, 'units') # 向上移动
else:
self.bottom_frame_of_ping_page_widget_dict["canvas"].yview_scroll(1, 'units') # 向下移动
def update_canvas_of_bottom_frame_of_ping_page(self):
# 更新Frame的尺寸
self.bottom_frame_of_ping_page_widget_dict["frame"].update_idletasks()
self.bottom_frame_of_ping_page_widget_dict["canvas"].configure(
scrollregion=(0, 0, self.bottom_frame_of_ping_page_widget_dict["frame"].winfo_width(),
self.bottom_frame_of_ping_page_widget_dict["frame"].winfo_height()))
self.bottom_frame_of_ping_page_widget_dict["canvas"].bind("<MouseWheel>", self.proces_mouse_scroll_of_bottom_frame_of_ping_page)
self.bottom_frame_of_ping_page_widget_dict["frame"].bind("<MouseWheel>", self.proces_mouse_scroll_of_bottom_frame_of_ping_page)
# 滚动条移到最开头
self.bottom_frame_of_ping_page_widget_dict["canvas"].yview(tkinter.MOVETO, 0.0) # MOVETO表示移动到,0.0表示最开头
def front_end_input_func_printable_char_ipv4(self, event):
"""
处理普通可打印字符,控制键及组合按键
★★★ 按键,ascii字符,vt100控制符是3个不同的概念
按键可以对应一个字符,也可没有相应字符,
按下shift/ctrl等控制键后再按其他键,可能会产生换档字符(如按下shift加数字键2,产生字符@)
vt100控制符是由ESC(十六进制为\0x1b,八进制为\033)加其他可打印字符组成,比如:
按键↑(方向键Up)对应的vt100控制符为 ESC加字母OA,即b'\033OA'
★★★
"""
# print("普通字符输入如下:")
# print(event.keysym)
# print(event.keycode)
# 非可打印字符没有event.char,event.char为空,只有event.keycode
if event.keysym == "Return":
self.calculate()
def front_end_input_func_printable_char_ipv6(self, event):
# print("普通字符输入如下:")
# print(event.keysym)
# print(event.keycode)
# 非可打印字符没有event.char,event.char为空,只有event.keycode
if event.keysym == "Return":
self.calculate6()
def set_sv_netmask_int(self, netmask_int: int):
# print(type(netmask_int)) # <class 'str'>
self.widget_dict_ipv4["sv_netmask_int"].set(netmask_int)
self.calculate(maskint=str(netmask_int))
def set_sv_netmask_int_ipv6(self, netmask_int: int):
self.widget_dict_ipv6["sv_ipv6_prefix_len_int"].set(netmask_int)
self.calculate6(maskint=str(netmask_int))
def set_netmask_scale_on_spinbox_change(self):
netmask_int_str = self.widget_dict_ipv4["sv_netmask_int"].get()
self.widget_dict_ipv4["netmask_scale"].set(int(netmask_int_str))
self.calculate(maskint=netmask_int_str)
def set_netmask_scale_on_spinbox_change_ipv6(self):
netmask_int_str = self.widget_dict_ipv6["sv_ipv6_prefix_len_int"].get()
self.widget_dict_ipv6["ipv6_prefix_len_scale"].set(int(netmask_int_str))
self.calculate6(maskint=netmask_int_str)
def clear(self):
self.widget_dict_ipv4["sv_input_ip"].set("")
self.widget_dict_ipv4["sv_netmask_int"].set(0)
self.widget_dict_ipv4["netmask_scale"].set(0)
self.widget_dict_ipv4["text_ip_base_info"].delete("1.0", tkinter.END)
self.widget_dict_ipv4["text_other_hostseg"].delete("1.0", tkinter.END)
self.current_netseg = ""
self.current_maskint = 32
self.is_calculated = False
def clear6(self):
self.widget_dict_ipv6["sv_input_ipv6"].set("")
self.widget_dict_ipv6["sv_ipv6_prefix_len_int"].set(0)
self.widget_dict_ipv6["ipv6_prefix_len_scale"].set(0)
self.widget_dict_ipv6["text_ipv6_base_info"].delete("1.0", tkinter.END)
# self.current_netseg = ""
# self.current_maskint = 32
self.is_calculated6 = False
def stop_ping_detect(self):
self.is_stopped_all_ping_detect = True
self.counter_stopped_all_ping_detect += 1
def restart_ping_detect(self):
self.stop_ping_detect()
thread = threading.Thread(target=self.restart_ping_detect_xx)
thread.start()
def restart_ping_detect_xx(self):
time.sleep(2)
not_restarted_detect_obj_list = []
for detect_obj in self.current_ping_detect_obj_list:
if detect_obj.is_finished:
detect_obj.restart_this_job()
else:
not_restarted_detect_obj_list.append(detect_obj)
if len(not_restarted_detect_obj_list) > 0:
message_list = [obj.target_ip for obj in not_restarted_detect_obj_list]
messagebox.showinfo("提示", "以下目标检测进程未及时结束,无法重新检测:\n" + "\n".join(message_list) + "\n可单独重新检测以上对象")
def reset_ping_parameter(self):
self.widget_dict_ping["sv_count"].set(self.detect_count_default)
self.widget_dict_ping["sv_interval"].set(self.detect_interval_default)
self.widget_dict_ping["sv_timeout"].set(self.detect_timeout_default)
self.widget_dict_ping["sv_size"].set(self.detect_pkg_size_default)
self.widget_dict_ping["sv_ttl"].set(self.detect_ip_ttl_default)
self.widget_dict_ping["bool_dont_frag"].set(self.detect_ip_dont_frag_default)
def clear_ping_target(self):
self.is_stopped_all_ping_detect = True
self.counter_stopped_all_ping_detect += 1
for thread_ping_detect in self.thread_start_ping_detect_list:
stop_thread_silently(thread_ping_detect)
self.clear_tkinter_widget(self.bottom_frame_of_ping_page_widget_dict["frame"])
self.thread_start_ping_detect_list = []
self.current_ping_detect_obj_list = []
self.update_canvas_of_bottom_frame_of_ping_page()
def start_ping(self):
self.is_stopped_all_ping_detect = False
self.is_quit = False
try:
detect_count = int(self.widget_dict_ping["sv_count"].get())
except ValueError:
detect_count = self.detect_count_default
try:
detect_interval = int(self.widget_dict_ping["sv_interval"].get())
except ValueError:
detect_interval = self.detect_interval_default
try:
detect_timeout = int(self.widget_dict_ping["sv_timeout"].get())
except ValueError:
detect_timeout = self.detect_timeout_default
try:
detect_pkg_size = int(self.widget_dict_ping["sv_size"].get())
except ValueError:
detect_pkg_size = self.detect_pkg_size_default
try:
detect_ip_ttl = int(self.widget_dict_ping["sv_ttl"].get())
except ValueError:
detect_ip_ttl = self.detect_ip_ttl_default
dont_frag = self.widget_dict_ping["bool_dont_frag"].get()
if detect_count < self.detect_count_min:
detect_count = self.detect_count_min
if detect_count > self.detect_count_max:
detect_count = self.detect_count_max
self.widget_dict_ping["sv_count"].set(detect_count)
if detect_interval < self.detect_interval_min:
detect_interval = self.detect_interval_min
if detect_interval > self.detect_interval_max:
detect_interval = self.detect_interval_max
self.widget_dict_ping["sv_interval"].set(detect_interval)
if detect_timeout < self.detect_timeout_min:
detect_timeout = self.detect_timeout_min
if detect_timeout > self.detect_timeout_max:
detect_timeout = self.detect_timeout_max
self.widget_dict_ping["sv_timeout"].set(detect_timeout)
if detect_pkg_size < self.detect_pkg_size_min:
detect_pkg_size = self.detect_pkg_size_min
if detect_pkg_size > self.detect_pkg_size_max:
detect_pkg_size = self.detect_pkg_size_max
self.widget_dict_ping["sv_size"].set(detect_pkg_size)
if detect_ip_ttl < self.detect_ip_ttl_min:
detect_ip_ttl = self.detect_ip_ttl_min
if detect_ip_ttl > self.detect_ip_ttl_max:
detect_ip_ttl = self.detect_ip_ttl_max
self.widget_dict_ping["sv_ttl"].set(detect_ip_ttl)
target_ip_lines = self.widget_dict_ping["text_input_ip"].get("1.0", tkinter.END)
for target_ip in target_ip_lines.split("\n"):
target_ip_strip = target_ip.strip()
if cofnet.is_ip_addr(target_ip_strip):
self.target_ip_list.append(target_ip_strip)
ping_detect_item_info_obj = PingDetectItemInfo(top_frame=self.bottom_frame_of_ping_page_widget_dict["frame"],
width=self.width, target_ip=target_ip_strip, detect_count=detect_count,
detect_interval=detect_interval, detect_timeout=detect_timeout,
detect_pkg_size=detect_pkg_size, detect_ip_ttl=detect_ip_ttl,
dont_frag=dont_frag, main_window=self)
thread_start_ping_detect = threading.Thread(target=ping_detect_item_info_obj.show)
self.thread_start_ping_detect_list.append(thread_start_ping_detect)
ping_detect_item_info_obj.set_curent_ping_detect_thread_id(thread_start_ping_detect)
self.current_ping_detect_obj_list.append(ping_detect_item_info_obj)
thread_start_ping_detect.start()
elif cofnet.is_ipv6_addr(target_ip_strip):
self.target_ipv6_list.append(target_ip_strip)
else:
continue
self.update_canvas_of_bottom_frame_of_ping_page()
# 清空输入
self.widget_dict_ping["text_input_ip"].delete("1.0", tkinter.END)
def calculate(self, maskint=None):
# maskint如果要赋值,需要赋str类型的值
input_ip_str = self.widget_dict_ipv4["sv_input_ip"].get().strip()
if input_ip_str == "":
return
if cofnet.is_ip_addr(input_ip_str): # 输入信息为 纯ip,例如 "10.99.1.3"
self.calculate_ip(input_ip_str, maskint=maskint)
return
if cofnet.is_ip_with_maskint(input_ip_str): # 输入信息为 ip/掩码位数,例如 "10.99.1.3/24"
self.calculate_ip_maskint(input_ip_str, maskint=maskint)
return
if cofnet.is_ip_range(input_ip_str): # 输入信息为 ip-range,例如 "10.99.1.33-55"
self.calculate_ip_range(input_ip_str)
return
if cofnet.is_ip_range_2(input_ip_str): # 输入信息为 ip-range,例如 "10.99.1.33-10.99.1.55"
self.calculate_ip_range2(input_ip_str)
return
if cofnet.is_cidr(input_ip_str): # 输入信息为 cidr,例如 "10.99.1.0/24"
self.calculate_cidr(input_ip_str)
return
try:
ip_addr = cofnet.int32_to_ip(int(input_ip_str, base=16)) # 输入信息为十六进制数的ip地址
if cofnet.is_ip_addr(ip_addr): # 输入信息转为纯ip,例如 "10.99.1.3"
self.calculate_ip(ip_addr, maskint=maskint)
return
except ValueError:
return
print(f"您输入的ip地址信息格式不正确 {input_ip_str}")
def calculate6(self, maskint=None):
# maskint如果要赋值,需要赋str类型的值
input_ipv6_str = self.widget_dict_ipv6["sv_input_ipv6"].get().strip()
if input_ipv6_str == "":
return
if cofnet.is_ipv6_addr(input_ipv6_str): # 输入信息为 ipv6地址,例如 "FD00:1234::11"
self.calculate6_ipv6(input_ipv6_str, ipv6_prefix_len=maskint)
return
if cofnet.is_ipv6_with_prefix_len(input_ipv6_str): # 输入信息为 cidrv6,例如 "FD00::11/64"
self.calculate6_ipv6_with_prefix_len(input_ipv6_str, ipv6_prefix_len=maskint)
return
print(f"您输入的ipv6地址信息格式不正确 {input_ipv6_str}")
def calculate_ip(self, input_ip_str: str, maskint=None):
"""
输入信息为 纯ip,掩码位数
输入信息为 纯ip,子网掩码位数(可选)
maskint如果要赋值,需要赋str类型的值
"""
if maskint is None:
new_maskint = "32"
else:
if int(maskint) < 0 or int(maskint) > 32:
messagebox.showinfo("Error", "子网掩码应该在[0-32]区间内")
return
else:
new_maskint = maskint
self.widget_dict_ipv4["text_ip_base_info"].delete("1.0", tkinter.END)
# 开始计算
ip_hex_address = cofnet.ip_to_hex_string(input_ip_str)
ip_address = f"ip地址: {input_ip_str} ip地址十六进制表示: {ip_hex_address}\n" # 第 1 行
ip_int = cofnet.ip_or_maskbyte_to_int(input_ip_str)
ip_int_show = f"ip地址转为整数值: {ip_int}(十进制)\n" # 第 2 行
ip_binary_str = cofnet.ip_or_maskbyte_to_binary_with_space(input_ip_str)
ip_binary_show = f"ip地址二进制表示: {ip_binary_str}\n" # 第 3 行
maskbyte = cofnet.maskint_to_maskbyte(int(new_maskint))
netseg_hex_address = cofnet.ip_to_hex_string(maskbyte)
wildcard_mask = cofnet.maskint_to_wildcard_mask(int(new_maskint))
maskbyte_show = f"子网掩码: {maskbyte} ({netseg_hex_address}) 反掩码: {wildcard_mask}\n" # 第 4 行
ip_netseg = cofnet.get_netseg_byte(input_ip_str, new_maskint)
ip_hostseg = cofnet.get_hostseg_int(input_ip_str, new_maskint)
host_seg_num = cofnet.get_hostseg_num(int(new_maskint))
ip_netseg_hex_address = cofnet.ip_to_hex_string(ip_netseg)
ip_netseg_info = f"ip地址对应网络号: {ip_netseg}/{new_maskint} 十六进制表示: {ip_netseg_hex_address}\n" # 第 5 行
ip_hostseg_info = f"本ip为本网段第 {ip_hostseg + 1} 个ip(第1个ip是主机号为全0的ip)\n主机号可用ip总量: {host_seg_num} " # 第 6、7 行
ip_netseg_int = cofnet.get_netseg_int(input_ip_str, new_maskint)
last_ip_address = cofnet.int32_to_ip(ip_netseg_int + host_seg_num - 1)
ip_hostseg_range = f"({ip_netseg}->{last_ip_address})"
# 将ip相关信息输出到Text控件中
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_address)
start_index1 = "1.6"
end_index1 = "1." + str(6 + len(input_ip_str))
self.widget_dict_ipv4["text_ip_base_info"].tag_add("ip_address_fg", start_index1, end_index1)
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_int_show)
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_binary_show)
new_maskint_with_space = cofnet.get_maskint_with_space(int(new_maskint))
start_index2 = "3.11"
end_index2 = "3." + str(11 + new_maskint_with_space)
self.widget_dict_ipv4["text_ip_base_info"].tag_add("maskint_fg", start_index2, end_index2)
self.widget_dict_ipv4["text_ip_base_info"].tag_add("hostseg_fg", end_index2, end_index2 + " lineend")
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, maskbyte_show)
start_index3 = "4.6"
end_index3 = "4." + str(6 + len(maskbyte))
self.widget_dict_ipv4["text_ip_base_info"].tag_add("maskbyte_fg", start_index3, end_index3)
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_netseg_info)
start_index4 = "5.11"
end_index4 = "5." + str(11 + len(ip_netseg))
start_index4_2 = "5." + str(12 + len(ip_netseg))
end_index4_2 = "5." + str(12 + len(str(new_maskint)))
self.widget_dict_ipv4["text_ip_base_info"].tag_add("maskint_fg", start_index4, end_index4)
self.widget_dict_ipv4["text_ip_base_info"].tag_add("maskbyte_fg", start_index4_2, end_index4_2)
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_hostseg_info)
start_index5 = "6.9"
end_index5 = "6." + str(9 + len(str(ip_hostseg)))
self.widget_dict_ipv4["text_ip_base_info"].tag_add("hostseg_fg", start_index5, end_index5)
start_index6 = "7.11"
end_index6 = "7." + str(11 + len(str(host_seg_num)))
self.widget_dict_ipv4["text_ip_base_info"].tag_add("hostseg_num_fg", start_index6, end_index6)
self.widget_dict_ipv4["text_ip_base_info"].insert(tkinter.END, ip_hostseg_range)
# 更新子网掩码滑块及spinbox的值
self.widget_dict_ipv4["sv_netmask_int"].set(int(new_maskint))
self.widget_dict_ipv4["netmask_scale"].set(int(new_maskint))
# 输出同一网段下的所有主机ip
self.widget_dict_ipv4["text_other_hostseg"].delete("1.0", tkinter.END)
if host_seg_num > 32768: # 小于17位掩码时不再显示同网段所有ip
self.widget_dict_ipv4["text_other_hostseg"].insert(tkinter.END, "小于17位掩码时不再显示同网段所有ip")
else:
head = "序号\tip地址\t备注\n"
self.widget_dict_ipv4["text_other_hostseg"].insert(tkinter.END, head)
ip_netseg_int = cofnet.get_netseg_int(input_ip_str, new_maskint)
for i in range(host_seg_num):
ip_address = cofnet.int32_to_ip(ip_netseg_int + i)
if i == ip_hostseg and i == 0:
ip_line_info = str(i + 1) + "\t" + ip_address + "\t此ip为您输入的ip(主机号为全0)\n"
elif i == ip_hostseg and i == host_seg_num - 1:
ip_line_info = str(i + 1) + "\t" + ip_address + "\t此ip为您输入的ip(主机号为全1)\n"
elif i == ip_hostseg:
ip_line_info = str(i + 1) + "\t" + ip_address + "\t此ip为您输入的ip\n"
elif i == 0:
ip_line_info = str(i + 1) + "\t" + ip_address + "\t此ip主机号为全0\n"
elif i == host_seg_num - 1:
ip_line_info = str(i + 1) + "\t" + ip_address + "\t此ip主机号为全1\n"
else:
ip_line_info = str(i + 1) + "\t" + ip_address + "\n"
self.widget_dict_ipv4["text_other_hostseg"].insert(tkinter.END, ip_line_info)
if i == ip_hostseg:
start_index = str(i + 2) + "." + str(len(str(i + 2)))
end_index = str(i + 2) + "." + str(len(str(i + 2)) + 1 + len(ip_address))
self.widget_dict_ipv4["text_other_hostseg"].tag_add("ip_address_fg", start_index, end_index)
# 记录当前网段及子网掩码位数
self.current_netseg = ip_netseg
self.current_maskint = new_maskint
self.is_calculated = True
self.widget_dict_ipv4["sv_input_ip"].set("")
self.widget_dict_ipv4["sv_input_ip"].set(input_ip_str + "/" + str(new_maskint))
def calculate_ip_maskint(self, input_ip_maskint_str, maskint=None):
# 输入信息为 ip/掩码位数,例如 "10.99.1.3/24"
# maskint如果要赋值,需要赋str类型的值
self.widget_dict_ipv4["text_ip_base_info"].delete("1.0", tkinter.END)
ip_maskint_seg_list = input_ip_maskint_str.split("/")
input_ip_str = ip_maskint_seg_list[0]
if maskint is None:
new_maskint = ip_maskint_seg_list[1]
else:
new_maskint = maskint
self.calculate_ip(input_ip_str, new_maskint)
def calculate_last_netseg(self):
# 计算上一子网信息
if not self.is_calculated:
return
else:
self.widget_dict_ipv4["text_ip_base_info"].delete("1.0", tkinter.END)
current_netseg_int = cofnet.ip_or_maskbyte_to_int(self.current_netseg)
shift_bit = 32 - int(self.current_maskint)
last_netseg = cofnet.int32_to_ip(((current_netseg_int >> shift_bit) - 1) << shift_bit)
self.calculate_ip(last_netseg, self.current_maskint)
def calculate_last_cidrv6(self):
# 计算上一地址块信息
if not self.is_calculated6:
return
else:
pass
def calculate_next_netseg(self):
# 计算下一子网信息
if not self.is_calculated:
return
else:
self.widget_dict_ipv4["text_ip_base_info"].delete("1.0", tkinter.END)
current_netseg_int = cofnet.ip_or_maskbyte_to_int(self.current_netseg)
shift_bit = 32 - int(self.current_maskint)
next_netseg = cofnet.int32_to_ip(((current_netseg_int >> shift_bit) + 1) << shift_bit)
self.calculate_ip(next_netseg, self.current_maskint)
def calculate_next_cidrv6(self):
# 计算下一地址块信息
if not self.is_calculated6:
return
else:
pass
def calculate_ip_range(self, input_ip_str):
# 输入信息为 ip-range,例如 "10.99.1.33-55"
# 需要计算ip默认所属类型,Class A,B,C,D
pass
def calculate_ip_range2(self, input_ip_str):
# 输入信息为 ip-range,例如 "10.99.1.33-10.99.1.55"
pass
def calculate_cidr(self, input_ip_str): # 被前面的 calculate_ip_maskint() 给替代了
# 输入信息为 cidr,例如 "10.99.1.0/24"
pass
@staticmethod
def ipv6_2seg_to_map_binary_str(two_seg_str: str, binary_str_offset: int) -> str:
two_seg_map_list = []
for i in range(binary_str_offset):
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[0])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[1])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[2])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[3])
two_seg_map_list.append(" : ")
two_seg_map_list.append(two_seg_str[4])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[5])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[6])
two_seg_map_list.append(" ")
two_seg_map_list.append(two_seg_str[7])
two_seg_map_list.append(" ")
return "".join(two_seg_map_list)
def calculate6_ipv6(self, input_ipv6_str: str, ipv6_prefix_len=None):
"""
输入信息为 ipv6,前缀位数(可选)
ipv6_prefix_len如果要赋值,需要赋str类型的值
"""
if ipv6_prefix_len is None:
new_ipv6_prefix_len = "128"
else:
if int(ipv6_prefix_len) < 0 or int(ipv6_prefix_len) > 128:
messagebox.showinfo("Error", "ipv6前缀位数应该在[0-128]区间内")
return
else:
new_ipv6_prefix_len = ipv6_prefix_len
self.widget_dict_ipv6["text_ipv6_base_info"].delete("1.0", tkinter.END)
# 开始计算
ipv6_address_full = cofnet.convert_to_ipv6_full(input_ipv6_str)
ipv6_address_short = cofnet.convert_to_ipv6_short(input_ipv6_str)
ipv6_prefix_cidrv6 = cofnet.get_ipv6_prefix_cidrv6(input_ipv6_str, int(new_ipv6_prefix_len))
ipv6_address_full_text = f"ipv6地址完全式: {ipv6_address_full}\n"
ipv6_address_short_text = f"ipv6地址缩写式: {ipv6_address_short}"
ipv6_prefix_text = f"ipv6地址前缀: {ipv6_prefix_cidrv6}\n"
# ipv6用二进制表示
ipv6_address_full_seg_list = ipv6_address_full.split(":")
seg_1_2 = ipv6_address_full_seg_list[0] + ipv6_address_full_seg_list[1]
seg_1_2_ip_format = cofnet.int32_to_ip(int(seg_1_2, base=16))
ipv6_seg_1_2_binary_str = cofnet.ip_or_maskbyte_to_binary_with_space(seg_1_2_ip_format)
seg_1_2_map_str = self.ipv6_2seg_to_map_binary_str(seg_1_2, 8)
ipv6_seg_1_2_binary_text = f"seg1_2: {ipv6_seg_1_2_binary_str} 1-32位\n{seg_1_2_map_str}<{seg_1_2_ip_format}>\n"
seg_3_4 = ipv6_address_full_seg_list[2] + ipv6_address_full_seg_list[3]
seg_3_4_ip_format = cofnet.int32_to_ip(int(seg_3_4, base=16))
ipv6_seg_3_4_binary_str = cofnet.ip_or_maskbyte_to_binary_with_space(seg_3_4_ip_format)
seg_3_4_map_str = self.ipv6_2seg_to_map_binary_str(seg_3_4, 8)
ipv6_seg_3_4_binary_text = f"seg3_4: {ipv6_seg_3_4_binary_str} 33-64位\n{seg_3_4_map_str}<{seg_3_4_ip_format}>\n"
seg_5_6 = ipv6_address_full_seg_list[4] + ipv6_address_full_seg_list[5]
seg_5_6_ip_format = cofnet.int32_to_ip(int(seg_5_6, base=16))
ipv6_seg_5_6_binary_str = cofnet.ip_or_maskbyte_to_binary_with_space(seg_5_6_ip_format)
seg_5_6_map_str = self.ipv6_2seg_to_map_binary_str(seg_5_6, 8)
ipv6_seg_5_6_binary_text = f"seg5_6: {ipv6_seg_5_6_binary_str} 65-96位\n{seg_5_6_map_str}<{seg_5_6_ip_format}>\n"
seg_7_8 = ipv6_address_full_seg_list[6] + ipv6_address_full_seg_list[7]
seg_7_8_ip_format = cofnet.int32_to_ip(int(seg_7_8, base=16))
ipv6_seg_7_8_binary_str = cofnet.ip_or_maskbyte_to_binary_with_space(seg_7_8_ip_format)
seg_7_8_map_str = self.ipv6_2seg_to_map_binary_str(seg_7_8, 8)
ipv6_seg_7_8_binary_text = f"seg7_8: {ipv6_seg_7_8_binary_str} 97-128位\n{seg_7_8_map_str}<{seg_7_8_ip_format}>\n"
# 将ip相关信息输出到Text控件中
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_address_full_text)
start_index1 = "1.11"
end_index1 = "1." + str(11 + len(ipv6_address_full))
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", start_index1, end_index1)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_address_short_text)
start_index2 = "2.11"
end_index2 = "2." + str(11 + len(ipv6_address_short))
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", start_index2, end_index2)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, "/" + new_ipv6_prefix_len + "\n")
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_prefix_text)
start_index3 = "3.10"
end_index3 = "3." + str(10 + len(ipv6_prefix_cidrv6))
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", start_index3, end_index3)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, "ipv6地址用二进制表示如下: 十进制表示:\n")
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_seg_1_2_binary_text)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_seg_3_4_binary_text)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_seg_5_6_binary_text)
self.widget_dict_ipv6["text_ipv6_base_info"].insert(tkinter.END, ipv6_seg_7_8_binary_text)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", "6.8", "6.40")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", "8.8", "8.40")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", "10.8", "10.40")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_address_fg", "12.8", "12.40")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("maskbyte_fg", "5.45", "5.51")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("maskbyte_fg", "7.45", "7.52")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("maskbyte_fg", "9.45", "9.52")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("maskbyte_fg", "11.45", "11.53")
two_seg_bin_prefix_num = int(new_ipv6_prefix_len) // 32
two_seg_bin_prefix_remainder = int(new_ipv6_prefix_len) % 32
color_bit_str_num = cofnet.get_maskint_with_space(two_seg_bin_prefix_remainder)
if two_seg_bin_prefix_num == 0:
start_index5 = "5.8"
end_index5 = "5." + str(8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", start_index5, end_index5)
start_index5_h = "5." + str(8 + color_bit_str_num)
end_index5_h = "5." + str(35 - color_bit_str_num + 8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", start_index5_h, end_index5_h)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "7.8", "7.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "9.8", "9.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "11.8", "11.43")
elif two_seg_bin_prefix_num == 1:
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "5.8", "5.43")
start_index7 = "7.8"
end_index7 = "7." + str(8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", start_index7, end_index7)
start_index7_h = "7." + str(8 + color_bit_str_num)
end_index7_h = "7." + str(35 - color_bit_str_num + 8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", start_index7_h, end_index7_h)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "9.8", "9.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "11.8", "11.43")
elif two_seg_bin_prefix_num == 2:
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "5.8", "5.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "7.8", "7.43")
start_index9 = "9.8"
end_index9 = "9." + str(8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", start_index9, end_index9)
start_index9_h = "9." + str(8 + color_bit_str_num)
end_index9_h = "9." + str(35 - color_bit_str_num + 8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", start_index9_h, end_index9_h)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", "11.8", "11.43")
elif two_seg_bin_prefix_num == 3:
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "5.8", "5.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "7.8", "7.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "9.8", "9.43")
start_index11 = "11.8"
end_index11 = "11." + str(8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", start_index11, end_index11)
start_index11_h = "11." + str(8 + color_bit_str_num)
end_index11_h = "11." + str(35 - color_bit_str_num + 8 + color_bit_str_num)
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("hostseg_fg", start_index11_h, end_index11_h)
else:
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "5.8", "5.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "7.8", "7.43")
self.widget_dict_ipv6["text_ipv6_base_info"].tag_add("ipv6_prefix_fg", "9.8", "9.43")