"""Network Monitoring v2.0, improved to use multi-process concurrency monitoring multiple IP or domain names, more efficient compatible WinXP system simultaneously check local to baidu.com or qq.com and other communications and attached to the mail, you can more objectively understand the local network situation linxingyi20160305 """ImportOS, time,subprocess, Smtplib, datetime, Platform fromMultiprocessingImportPool fromEmail.headerImportHeader fromEmail.mime.textImportMimetextip= ["www.baidu.com","www.qq.com","www.sina.com"]#a list of temporary files that hold the result of the ping command, with the same size as the IP listf = ["D:/ping11.txt","D:/ping12.txt","D:/ping13.txt"]#Save a list of network states in the same size as the IP listStatus = ["","",""]#definition of outgoing mailFROM_ADDR ='' #E -mail address of the senderTO_ADDR = ['# # # # @qq. com']#recipient e-mail address, with, to separateSmtp_server ='smtp.qq.com'Password='############'Mailtext= ["","",""]#Monitor IP traffic, output to temp filedefPing (name,ip,file):Print('Run Task%s (%s) ...'%( name, Os.getpid ())) Subprocess.call (["Ping"Ip"- N"," -"],shell=true,stdout=open (file,"WB"))#determine network status based on temporary file contentsdefNetstatus (FILE,IP):#determine if it is WinXPifPlatform.version (). Split (".") [0] = ="5": With open (file,"R") as F:a=F.readlines ()ifLen (a)! = 1:#IP = A[2].split () [1]count = Int (A[-6].split () [-3]) value= Int (A[-2].split () [ -1][:-2]) log (IP+"the number of packets dropped is%s"%count) log (IP+"average ping value is%s"%value)#judging the condition of the network differenceifCount > 5orValue > 100: Status=" Bad"returnStatus,a[-6:]Else: Status="Good"returnStatus,a[-6:]Else:#IP = A[0].split () [1]Status ="the request cannot be found on the host"Log (IP+status)returnStatus,a[0]Else: With open (file,"R") as F:a=F.readlines ()ifLen (a)! = 1:#IP = A[1].split () [2]count = Int (A[-3].split () [-3]) value= Int (A[-1].split () [ -1][:-2]) log (IP+"the number of packets dropped is%s"%count) log (IP+"average ping value is%s"%value)#judging the condition of the network differenceifCount > 5orValue > 100: Status=" Bad"returnStatus,a[-3:]Else: Status="Good"returnStatus,a[-3:]Else:#IP = A[0].split () [2]Status ="the request cannot be found on the host"Log (IP+status)returnStatus,a[0]#output logs and print in consoledeflog (str): With open ('D:/dnslog.txt','a') as F:f.write (str+"\ n")Print(str)#Send maildefFaemail (msgtext): Msg= Mimetext (Msgtext,'Plain','Utf-8') msg[' from'] =from_addr msg[' to'] =', '. Join (TO_ADDR) msg['Subject'] = Header ('Line Warning','Utf-8'). Encode () server= Smtplib. Smtp_ssl (Smtp_server, 465) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, To_addr, msg.as_string ()) Server.quit ()if __name__=='__main__':#Continuous Monitoring whileTrue:Print('Parent process%s.'%os.getpid ()) Start=time.time () log (str (Datetime.datetime.now ()))#to create a process pool with the same size as the IP list, call the Ping methodp = Pool (3) forIinchRange (3): P.apply_async (Ping,args=(I,ip[i],f[i],))Print('waiting-subprocesses done ...') P.close () P.join ()#Get network status and message text forIinchRange (3): Status[i],s=Netstatus (F[i],ip[i]) mailtext[i]= Ip[i] +"". Join (s) forSinchStatus:Print(s)Print("". Join (Mailtext))#Select here to send mail when the first IP network is in poor conditionTry:ifStatus[0] = =" Bad": Faemail ("". Join (Mailtext))exceptException as E:log (E,": Message failed to send") End=time.time ()Print('runs%0.2f seconds.'% (End-start)) Log ("------------------------------------------------------------------")#time interval for each monitoringTime.sleep (10)
Concurrent Ping monitoring network status