TK-Ping.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2.0-only.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 = ("freemonobold", 8)
  18. font2 = ("freemonobold", 8)#12) # 10
  19. font3 = ("freemonobold", 8) #12) # 16
  20. font20 = font=("freemonobold", 12) # 22
  21. font120 = font=("freemonobold", 22) # 22
  22. root = Tkinter.Tk()
  23. #root.geometry("900x700+100+100")
  24. #root.geometry("500x800+100+100")
  25. root.geometry("400x600+100+100")
  26. root.title( title)
  27. eframe = Tkinter.Frame(root)
  28. eframe.pack(side="top",expand=0,fill="x")
  29. eframe1 = Tkinter.Frame(root)
  30. eframe1.pack(side="top",expand=1,fill="both")
  31. #-------------------------------------------- line
  32. line_frame = Tkinter.Frame(eframe)
  33. line_frame.pack(side="top",expand=0,fill="x")
  34. local_ips = []
  35. #local_ips.append("2.0.0.255")
  36. cmd='ip a | grep " inet " | cut -d " " -f 6 | sort -h'
  37. r=os.popen(cmd)
  38. print("cmd",cmd)
  39. txt=r.readlines()
  40. for ip in txt:
  41. ip = ip.strip()
  42. print([ip])
  43. if ip.startswith("127."):
  44. continue
  45. if ip.count(".") != 3:
  46. continue
  47. print("-",ip)
  48. local_ips.append(ip)
  49. #-------------------------------------------- line
  50. lb = Tkinter.Label(line_frame,text="ping -c1 ",width=8,font=font2) #,state="disabled")
  51. lb.pack(side="left",expand=0,fill="y")
  52. #lb.insert("end","ping -c1 ")
  53. #-------------------------------------------- line
  54. #local_ips.sort()
  55. option_list=local_ips
  56. p_variable = Tkinter.StringVar(root)
  57. p_variable.set(option_list[0]) # default value
  58. e_poll_new = Tkinter.OptionMenu(line_frame, p_variable,*option_list)
  59. e_poll_new.configure(font=font2)
  60. e_poll_new.configure(width=16)
  61. e_poll_new["bg"] = "#fff"
  62. #print(dir(e_poll_new))
  63. e_poll_new.option_add("end","") #', 'option_clear', 'option_get
  64. e_poll_new.option_add("end","215.0.0.0")
  65. e_poll_new.pack(side="left")
  66. #exit()
  67. import queue
  68. q = queue.Queue()
  69. def clear_list():
  70. b_scan.delete("0.0","end")
  71. def sort_list():
  72. lines = b_scan.get("0.0","end")
  73. lines = lines.split("\n")
  74. lines = list(set(lines))
  75. lines.sort()
  76. b_scan.delete("0.0","end")
  77. for line in lines:
  78. print("-",[line])
  79. if line:
  80. b_scan.insert("end",line+"\n")
  81. count_list()
  82. def count_list():
  83. lines = b_scan.get("0.0","end")
  84. lines = lines.split("\n")
  85. lines = list(set(lines))
  86. lines.sort()
  87. i=0
  88. for line in lines:
  89. if line.strip():
  90. i+=1
  91. b_count["text"] = str(i)
  92. def clean_ip(ip):
  93. if "/" in ip:
  94. ip = ip.split("/")[0]
  95. ip = ip.replace("_","")
  96. ip = ip.split(".")
  97. ip = "{}.{}.{}.{}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
  98. return ip
  99. def prettier_ip(ip):
  100. if "/" in ip:
  101. ip = ip.split("/")[0]
  102. ip = ip.replace("_","")
  103. ip = ip.split(".")
  104. ip = "{:03}.{:03}.{:03}.{:03}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
  105. return ip
  106. def cmd(IP):
  107. #print("IP:",IP)
  108. #b_scan.insert("end",IP+"\n")
  109. ip = "xxx"
  110. try:
  111. myip = p_variable.get() # = Tkinter.StringVar(root)
  112. myip = clean_ip(myip)
  113. except Exception as e:
  114. print("err",e)
  115. CMD = "ping -c1 '{}' 2> /dev/null".format(clean_ip(IP))
  116. print(CMD)
  117. r=os.popen(CMD)
  118. #CMD = "ping -c1 '{}' 2> /dev/null".format(IP)
  119. for line in r.readlines():
  120. #print([line])
  121. #print(IP,[line],CMD)
  122. if " packets transmitted" in line and not "100%" in line:
  123. print(IP,[line],CMD)
  124. if myip in clean_ip(IP):
  125. b_scan.insert("end",str(IP).ljust(50," ")+str(" OK <-- me ")+"\n")
  126. else:
  127. b_scan.insert("end",str(IP).ljust(50," ")+str(" OK")+"\n")
  128. count_list()
  129. ping_lock=0
  130. def _ping():
  131. global ping_lock
  132. if ping_lock:
  133. return
  134. btn_ping["bg"] = "orange"
  135. ping_lock=1
  136. b_scan.insert("end","\n")
  137. ip = p_variable.get() # = Tkinter.StringVar(root)
  138. ip = ".".join(ip.split(".")[:-1])
  139. print("ip",ip)
  140. out=[]
  141. for i in range(1,255):
  142. IP="{}.{}".format(ip,i)
  143. IP= prettier_ip(IP)
  144. out.append(IP)
  145. for o in out:
  146. print(":",o)
  147. for IP in out:
  148. thread.start_new_thread(cmd,(IP,))
  149. ping_lock=0
  150. btn_ping["bg"] = "green"
  151. def ping():
  152. thread.start_new_thread(_ping,()) #node_cmd_recive, () )
  153. #-------------------------------------------- line
  154. btn_ping = Tkinter.Button(line_frame,text="start Ping",width=7,command=ping,font=font2)
  155. btn_ping.pack(side="left",expand=0,fill="y")
  156. #-------------------------------------------- line
  157. b_scan2 = Tkinter.Button(line_frame,text="sort",width=2,command=sort_list,font=font2)
  158. b_scan2.pack(side="left",expand=0,fill="y")
  159. #-------------------------------------------- line
  160. b_scan2 = Tkinter.Button(line_frame,text="clear",width=2,command=clear_list,font=font2)
  161. b_scan2.pack(side="left",expand=0,fill="y")
  162. #-------------------------------------------- line
  163. b_count = Tkinter.Button(line_frame,text="0",width=1,font=font2)
  164. b_count.pack(side="left",expand=0,fill="y")
  165. #-------------------------------------------- line
  166. eframe10 = Tkinter.Frame(eframe1)
  167. eframe10.pack(side="top",expand=1,fill="both")
  168. scroll_bar = Tkinter.Scrollbar(eframe10)
  169. scroll_bar.pack(side="right", fill="y")
  170. b_scan = Tkinter.Text(eframe10,width=16,font=font1,yscrollcommand = scroll_bar.set)
  171. b_scan.pack(side="left",expand=1,fill="both")
  172. scroll_bar.config( command = b_scan.yview )
  173. #-------------------------------------------- line
  174. root.mainloop()