diff --git a/Jamf Pro/Extension Attributes/jamf_ea_LicensedFonts.py b/Jamf Pro/Extension Attributes/jamf_ea_LicensedFonts.py index ba72cf8..0cbc6a8 100644 --- a/Jamf Pro/Extension Attributes/jamf_ea_LicensedFonts.py +++ b/Jamf Pro/Extension Attributes/jamf_ea_LicensedFonts.py @@ -3,7 +3,7 @@ ################################################################################################### # Script Name: jamf_ea_LicensedFonts.py # By: Zack Thompson / Created: 1/12/2019 -# Version: 1.1.0 / Updated: 10/21/2020 / By: ZT +# Version: 1.2.0 / Updated: 2/18/2021 / By: ZT # # Description: A Jamf Extension Attribute to check if any Licensed Fonts are installed. # @@ -11,65 +11,90 @@ import re import os +import shlex import subprocess def runUtility(command): - """A helper function for subprocess. - Args: - command: List containing command and arguments in a list - Returns: - stdout: output of the command """ - try: - process = subprocess.check_output(command) - except subprocess.CalledProcessError as error: - print ('return code = ', error.returncode) - print ('result = ', error) + A helper function for subprocess. - return process - -def check(dir): - """Provide a directory and the files within are checked using regex against the string. Args: - dir: A directory path + command: The command line level syntax that would be written in shell or a terminal window. (str) + Returns: + Results in a dictionary. + """ + + # Validate that command is not a string + if not isinstance(command, str): + raise TypeError('Command must be a str type') + + # Format the command + command = shlex.split(command) + + # Run the command + process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, universal_newlines=True ) + (stdout, stderr) = process.communicate() + + result_dict = { + "stdout": (stdout).strip(), + "stderr": (stderr).strip() if stderr != None else None, + "status": process.returncode, + "success": True if process.returncode == 0 else False + } + + return result_dict + + +def check(path): + """ + Provide a directory and the files within are checked using regex against the string. + + Args: + path: A directory path Returns: count: an [int] of files matching the string """ - count = 0 + count = 0 try: + # Get all the files in the directory. - files = os.listdir(dir) + files = os.listdir(path) # Loop over the files. - for file in files: + for afile in files: + # Check the file via regex and ignore case. if re.search(r'(Name_or_Prefix_of_Font_Here)', file, flags=re.IGNORECASE): # Substitute "Name_or_Prefix_of_Font_Here" with the font you're looking for. count += 1 + except: pass return count + def main(): # Define Variables - cmd_all_Users = ['/usr/bin/dscl', '.', 'list', '/Users'] + cmd_all_Users = "/usr/bin/dscl . list /Users" home_directories = [] - system_accounts = ['daemon', 'Guest', 'nobody', 'root'] # Add your Jamf Management Account to this dictionary. + system_accounts = ['cas', 'cascom', 'daemon', 'Guest', 'nobody', 'root'] user_Font_Count = 0 # Get a list of all user accounts on this system. - all_users = (runUtility(cmd_all_Users)).split() + all_users = (runUtility(cmd_all_Users))["stdout"].split() # Loop over each user. for user in all_users: + # Ignore any system accounts or known ignorable accounts. if ( user[0] != '_' ) and ( user not in system_accounts ): + # Get the home directory of each user account. - cmd_Home_Directory = ['/usr/bin/dscl', '.', 'read', '/Users/' + user, 'NFSHomeDirectory'] - home_directory = ((runUtility(cmd_Home_Directory)).replace('NFSHomeDirectory: ', '')).strip() + cmd_Home_Directory = "/usr/bin/dscl . read /Users/{} NFSHomeDirectory".format(user) + home_directory = ((runUtility(cmd_Home_Directory))["stdout"].replace('NFSHomeDirectory: ', '')).strip() home_directories.append(home_directory) # Check the System Fonts folder (aka available to all users). @@ -77,6 +102,7 @@ def main(): # Loop over each home directory. for directory in home_directories: + # Calculate if the local accounts have matching fonts. user_Font_Count = user_Font_Count + check(directory + "/Library/Fonts") @@ -89,5 +115,6 @@ def main(): else: print ("Installed") + if __name__ == "__main__": main()