def searchDirForMAC()

in vmassist/linux/vmassist.py [0:0]


def searchDirForMAC(dirToSearch, returnDict, okMAC):
  # return all MACs found defined in some config file in the passed in directory
  #   We'll add each file, and the MAC(s) found in there, to the passed in dict
  # accept an 'ok' MAC definition, because it would be ok for cloud-init managed
  #   configs to have a mac defined - CI will reset the configs if the mac changes
  # Define a MAC address regex pattern (e.g., 00:1A:2B:3C:4D:5E or 00-1A-2B-3C-4D-5E)
  mac_pattern = re.compile(r'([0-9a-f]{2}(?::[0-9a-f]{2}){5})', re.IGNORECASE)
  # Walk through all files in the directory
  for root, _, files in os.walk(dirToSearch):
    # loop through all files in the dirToSearch
    for file_name in files:
      file_path = os.path.join(root, file_name)
      # Check if it's a regular file (skip links, sockets, pipes, etc.)
      if os.path.isfile(file_path):
        try:
          with open(file_path, 'r') as file:
            content = file.read()
            # Find all MAC addresses in the file
            mac_addresses = mac_pattern.findall(content)
            # If MAC addresses are found, and not the passed in okMAC, add them to the dictionary
            # mac_addresses as returned from findall will be an array of string(s)
            if ( mac_addresses and (okMAC not in mac_addresses) ):
              returnDict[file_path] = mac_addresses
        except (UnicodeDecodeError, PermissionError):
          # Skip files that can't be read due to encoding or permission issues
          # just create the exception code block, but we won't be using it
          pass