pingip=r"CHANGE_ME"  #Provide the domain name or IP address to ping
Send_Email=1         #Provide 1 if you want to send ping status report as an mail or Provide 0 if you don't want to send an mail. 
Receiver = "CHANGE_ME"  ## Provide an Toemail address where the mail need to be sent. the datatype should be a string.
Sender = "CHANGE_ME"  ## Provide the From Email address from which the mail to be send. the datatype should be a string.
Password = "CHANGE_ME"               ##Provide password for from email. the datatype should be a string.                  
import ctypes
import os,re,sys
import _winreg,difflib,filecmp
import smtplib
import mimetypes
import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
fileToSend=os.path.join(os.environ['TEMP'], 'report.txt')
def gmail(sender_email,password,receiver,file):
    fileToSend = file 
    msg = MIMEMultipart()
    msg["From"] = sender_email
    msg["To"] = receiver
    msg["Subject"] = "PING STATUS FOR THE IP %s"%(pingip)
    
    ctype, encoding = mimetypes.guess_type(fileToSend)
    if ctype is None or encoding is not None:
        ctype = "application/octet-stream"

    maintype, subtype = ctype.split("/", 1)

    if maintype == "text":
        cp = open(fileToSend)
        attachment = MIMEText(cp.read(), _subtype=subtype)
        cp.close()
    elif maintype == "image":
        cp = open(fileToSend, "rb")
        attachment = MIMEImage(cp.read(), _subtype=subtype)
        cp.close()
    elif maintype == "audio":
        cp = open(fileToSend, "rb")
        attachment = MIMEAudio(cp.read(), _subtype=subtype)
        cp.close()
    else:
        cp = open(fileToSend, "rb")
        attachment = MIMEBase(maintype, subtype)
        attachment.set_payload(cp.read())
        cp.close()
        encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
    msg.attach(attachment)
    server = smtplib.SMTP("smtp.gmail.com:587")
    server.starttls()
    server.login(sender_email,password)
    server.sendmail(sender_email, receiver, msg.as_string())
    server.quit()
    
def Download(URL, DownloadTo = None, FileName = None):
    import urllib
    import ssl
    if FileName:
        FileName = FileName
    else:
        FileName = URL.split('/')[-1]
        
    if DownloadTo:
        DownloadTo = DownloadTo
    else:
        DownloadTo = os.path.join(os.environ['TEMP'])
    os.chdir(DownloadTo)
    DF=os.path.join(os.environ['TEMP'],FileName)
    with open(FileName, 'wb') as f:
        try:
            context = ssl._create_unverified_context()
            f.write(urllib.urlopen(URL,context=context).read())
        except:
            f.write(urllib.urlopen(URL).read())
    if os.path.isfile(DF):
        return DF
    else:
        return False

    
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

command="ping  "+pingip+" >C:\pingstatus.txt"
with disable_file_system_redirection():
    os.popen(command).read()
    average=os.popen('findstr "Average" C:\pingstatus.txt').read()
if average:
    print "ping status of "+pingip+" is "+ average.strip()
    with open(fileToSend, 'w') as f:
        f.write("The ping status for the domain name or ip given by the user  ")
        f.write(pingip)
        f.write("\n")
        f.write(average)
else:
    print "ping status of "+pingip+" is "+" request timed out - failed"
    with open(fileToSend, 'w') as f:
        f.write("The ping status for the domain name or ip given by the user  ")
        f.write(pingip)
        f.write("\n")
        f.write("ping status request is timed out - failed")

HOMEPATH = r"C:\Program Files (x86)"
if os.path.exists(HOMEPATH):
        HOMEPATH = r"C:\Program Files (x86)"
else:
    HOMEPATH =r"C:\Program Files"

DEST= os.path.join(HOMEPATH,r'ITarian\Endpoint Manager\Lib\site-packages')

if Send_Email==1:
    gmail(Sender,Password,Receiver,fileToSend)
    