Chủ Nhật, 8 tháng 2, 2015

Check IDS

#!/usr/bin/python
import os, smtplib
from threading import Thread

def sendmail(subject):
        "Sends an email to the specified recipent "
        username = 'checkipwingame@gmail.com'
        password = 'xxxxx'

        server = smtplib.SMTP('smtp.gmail.com:25')
        server.starttls()
        server.login(username, password)

        fromAddr ='checkipwingame@gmail.com'
        toAddr = 'cuvanhai@gmail.com'

        header = 'To: ' + toAddr + '\n' + 'From: ' + fromAddr + '\n' + 'Subject: ' + subject + '\n'
        msg = header + '\nCheck it now!\n'

        server.sendmail(fromAddr, toAddr, msg)
        server.close()

def checkIpDown(ip):
        ISP = ip
        ret = os.system('ping -c 4 ' + ip + ' &> /dev/null')
        fr = open(ip,"r")
        str = fr.read(1)
        fr.close()
        if ISP == "118.70.183.107":
                ISP = "118.70.183.107"

        if ret != 0 and str == "1":
                message = ISP+' has been DOWN'
                sendmail(message)
                fo1 = open(ip,"w")
                fo1.write("0")
                fo1.close()
                print message
        elif (ret == 0 and str == "0") :
               message = ISP+' has been UP'
               sendmail(message)
               fo2 = open(ip,"w")
               fo2.write("1")
               fo2.close()
               print message
        elif (ret != 0 and str == "0") :
                print ISP, "network not connect" # Co the xoa dong nay


ip2Check = ['118.70.183.107']

for ip in ip2Check:
        t = Thread(target=checkIpDown, args=(ip,))
        t.start()