FTPorSFTPserver = 1 #give 0 for normal FTP server. give 1 for SFTP server
hostname = "CHANGE ME" # give here the hostname for FTP SERVER OR SFTP SERVER ex: sftp://c1files.company.com
username = "CHANGE ME" # give here the username for FTP SERVER OR SFTP SERVER
password = "CHANGE ME" # give here the password for FTP SERVER OR SFTP SERVER
dirpath = "CHANGE ME"  # give here the directory path where do you want to save the CCSMlogs for example: dirpath = 'CCS/Logscollections'

import subprocess
import os
from ftplib import FTP

devicename = os.popen("hostname").read().split("-")[0]
filename = devicename+"_CCSMlogs.zip"

def uploadingfileFTP(hostname,username,password,dirpath,filepath,filename):
	ftp = FTP()
	ftp.connect(hostname)
	ftp.login(username,password)
	ftp.cwd(dirpath)
	ftp.storbinary("STOR "+filename, open(filepath,'rb'))
	ftp.quit()
	print("successfully uploaded the file to the FTP SERVER")
		

bashScript = """if   [ "$1" == "--enableLogs" ]; then
	sudo defaults write com.comodo.FileAccessDaemon logLevel 6    
	echo "CCSM extended logs were enabled, please restart the endpoint"
elif [ "$1" == "--collectLogs" ]; then
	echo "Creating  /Library/Caches/CCSMlogs temporary directory"
	mkdir /Library/Caches/CCSMlogs

	echo "Listing Comodo Files to /Library/Caches/CCSMFileList.txt"

	echo "/Applications/Comodo/CCS/" >> /Library/Caches/CCSMFileList.txt
	ls '/Applications/Comodo/CCS/' >> /Library/Caches/CCSMFileList.txt
	echo "" >> /Library/Caches/CCSMFileList.txt

	echo "/Library/Application Support/Comodo/CCS" >> /Library/Caches/CCSMFileList.txt
	ls '/Library/Application Support/Comodo/CCS'  >> /Library/Caches/CCSMFileList.txt
	echo "" >> /Library/Caches/CCSMFileList.txt

	echo "/Library/LaunchDaemons/" >> /Library/Caches/CCSMFileList.txt
	ls '/Library/LaunchDaemons/'  >> /Library/Caches/CCSMFileList.txt
	echo "" >> /Library/Caches/CCSMFileList.txt

	echo "/Library/LaunchAgents/" >> /Library/Caches/CCSMFileList.txt
	ls '/Library/LaunchAgents/'  >> /Library/Caches/CCSMFileList.txt
	echo "" >> /Library/Caches/CCSMFileList.txt

	echo "/Library/Extensions/" >> /Library/Caches/CCSMFileList.txt
	ls '/Library/Extensions/'  >> /Library/Caches/CCSMFileList.txt
	echo "" >> /Library/Caches/CCSMFileList.txt
	
	echo "Collecting needed CCSM logs"
	echo "Creating directories in /Library/Caches"
	mkdir /Library/Caches/CCSMlogs/Logs
	mkdir /Library/Caches/CCSMlogs/SystemLogs
	mkdir /Library/Caches/CCSMlogs/DiagnosticReports

	echo "Gathering logs to /Library/Caches/CCSMlogs"
	cp -a /Library/Caches/CCSMFileList.txt /Library/Caches/CCSMlogs/
	rm /Library/Caches/CCSMFileList.txt
	cp -a '/var/log/'. /Library/Caches/CCSMlogs/SystemLogs/
	cp -a '/Library/Application Support/COMODO/CCS/Logs/'. /Library/Caches/CCSMlogs/Logs/
	cp -a '/Library/Logs/DiagnosticReports/'. /Library/Caches/CCSMlogs/DiagnosticReports/
	cp -a '/Library/Application Support/Comodo/CCS/settings.plist' /Library/Caches/CCSMlogs/Logs/

	echo "Zipping collected logs"
	zip -r /Library/Caches/%s /Library/Caches/CCSMlogs -x "*.DS_Store"
	rm -r /Library/Caches/CCSMlogs	
else
    echo "Please enter correct parameter"
fi"""%(filename)

SftpCommands = """spawn sftp %s@%s
expect "password:"
send "%s\\n"
expect "sftp>"
send "cd %s\\n"
expect "sftp>"
send "put /Library/Caches/%s\\n"
expect "sftp>"
send "exit\\n"
interact
"""%(username,hostname.split("//")[1],password,dirpath,filename)

def execute(arguments):
    p = subprocess.Popen(arguments, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
    output, error = p.communicate()
    if output is not None and output != '':
        print output

path = "/Library/Caches/CollectCCSMlogs.sh"
path1 = "/Library/Caches/sftpcommands.sh"

with open(path,'wb') as f:
    f.write(bashScript)

with open(path1,'wb') as f:
    f.write(SftpCommands)

execute("sh %s --collectLogs"%(path))
filepath = '/Library/Caches/%s'%(filename)
if FTPorSFTPserver==0:
    uploadingfileFTP(hostname,username,password,dirpath,filepath,filename)
else:
    execute("ssh -o StrictHostKeyChecking=no %s@%s"%(username,hostname.split("//")[1]))
    execute("expect %s"%(path1))
os.remove(path)
os.remove(filepath)
os.remove(path1)