Thứ Hai, 5 tháng 5, 2014

check IP with Python

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

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

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

        fromAddr ='hoangtienthanh@gmail.com'
        toAddr = 'thanhht@ssgroup.com.vn'

        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 3 ' + ip + ' &> /dev/null')
           fr = open(ip,"r")
           str = fr.read(1)
           fr.close()
    if ISP == "123.30.173.189":
        ISP = "VNPT Internet"
    elif ISP == "123.30.173.190":
        ISP = "VNPT WAN"
    elif ISP == "192.168.10.100":
        ISP = "FPT Internet"
    elif ISP == "192.168.10.101":
        ISP = "FPT WAN"
           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

# 173.189: VNPT Internet
# 173.190: VNPT WAN
# 10.100 : FPT Internet
# 10.101 : FPT WAN
ip2Check = ['123.30.173.189','123.30.173.190','192.168.10.100','192.168.10.101']

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

 Note: please, begin, create file name 123.30.173.189, 123.30.173.190, 192.168.10.100 , 192.168.10.101 with content 0 (down) or 1 (up)

Không có nhận xét nào:

Đăng nhận xét