TK-Ping.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Valid-License-Identifier: GPL-2.0-only
  5. SPDX-URL: https://spdx.org/licenses/GPL-1.0.html
  6. (c) 2012 micha@librelight.de
  7. """
  8. import os
  9. import time
  10. import struct
  11. import sys
  12. import tkinter as Tkinter
  13. import _thread as thread
  14. import nodescan
  15. title = "TK-Ping"
  16. sys.stdout.write("\x1b]2;"+title+"\x07")
  17. font1 = ("Helvetica", 8)
  18. font2 = ("Helvetica", 12) # 10
  19. font3 = ("Helvetica", 12) # 16
  20. font20 = font=("Helvetica", 12) # 22
  21. font120 = font=("Helvetica", 22) # 22
  22. root = Tkinter.Tk()
  23. #root.geometry("900x700+100+100")
  24. root.geometry("500x800+100+100")
  25. root.title( title)
  26. eframe = Tkinter.Frame(root)
  27. eframe.pack(side="top",expand=0,fill="x")
  28. eframe1 = Tkinter.Frame(root)
  29. eframe1.pack(side="top",expand=1,fill="both")
  30. #-------------------------------------------- line
  31. line_frame = Tkinter.Frame(eframe)
  32. line_frame.pack(side="top",expand=0,fill="x")
  33. local_ips = []
  34. #local_ips.append("2.0.0.255")
  35. cmd='ip a | grep " inet " | cut -d " " -f 6 | sort -h'
  36. r=os.popen(cmd)
  37. print("cmd",cmd)
  38. txt=r.readlines()
  39. for ip in txt:
  40. ip = ip.strip()
  41. print([ip])
  42. if ip.startswith("127."):
  43. continue
  44. if ip.count(".") != 3:
  45. continue
  46. print("-",ip)
  47. local_ips.append(ip)
  48. #local_ips.sort()
  49. option_list=local_ips
  50. p_variable = Tkinter.StringVar(root)
  51. p_variable.set(option_list[0]) # default value
  52. e_poll_new = Tkinter.OptionMenu(line_frame, p_variable,*option_list)
  53. e_poll_new.configure(font=("Helvetica", 13))
  54. e_poll_new.configure(width=16)
  55. e_poll_new["bg"] = "#fff"
  56. #print(dir(e_poll_new))
  57. e_poll_new.option_add("end","") #', 'option_clear', 'option_get
  58. e_poll_new.option_add("end","215.0.0.0")
  59. e_poll_new.pack(side="left")
  60. #exit()
  61. import queue
  62. q = queue.Queue()
  63. def clear_list():
  64. b_scan.delete("0.0","end")
  65. def sort_list():
  66. lines = b_scan.get("0.0","end")
  67. lines = lines.split("\n")
  68. lines = list(set(lines))
  69. lines.sort()
  70. b_scan.delete("0.0","end")
  71. for line in lines:
  72. #print("-",[line])
  73. if line:
  74. b_scan.insert("end",line+"\n")
  75. def cmd(CMD):
  76. #print("CMD:",CMD)
  77. #b_scan.insert("end",CMD+"\n")
  78. ip = "xxx"
  79. try:
  80. ip = p_variable.get() # = Tkinter.StringVar(root)
  81. ip = ip.split("/")[0]
  82. ip = ip.split(".")
  83. ip = "{}.{}.{}.{:03}".format(ip[0],ip[1],ip[2],int(ip[3]))
  84. except Exception as e:
  85. print("err",e)
  86. r=os.popen(CMD+" 2> /dev/null ")
  87. for line in r.readlines():
  88. #print([line])
  89. if " packets transmitted" in line and not "100%" in line:
  90. print(CMD,[line])
  91. if ip in CMD:
  92. b_scan.insert("end",str(CMD).ljust(50," ")+str(" OK <-- me ")+"\n")
  93. else:
  94. b_scan.insert("end",str(CMD).ljust(50," ")+str(" OK")+"\n")
  95. ping_lock=0
  96. def _ping():
  97. global ping_lock
  98. if ping_lock:
  99. return
  100. btn_ping["bg"] = "orange"
  101. ping_lock=1
  102. b_scan.insert("end","\n")
  103. ip = p_variable.get() # = Tkinter.StringVar(root)
  104. ip = ".".join(ip.split(".")[:-1])
  105. print("ip",ip)
  106. for i in range(1,255):
  107. CMD="ping -c1 '{}.{:03}' ".format(ip,i)
  108. #cmd(CMD)
  109. thread.start_new_thread(cmd,(CMD,)) #node_cmd_recive, () )
  110. ping_lock=0
  111. btn_ping["bg"] = "green"
  112. #sort_list()
  113. def ping():
  114. thread.start_new_thread(_ping,()) #node_cmd_recive, () )
  115. #-------------------------------------------- line
  116. btn_ping = Tkinter.Button(line_frame,text="start Ping",width=8,command=ping,font=font2)
  117. btn_ping.pack(side="left",expand=0,fill="y")
  118. #-------------------------------------------- line
  119. b_scan2 = Tkinter.Button(line_frame,text="sort",width=8,command=sort_list,font=font2)
  120. b_scan2.pack(side="left",expand=0,fill="y")
  121. #-------------------------------------------- line
  122. b_scan2 = Tkinter.Button(line_frame,text="clear",width=8,command=clear_list,font=font2)
  123. b_scan2.pack(side="left",expand=0,fill="y")
  124. #-------------------------------------------- line
  125. eframe10 = Tkinter.Frame(eframe1)
  126. eframe10.pack(side="top",expand=1,fill="both")
  127. scroll_bar = Tkinter.Scrollbar(eframe10)
  128. scroll_bar.pack(side="right", fill="y")
  129. b_scan = Tkinter.Text(eframe10,width=20,font=font2,yscrollcommand = scroll_bar.set)
  130. b_scan.pack(side="left",expand=1,fill="both")
  131. scroll_bar.config( command = b_scan.yview )
  132. #-------------------------------------------- line
  133. root.mainloop()