The Script will send the patch reports to your email.
The combined log report will also be available in the following path in the device - "C:\ProgramData\log_report.txt"
The patch report will also be available in the following path in the device - "C:\ProgramData\patch_report.txt"
You must run the script as a system user. Note: Provide only the Tomail address to which mail to be sent: emailto=['abc@yopmail.com'] # Provide an Toemail address where the report need to be sent.You can also provide any number of To eamil address For example: ['tamil@yopmail.com','sensor@yopmail.com']
#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
emailto=['XXXX@YYYY.com'] # Provide an Toemail address where the report need to be sent.You can also provide any number of To eamil address For example: ['tamil@yopmail.com','sensor@yopmail.com']
emailfrom='XXXXXX@gmail.com'
password='CXXXXX@123'
smtpserver='smtp.gmail.com'
port=587
sendmail=1 ## [1 sends mail or 0 does not send mail] if sendmail=1 then user have to set the required information to send out a email from the code.
msgbody=r'''Hi
Please find the attachment for the Endpoints Third Party Patch Logs in Text file format.
Thank you.'''
si=[]
e=[]
b=[]
d="Rmm_dll"
import os
import re
import shutil
import random
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
logreport=os.path.join(os.environ['ProgramData'],'log_report.txt')
patchreport=os.path.join(os.environ['ProgramData'],'patch_report.txt')
if (os.path.exists(logreport) & os.path.exists(patchreport)):
os.remove(logreport)
os.remove(patchreport)
print "File Successfully Deleted!!"
else:
print "No Such Files Exist!!"
def computername():
return os.environ['COMPUTERNAME']
def ipaddress():
return socket.gethostbyname(socket.gethostname())
def emailreport(subject, emailto,emailfrom,fileToSend,password,smtpserver,port,msgbody):
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = ",".join(emailto)
msg["Subject"] = subject
msg.preamble = subject
body = MIMEText(msgbody)
msg.attach(body)
with open(fileToSend, 'rb') as fp:
record = MIMEBase('text', '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
def getrmm(a):
os.chdir(a)
c=os.popen("dir").read()
find=re.findall('RmmService.?\d?.log',c,re.MULTILINE)
ds=os.path.join(os.environ['ProgramData'],'log_report.txt')
if find:
count = 0
outfh= open(ds, 'a')
for f in find:
sc=os.path.join(a,f)
infh = open(sc,'r')
shutil.copyfileobj(infh, outfh)
outfh.write("\n")
infh.close()
outfh.close()
else:
print 'There is no RMM LOGS'
return ds
def getpatchlogs(b):
copying = True
patch_rep=os.path.join(os.environ['ProgramData'],'patch_report.txt')
outfh= open(patch_rep,'a')
with open (b, "r") as infh:
infh= iter(infh)
for line in infh:
if copying:
if (line.startswith(' scanStatus: SCAN_STARTED') or line.startswith(' scanType: COMPLETE_OS_PATCH') or line.startswith(' scanType: MISSING_OS_PATCH') or line.startswith(' scanType: INSTALLED_OS_PATCH') or line.startswith(' osPatchDataList {') or line.startswith('procedures {') or line.startswith('procedureResult {') or line.startswith('inventoryScanReply {') or line.startswith(' softwareMetaData {') or line.startswith( ' msgProcedureSet {')):
copying = False
elif "ComponentKB: " in line:
copying = False
elif "=======INSTALLED OS PATCHES======" in line:
copying = False
elif "=======MISSED OS PATCHES======" in line:
copying = False
elif "msgMonitorTaskList {" in line:
copying = False
elif "msgScheduledTaskList {" in line:
copying = False
else:
outfh.write(line)
elif (line.startswith(' scanStatus: SCAN_FINISHED_SUCCESS') or line.startswith(' scanResult: "Patch Scan completed succesfully."') or line.startswith(' scanResult: "Third Patch Scan completed succesfully."') or line.startswith(' repeat: NEVER') or line.startswith(' timeZone: \".*\"')or line.startswith(' canRequestUserInput:') or line.startswith(' endTimeSettings {' ) or line.startswith(' autoRemediationState:') or line.startswith(' procedureType: SCHEDULED') or line.startswith(' previousVersion:')):
line = infh.next()
copying = True
outfh.close()
recent_log= pmlpath+'\\RmmService.log'
last_scan_start_line_no=0
last_scan_end_line_no=0
with open(recent_log, 'r') as f:
start_line_numbers = [n for n, line in enumerate(f,1) if re.search(' scanStatus: SCAN_STARTED', line) or re.search('inventoryScanReply {',line) or re.search('inventoryScan {',line) or re.search('scanReply {',line)]
l=len(start_line_numbers)
if l!=0:
last_scan_start_line_no=start_line_numbers[l-1]
f.close()
with open(recent_log, 'r') as f:
end_line_numbers = [m for m, lines in enumerate(f,1) if re.search(' scanResult: "Third Patch Scan completed succesfully."', lines) or re.search('.*Third Patch scan finished\.', lines)]
l=len(end_line_numbers)
if l!=0:
last_scan_end_line_no= end_line_numbers[l-1]
f.close()
if (last_scan_start_line_no & last_scan_end_line_no):
if last_scan_end_line_no > last_scan_start_line_no:
with open(recent_log, 'r') as f:
scan_data= f.readlines()
scan_data= scan_data[last_scan_start_line_no-2:last_scan_end_line_no]
out = open(patch_rep,'r+')
lines = out.readlines()
out.seek(0)
out.write("Recent Scan Report:\n\n")
for lines in scan_data:
out.write(lines)
out.write("\n\n Other Operations:\n\n")
for line in lines:
out.write(line)
out.close()
f.close()
return patch_rep
if 'PROGRAMFILES(X86)' in os.environ.keys():
pmlpath=os.path.join(os.environ['PROGRAMFILES(X86)'], 'COMODO\\Endpoint Manager\\rmmlogs')
else:
pmlpath=os.path.join(os.environ['PROGRAMFILES'], 'COMODO\\Endpoint Manager\\rmmlogs')
log= getrmm(pmlpath)
s= getpatchlogs(log)
print "Please find the Combined Log Report in the path: "+log
print "Please find the Patch Log Report in the path: "+s
subject='%s %s Patch Logs Report'%(computername(), ipaddress())
fileToSend=s
if sendmail==1:
print emailreport(subject,emailto,emailfrom,fileToSend,password,smtpserver,port,msgbody)
print 'mail sent'
else:
print " Set sendmail value as 1 "
Comments