123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #! /usr/bin/python
- # -*- coding: utf-8 -*-
- """
- Valid-License-Identifier: GPL-2.0-only
- SPDX-URL: https://spdx.org/licenses/GPL-2.0-only.html
- (c) 2012 micha@librelight.de
- """
- import os
- import time
- import struct
- import sys
- import tkinter as Tkinter
- import _thread as thread
- import nodescan
- title = "TK-Ping"
- sys.stdout.write("\x1b]2;"+title+"\x07")
- font1 = ("freemonobold", 8)
- font2 = ("freemonobold", 8)#12) # 10
- font3 = ("freemonobold", 8) #12) # 16
- font20 = font=("freemonobold", 12) # 22
- font120 = font=("freemonobold", 22) # 22
- root = Tkinter.Tk()
- #root.geometry("900x700+100+100")
- #root.geometry("500x800+100+100")
- root.geometry("400x600+100+100")
- root.title( title)
- eframe = Tkinter.Frame(root)
- eframe.pack(side="top",expand=0,fill="x")
- eframe1 = Tkinter.Frame(root)
- eframe1.pack(side="top",expand=1,fill="both")
- #-------------------------------------------- line
- line_frame = Tkinter.Frame(eframe)
- line_frame.pack(side="top",expand=0,fill="x")
- local_ips = []
- #local_ips.append("2.0.0.255")
- cmd='ip a | grep " inet " | cut -d " " -f 6 | sort -h'
- r=os.popen(cmd)
- print("cmd",cmd)
- txt=r.readlines()
- for ip in txt:
- ip = ip.strip()
- print([ip])
- if ip.startswith("127."):
- continue
- if ip.count(".") != 3:
- continue
-
- print("-",ip)
- local_ips.append(ip)
- #-------------------------------------------- line
- lb = Tkinter.Label(line_frame,text="ping -c1 ",width=8,font=font2) #,state="disabled")
- lb.pack(side="left",expand=0,fill="y")
- #lb.insert("end","ping -c1 ")
- #-------------------------------------------- line
- #local_ips.sort()
- option_list=local_ips
- p_variable = Tkinter.StringVar(root)
- p_variable.set(option_list[0]) # default value
- e_poll_new = Tkinter.OptionMenu(line_frame, p_variable,*option_list)
- e_poll_new.configure(font=font2)
- e_poll_new.configure(width=16)
- e_poll_new["bg"] = "#fff"
- #print(dir(e_poll_new))
- e_poll_new.option_add("end","") #', 'option_clear', 'option_get
- e_poll_new.option_add("end","215.0.0.0")
- e_poll_new.pack(side="left")
- #exit()
- import queue
- q = queue.Queue()
- def clear_list():
- b_scan.delete("0.0","end")
- def sort_list():
- lines = b_scan.get("0.0","end")
- lines = lines.split("\n")
- lines = list(set(lines))
- lines.sort()
- b_scan.delete("0.0","end")
- for line in lines:
- print("-",[line])
- if line:
- b_scan.insert("end",line+"\n")
- count_list()
- def count_list():
- lines = b_scan.get("0.0","end")
- lines = lines.split("\n")
- lines = list(set(lines))
- lines.sort()
- i=0
- for line in lines:
- if line.strip():
- i+=1
- b_count["text"] = str(i)
- def clean_ip(ip):
- if "/" in ip:
- ip = ip.split("/")[0]
- ip = ip.replace("_","")
- ip = ip.split(".")
- ip = "{}.{}.{}.{}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
- return ip
- def prettier_ip(ip):
- if "/" in ip:
- ip = ip.split("/")[0]
- ip = ip.replace("_","")
- ip = ip.split(".")
- ip = "{:03}.{:03}.{:03}.{:03}".format(int(ip[0]),int(ip[1]),int(ip[2]),int(ip[3]))
- return ip
- def cmd(IP):
- #print("IP:",IP)
- #b_scan.insert("end",IP+"\n")
- ip = "xxx"
- try:
- myip = p_variable.get() # = Tkinter.StringVar(root)
- myip = clean_ip(myip)
- except Exception as e:
- print("err",e)
- CMD = "ping -c1 '{}' 2> /dev/null".format(clean_ip(IP))
- print(CMD)
- r=os.popen(CMD)
- #CMD = "ping -c1 '{}' 2> /dev/null".format(IP)
- for line in r.readlines():
- #print([line])
- #print(IP,[line],CMD)
- if " packets transmitted" in line and not "100%" in line:
- print(IP,[line],CMD)
- if myip in clean_ip(IP):
- b_scan.insert("end",str(IP).ljust(50," ")+str(" OK <-- me ")+"\n")
- else:
- b_scan.insert("end",str(IP).ljust(50," ")+str(" OK")+"\n")
- count_list()
- ping_lock=0
- def _ping():
- global ping_lock
- if ping_lock:
- return
- btn_ping["bg"] = "orange"
- ping_lock=1
- b_scan.insert("end","\n")
- ip = p_variable.get() # = Tkinter.StringVar(root)
- ip = ".".join(ip.split(".")[:-1])
- print("ip",ip)
- out=[]
- for i in range(1,255):
- IP="{}.{}".format(ip,i)
- IP= prettier_ip(IP)
- out.append(IP)
- for o in out:
- print(":",o)
- for IP in out:
- thread.start_new_thread(cmd,(IP,))
- ping_lock=0
- btn_ping["bg"] = "green"
- def ping():
- thread.start_new_thread(_ping,()) #node_cmd_recive, () )
-
- #-------------------------------------------- line
- btn_ping = Tkinter.Button(line_frame,text="start Ping",width=7,command=ping,font=font2)
- btn_ping.pack(side="left",expand=0,fill="y")
- #-------------------------------------------- line
- b_scan2 = Tkinter.Button(line_frame,text="sort",width=2,command=sort_list,font=font2)
- b_scan2.pack(side="left",expand=0,fill="y")
- #-------------------------------------------- line
- b_scan2 = Tkinter.Button(line_frame,text="clear",width=2,command=clear_list,font=font2)
- b_scan2.pack(side="left",expand=0,fill="y")
- #-------------------------------------------- line
- b_count = Tkinter.Button(line_frame,text="0",width=1,font=font2)
- b_count.pack(side="left",expand=0,fill="y")
- #-------------------------------------------- line
- eframe10 = Tkinter.Frame(eframe1)
- eframe10.pack(side="top",expand=1,fill="both")
- scroll_bar = Tkinter.Scrollbar(eframe10)
- scroll_bar.pack(side="right", fill="y")
-
- b_scan = Tkinter.Text(eframe10,width=16,font=font1,yscrollcommand = scroll_bar.set)
- b_scan.pack(side="left",expand=1,fill="both")
- scroll_bar.config( command = b_scan.yview )
- #-------------------------------------------- line
- root.mainloop()
|