12.08.2016 г.

Тест дали има пинг до масхина и изпращане на email при падане.

       

# -*- coding: utf-8 -*-

#IMPORT SOMTHING MODULES########################  
import os, subprocess, time                   ##
from sys import platform                      ##
import smtplib                                ##
################################################  


#HARDCORE CONST'S#########################################
PING=''                                                 ##
TARGET = '192.168.1.9'                                  ##
MASHINE = ' '                                           ## 
TAKE_STATUS = []                                        ##
TIME_TO_SLEEP = time.sleep(10)                          ##
sender = 'email address'                                ##
receivers = ['korea60@abv']                             ## 
message = """From: From Server - name 
To: To Person         
Subject: IP is down in {}
""".format(time.asctime(time.localtime(time.time())))
##########################################################

#DEFINE FUNCTION'S IS HERE

#1 TEST MASHINE VERSION
def check_mashine():
    global MASHINE
    global PING
    if platform.startswith('lin'):
        MASHINE = ' -c1'
        PING='/bin/ping'
    else:
        MASHINE = ' -n1'
        PING='ping'

#2 PING TARGET MASHINE AND COLLECT STATUS
def check_ping():
    global TAKE_STATUS
    check_mashine()
    RESPONSE = os.system("{}{} {} > /dev/null".format(PING,MASHINE,TARGET))
    if RESPONSE:
        TAKE_STATUS.append("1")
    else:
        pass


#3 SEND EMAIL
def send_emain():
    mailserver = smtplib.SMTP('smtp.gmail.com:587')
    # Identifikaciq
    mailserver.ehlo()
    # secure our email
    mailserver.starttls()
    # re-identify ourselves as an encrypted connection
    mailserver.ehlo()
    mailserver.login('username is here!!!!', 'password is here !!!!')
    mailserver.sendmail('from','to',message)
    #ZATVARQME SYRVARA
    mailserver.quit()

   

#TAKE THE MAGIC!!!
if __name__ == '__main__':
    while len(TAKE_STATUS) < 5:
        check_ping()
        TIME_TO_SLEEP
        send_emain()
    print ("MAIL SEND")



       
 

Няма коментари :

Публикуване на коментар