今天写的。
用IPERF作测试局域网速度的工具。
用AD域组策略推送给客户端,
xcopy /y \\XXX\XXX\Iperf\*.* c:\Iperf\*.*
然后,客户端会在每次LOGON的执行测试网速的BAT文件,并将结果存放在服务器指定的LOG目录下面。
c:\Iperf\Iperf3.exe -c x.x.x. > \\X.X.X.X\Iperf\log\%computername%.txt
之后,PYTHON上场。
分析指定目录下的每一个文件里的每一行,
将指定列表中的数字与提前指定的基线作比较。
分出不同的电脑列表。
然后发给指定邮件地址。
最后,指定WINDOWS计划任务或是LINUX CRONTAB JOB。
搞定。
1 #!/usr/bin/env python 2 import sys 3 import os.path 4 from smtplib import SMTP 5 import datetime 6 7 8 folder = 'D:\Iperf\Log\\' 9 msg_low_sh = '' 10 msg_normal = '' 11 msg_low_us = '' 12 msg_nodata = '' 13 for filename in os.listdir (folder): 14 #print(filename) 15 16 fp = open(folder + filename,'r') 17 18 for i, line in enumerate(fp): 19 if (i == 0 and line == "") or (len(line) < 3 ): 20 msg_nodata += filename + ".have now data.\n" 21 elif (i == 6): 22 speednumber = line.split() 23 if (float(speednumber[6])) < 300 and (float(speednumber[6])) > 8: 24 msg_low_sh += filename + ". Low Speed at ShangHai: " + speednumber[6] + "Mbits. We should notice this machine.\n" 25 if (float(speednumber[6])) <1.0: 26 msg_low_us += filename + "Low Speed at US: " + speednumber[6] + "Mbits. We should notice this machine.\n" 27 else: 28 msg_normal += filename + ". Normal Speed: " + speednumber[6] + "Mbits.\n" 29 fp.close() 30 #print (msg_low_sh) 31 #print (msg_low_us) 32 #print (msg_normal) 33 #print (msg_nodata) 34 35 text_file = open("LANSpeedOutput.txt", "w") 36 37 text_file.write(msg_low_sh) 38 text_file.write(msg_low_us) 39 text_file.write("\n\n\n") 40 text_file.write(msg_normal) 41 text_file.write("\n\n\n") 42 text_file.write(msg_nodata) 43 44 text_file.close() 45 46 debuglevel = 0 47 48 smtp = SMTP() 49 smtp.set_debuglevel(debuglevel) 50 smtp.connect('X.X.X.X', 25) 51 smtp.login('xxx', 'xxx') 52 53 from_addr = "<xxx>" 54 to_addr = ["xxx","xxx","xxx"] 55 date = datetime.datetime.now().strftime( "%d/%m/%Y " ) 56 fp = open(r'LANSpeedOutput.txt', 'r') 57 58 subj = "LAN Speed log at "+date 59 message_text = fp.read() 60 61 msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text ) 62 63 smtp.sendmail(from_addr, to_addr, msg) 64 smtp.quit()
最后效果图:
具体细节,再慢慢完善。
时间: 2025-01-30 12:04:02