python 实现局域网探测在线主机

 

python 3实现局域网探测在线主机,代码如下:

 1 # -*- coding: GBK -*-
 2 from tkinter import *
 3 import subprocess
 4 import threading
 5 import time
 6 import re
 7 
 8 
 9 def btn_click():
10     text.insert(INSERT, ("IP地址        状态!\n"))
11     i = 0
12     j = 1
13     thread_id = []
14     ip = int(ipText.get())
15     v = ('192.168.10.%d' % (x) for x in range(0, ip + 1))
16     for it in v:
17         loop = str('%s' % it)
18         thread_id.append(0)
19         thread_id[i] = Ping(loop, int(j / 20))
20         thread_id[i].start()
21         # thread_id[i].join()
22         i = i + 1
23         j = j + 10
24 
25 
26 class Ping(threading.Thread):
27     def __init__(self, str_ip, sleep_time):
28         threading.Thread.__init__(self)
29         self.str_ip = str_ip
30         self.sleep_time = sleep_time
31 
32     def run(self):
33         time.sleep(self.sleep_time)
34         ftp_ret = subprocess.Popen('ping %s -n 3' % self.str_ip, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
35                                    shell=True)
36         ret = ftp_ret.stdout.read()
37         str_ret = ret.decode("gbk")
38         ret_s = re.search("TTL", str_ret)
39         if ret_s:
40             text.insert(INSERT, ("%s    在线!\n" % self.str_ip))
41             # text.update()
42         else:
43             text.insert(INSERT, ("%s    下线!\n" % self.str_ip))
44 
45 
46 root = Tk()
47 root.title('局域网Ping程序')
48 root.geometry('600x400')
49 main_frame = Frame(root)
50 text_frame = Frame(main_frame)
51 ip_frame = Frame(main_frame)
52 botton_frame = Frame(ip_frame)
53 
54 l1 = Label(ip_frame, text='IP地址:192.168.10 从 0 到')
55 l2 = Label(ip_frame, text='   线程20   ')
56 ipText = Entry(ip_frame)
57 
58 l1.pack(side='left')
59 ipText.pack(side='left')
60 ipText['width'] = 5
61 l2.pack(side='left')
62 b = Button(ip_frame, text='开始', command=btn_click)
63 b['width'] = 4
64 b['height'] = 1
65 b.pack(side='left')
66 
67 text = Text(text_frame, width=30, height=25)
68 text.pack()
69 main_frame.pack()
70 ip_frame.pack(side='top', pady='5')
71 text_frame.pack()
72 root.mainloop()

 看效果图:

posted @ 2019-09-19 16:13  May_It_Be  阅读(904)  评论(0)    收藏  举报