Last active
May 11, 2022 22:14
-
-
Save spokanemac/ef7be96fc4501e03fb7590cbc6ce5101 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# A script to automate Watchman Monitoring's run plugin and zipping the resulting files | |
# Jack-Daniyel Strong | |
# 28-Feb-2017 | |
# | |
# Import Libraries | |
import sys, os, zipfile, signal | |
from datetime import datetime | |
# Defined Variables | |
pathMC = '/Library/MonitoringClient/' | |
pathMCplugins = pathMC + 'Plugins/' | |
pathMCrunplugin = pathMC + 'RunPlugin' | |
now = datetime.now() # get the current date and time | |
timestamp = now.strftime("%Y%m%d_%H%M") | |
zippath = os.path.join('/tmp/', "%s_support.zip" % timestamp) | |
# Handle if SIGINT is given so folks can't get in the weeds canceling sudocheck | |
def signal_handler(signal, frame): | |
print('SIGINT: You pressed Ctrl+C!') | |
sys.exit(0) | |
signal.signal(signal.SIGINT, signal_handler) | |
# We need to see if you can be root | |
print "\n\n" + 'Hi There! Are you an admin user? Please enter your password below:' | |
sudocheck = str(os.system('sudo -v')) | |
if 'Sorry' in sudocheck: | |
print "You must have root privileges to run " + sys.argv[0] | |
exit(1) | |
## promptUser | |
## Prompts User with passed question | |
## Optional default value can be passed. | |
## Loops until provided a non-null response if no default | |
## RETURNS provided result | |
def promptUser(prompt,default=''): | |
response = '' | |
# Convert prompt to string and state default if provided | |
cleanprompt = str(prompt) | |
if str(default): | |
cleanprompt += ' (' + str(default) + ')' | |
cleanprompt += ': ' | |
py3 = sys.version_info[0] > 2 #creates boolean value for test that Python major version > 2 | |
while (not str(response)): # While response is not null | |
if py3: | |
response = input(cleanprompt) | |
else: | |
response = raw_input(cleanprompt) | |
if not str(response) and str(default): | |
response = default | |
return response | |
## end promptUser | |
## pluginList | |
## Builds an array of plugins (lowercase/sorted) and strips 'check_' | |
def pluginList(): | |
plugins = [] # Define array | |
for file in os.listdir(pathMCplugins): # Loop file list | |
if file.endswith(".plist"): | |
plugins.append(file[6:].lower()) # Strip off 'check_' | |
# end loop | |
plugins.sort() | |
return plugins | |
## end pluginList | |
## createZip | |
## creates a zip file | |
def createZip(text, pluginName = 'plugin', path = '/Library/MonitoringClient'): | |
zf = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED) | |
zf.writestr('/' + pluginName[:-6] + '_debug.txt', text) | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
zf.write(os.path.join(root, file)) | |
zf.close() | |
## end createZip | |
## Let's do something! | |
def main(): | |
whichPlugin = '' | |
inlist = '' | |
pl = pluginList() | |
if str(sys.argv[1]): # if we were passed a unique plugin name | |
whichPlugin = str(sys.argv[1]) | |
while (len(inlist) != 1): # while we find only one plugin | |
inlist = [s for s in pl if whichPlugin in s] | |
if len(inlist) > 1: | |
print 'Multiple matches:' | |
for each in inlist: | |
print ' ' + each | |
if (len(inlist) != 1): | |
whichPlugin = promptUser('Which plugin?') | |
whichPlugin = inlist[0] # single string variable for clarity | |
print "\n" + 'Running Plugin Test for ' + whichPlugin | |
# run the plugin and send the result to create a text file | |
createZip(str(os.popen('sudo ' + pathMCrunplugin + ' ' + whichPlugin).read()),whichPlugin) | |
print "\n" + 'Zip Archive Complete.' | |
# This isn't kosher cross platform, probably needs to use shutil.move() | |
os.system('mv ' + zippath + ' ~/Desktop/' + os.path.basename(zippath)) | |
print "\n\n" + 'You will find "' + os.path.basename(zippath) + '" on your Desktop.' | |
print "\n\n" + 'Please send as an attachment in a reply to your ticket, or via our notes form:' + "\n" | |
print 'https://www.ourremotesupport.com/notes/' + "\n\n" | |
os.system('open ~/Desktop') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment