How do I find IoT certs status?

I want to know how many certificates I have in my AWS IoT and how many certificates are attached onto any things or not.

import boto3, time
from concurrent.futures import ThreadPoolExecutor

client =boto3.client(“iot”)

results = []
attached = []
non_attached = []

paginator = client.get_paginator(‘list_certificates’)
page_iterator = paginator.paginate()
for page in page_iterator:
results.extend(page[‘certificates’])
print(“Total certificates scanned are: “, len(results))

start_time = time.time()
def check_certificates(certificate):
principal = client.list_principal_things(principal=certificate[“certificateArn”])
# print(“%s – number of things: %d” % (certificate[“certificateId”], len(principal[“things”])))
if len(principal[“things”])==0:
non_attached.append(certificate[“certificateArn”])
else:
attached.append(certificate[“certificateArn”])
with ThreadPoolExecutor(max_workers=10) as executor:
future = [executor.submit(check_certificates, item) for item in results]

print(“Certs have thing(s) attached: “, len(attached))
print(“Certs have NO thing(s) attached: “, len(non_attached))
print(“— %s seconds using MultiThreading—” % (time.time() – start_time))

No Comments

Leave a Reply