TK-Ping.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 sort_list():
  64. lines = b_scan.get("0.0","end")
  65. lines = lines.split("\n")
  66. lines = list(set(lines))
  67. lines.sort()
  68. b_scan.delete("0.0","end")
  69. for line in lines:
  70. #print("-",[line])
  71. if line:
  72. b_scan.insert("end",line+"\n")
  73. def cmd(CMD):
  74. print("CMD:",CMD)
  75. #b_scan.insert("end",CMD+"\n")
  76. ip = "xxx"
  77. try:
  78. ip = p_variable.get() # = Tkinter.StringVar(root)
  79. ip = ip.split("/")[0]
  80. ip = ip.split(".")
  81. ip = "{}.{}.{}.{:03}".format(ip[0],ip[1],ip[2],int(ip[3]))
  82. except Exception as e:
  83. print("err",e)
  84. r=os.popen(CMD+" 2> /dev/null ")
  85. for line in r.readlines():
  86. print([line])
  87. if " packets transmitted" in line and not "100%" in line:
  88. print(CMD,[line])
  89. if ip in CMD:
  90. b_scan.insert("end",str(CMD).ljust(50," ")+str(" OK <-- me ")+"\n")
  91. else:
  92. b_scan.insert("end",str(CMD).ljust(50," ")+str(" OK")+"\n")
  93. ping_lock=0
  94. def _ping():
  95. global ping_lock
  96. if ping_lock:
  97. return
  98. btn_ping["bg"] = "orange"
  99. ping_lock=1
  100. b_scan.insert("end","\n")
  101. ip = p_variable.get() # = Tkinter.StringVar(root)
  102. ip = ".".join(ip.split(".")[:-1])
  103. print("ip",ip)
  104. for i in range(1,255):
  105. CMD="ping -c1 '{}.{:03}' ".format(ip,i)
  106. #cmd(CMD)
  107. thread.start_new_thread(cmd,(CMD,)) #node_cmd_recive, () )
  108. ping_lock=0
  109. btn_ping["bg"] = "green"
  110. #sort_list()
  111. def ping():
  112. thread.start_new_thread(_ping,()) #node_cmd_recive, () )
  113. #-------------------------------------------- line
  114. btn_ping = Tkinter.Button(line_frame,text="start Ping",width=8,command=ping,font=font2)
  115. btn_ping.pack(side="left",expand=0,fill="y")
  116. #-------------------------------------------- line
  117. b_scan2 = Tkinter.Button(line_frame,text="sort",width=8,command=sort_list,font=font2)
  118. b_scan2.pack(side="left",expand=0,fill="y")
  119. #-------------------------------------------- line
  120. eframe10 = Tkinter.Frame(eframe1)
  121. eframe10.pack(side="top",expand=1,fill="both")
  122. scroll_bar = Tkinter.Scrollbar(eframe10)
  123. scroll_bar.pack(side="right", fill="y")
  124. b_scan = Tkinter.Text(eframe10,width=20,font=font2,yscrollcommand = scroll_bar.set)
  125. b_scan.pack(side="left",expand=1,fill="both")
  126. scroll_bar.config( command = b_scan.yview )
  127. #-------------------------------------------- line
  128. root.mainloop()