#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
emailto=itsm.getParameter('EmailTo')  ## Provide an Toemail address where the mail need to be sent.You can also provide any number of To eamil address For example: ['varun@yopmail.com', xxxx@yopmail.com]
emailfrom=itsm.getParameter('EmailFrom')  ## Provide the From Email address from which the mail to be send
password=itsm.getParameter('Password')               ##Provide password for from email
smtpserver='smtp.gmail.com'
port=587

import os,shutil,re,sys,socket,_winreg,random,getpass
from datetime import date, datetime
import socket
import smtplib
import mimetypes
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
from email.mime.text import MIMEText
from subprocess import PIPE, Popen

def computername():
    return os.environ['COMPUTERNAME']

def ipaddress():
    return socket.gethostbyname(socket.gethostname())

subject='%s %s Alert : Network Scan Report!!!!!'%(computername(), ipaddress())		

def emailreport(subject, emailto,emailfrom,password,smtpserver,port,fileToSend):
    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = ",".join(emailto)
    msg["Subject"] = subject
    msg.preamble = subject
    with open(fileToSend) as fp:
        record = MIMEBase('application', 'octet-stream')
        record.set_payload(fp.read())
        encoders.encode_base64(record)
        record.add_header('Content-Disposition', 'attachment',
        filename=os.path.basename(fileToSend))
        msg.attach(record)
    try:
        server = smtplib.SMTP(smtpserver,port)
        server.ehlo()
        server.starttls()
        server.login(emailfrom, password)
        server.sendmail(emailfrom, emailto, msg.as_string())
        server.quit()
        return " \nThe email report has been sent to "+msg["To"]
    except Exception as e:
        return e

URL=r'http://www.nirsoft.net/utils/wakemeonlan.zip'
src_path=os.environ['TEMP']
print src_path

import zipfile
import ctypes
import sys
import platform
import _winreg
import ssl
import shutil
ssl._create_default_https_context = ssl._create_unverified_context


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)

def Download(src_path, URL):
    import urllib2
    import os
    print "Download started"
    fileName = 'Desktop_info.zip'
    fp = os.path.join(src_path, fileName)
    request = urllib2.Request(URL, headers={'User-Agent' : "Magic Browser"})
    parsed = urllib2.urlopen(request)
    if os.path.exists(src_path):
        print "Path already exists"
    if not os.path.exists(src_path):
        os.makedirs(src_path)
        print "Path created"
    with open(fp, 'wb') as f:
        while True:
            chunk=parsed.read(100*1000*1000)
            if chunk:
                f.write(chunk)
            else:
                break
    print "The file downloaded successfully in specified path"
    return fp

zip_path=Download(src_path, URL)
print zip_path
file_zip=os.environ['PROGRAMDATA']
file_zip1=file_zip+r'\info'

def filezip(zip_path,file_zip):
    with disable_file_system_redirection():
        with zipfile.ZipFile(zip_path,"r") as zip_ref:
            zip_ref.extractall(file_zip1)
            print 'file unzipped to ' +file_zip1

filezip(zip_path,file_zip1)
c="C:\ProgramData\info\WakeMeOnLan.exe"
file_zip1="C:\ProgramData\info"
if os.path.exists(c):
    os.chdir(file_zip1)
    file_path=os.path.join(os.environ['ProgramData'], 'report.csv')
    command = 'WakeMeOnLan.exe /scan /stab '+file_path
    a=os.popen(command).read()
    print file_path
    if os.path.isfile(file_path):
        print emailreport(subject,emailto,emailfrom,password,smtpserver,port,file_path)
