最后奉上源码,python3开发
检测功能 如下==》
import threading
import subprocess
import time
import os
from queue import Queue
start_time=time.time()
#删除指定文件
if os.path.exists('掉线IP.txt'):
os.remove('掉线IP.txt')
#定义工作线程
WORD_THREAD=50
#定义IP列表
IPs=[]
#将需要ping 的ip加入队列
IP_QUEUE = Queue()
# for i in range(1,5):
# IP_QUEUE.put('192.168.1.'+str(i))
# for j in range(1,255):
# IP_QUEUE.put('192.168.2.'+str(j))
# file1= open('待检测IP.txt','r',encoding='utf-8') #打开要去掉空行的文件
# file2 =open('待检测IP2.txt','w',encoding='utf-8') #生成没有空行的文件
# for line in file1.readlines():
# if line == '\n':
# line=line.strip('\n')
# file2.write(line)
# file1.close()
# file2.close()
f=open('待检测IP.txt',encoding='utf-8')
lines=f.readlines()
for line in lines:
if line.isspace():
continue
else:
line=line.replace("\n","")
#line =line.replace("\t","")
IPs.append(line)
for x in IPs:
#print(x)
IP_QUEUE.put(x)
# with open ('待检测IP.txt') as file:
# for myline in file.readlines():
# if myline.isspace:
# continue
# else:
# print(myline)
#print(myline,end='') #end=""
#定义一个执行ping 的函数
def ping_ip():
while not IP_QUEUE.empty():
ip=IP_QUEUE.get()
res =subprocess.call('ping -n 1 -w 5 %s' %ip,stdout=subprocess.PIPE) #linux系统将'gn'替换成'-c'
#运行结果
print(ip,True if res ==0 else False)
if res !=0 :
with open('掉线IP.txt','a') as f:
f.write(ip+'\n')#进行换行输入f.write(ip+'\n')
if __name__ == '__main__':
threads=[]
for i in range(WORD_THREAD):
thread=threading.Thread(target=ping_ip)#绑定方法
thread.start()
#time.sleep(0.1)
threads.append(thread)
for thread in threads:
thread.join()
print('程序运行耗时:%s' %(time.time()-start_time))
----------------------------------------------
报警功能,如下==》
import win32api,win32con
count = len(open('掉线IP.txt','rU').readlines())
print(count)
if count>=1:
win32api.MessageBox(0,'现已发现掉线设备总数:%s(台),请注意结合"掉线IP.txt"文本信息,安排检查现场!' %count,"提醒",win32con.MB_YESNOCANCEL)