TK-Ping.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = ("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. def clean_ip(ip):
  82. if "/" in ip:
  83. ip = ip.split("/")[0]
  84. ip = ip.replace("_","")
  85. ip = ip.split(".")
  86. ip = "{}.{}.{}.{}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
  87. return ip
  88. def prettier_ip(ip):
  89. if "/" in ip:
  90. ip = ip.split("/")[0]
  91. ip = ip.replace("_","")
  92. ip = ip.split(".")
  93. ip = "{:03}.{:03}.{:03}.{:03}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
  94. return ip
  95. def cmd(IP):
  96. #print("IP:",IP)
  97. #b_scan.insert("end",IP+"\n")
  98. ip = "xxx"
  99. try:
  100. myip = p_variable.get() # = Tkinter.StringVar(root)
  101. myip = clean_ip(myip)
  102. except Exception as e:
  103. print("err",e)
  104. CMD = "ping -c1 '{}' 2> /dev/null".format(clean_ip(IP))
  105. print(CMD)
  106. r=os.popen(CMD)
  107. #CMD = "ping -c1 '{}' 2> /dev/null".format(IP)
  108. for line in r.readlines():
  109. #print([line])
  110. #print(IP,[line],CMD)
  111. if " packets transmitted" in line and not "100%" in line:
  112. print(IP,[line],CMD)
  113. if myip in clean_ip(IP):
  114. b_scan.insert("end",str(IP).ljust(50," ")+str(" OK <-- me ")+"\n")
  115. else:
  116. b_scan.insert("end",str(IP).ljust(50," ")+str(" OK")+"\n")
  117. ping_lock=0
  118. def _ping():
  119. global ping_lock
  120. if ping_lock:
  121. return
  122. btn_ping["bg"] = "orange"
  123. ping_lock=1
  124. b_scan.insert("end","\n")
  125. ip = p_variable.get() # = Tkinter.StringVar(root)
  126. ip = ".".join(ip.split(".")[:-1])
  127. print("ip",ip)
  128. out=[]
  129. for i in range(1,255):
  130. IP="{}.{}".format(ip,i)
  131. IP= prettier_ip(IP)
  132. out.append(IP)
  133. for o in out:
  134. print(":",o)
  135. for IP in out:
  136. thread.start_new_thread(cmd,(IP,))
  137. ping_lock=0
  138. btn_ping["bg"] = "green"
  139. def ping():
  140. thread.start_new_thread(_ping,()) #node_cmd_recive, () )
  141. #-------------------------------------------- line
  142. btn_ping = Tkinter.Button(line_frame,text="start Ping",width=7,command=ping,font=font2)
  143. btn_ping.pack(side="left",expand=0,fill="y")
  144. #-------------------------------------------- line
  145. b_scan2 = Tkinter.Button(line_frame,text="sort",width=5,command=sort_list,font=font2)
  146. b_scan2.pack(side="left",expand=0,fill="y")
  147. #-------------------------------------------- line
  148. b_scan2 = Tkinter.Button(line_frame,text="clear",width=5,command=clear_list,font=font2)
  149. b_scan2.pack(side="left",expand=0,fill="y")
  150. #-------------------------------------------- line
  151. eframe10 = Tkinter.Frame(eframe1)
  152. eframe10.pack(side="top",expand=1,fill="both")
  153. scroll_bar = Tkinter.Scrollbar(eframe10)
  154. scroll_bar.pack(side="right", fill="y")
  155. b_scan = Tkinter.Text(eframe10,width=16,font=font1,yscrollcommand = scroll_bar.set)
  156. b_scan.pack(side="left",expand=1,fill="both")
  157. scroll_bar.config( command = b_scan.yview )
  158. #-------------------------------------------- line
  159. root.mainloop()