Created
May 12, 2021 19:56
-
-
Save jcaxmacher/307d58310a1c7f7ff66dd6caf26be5e8 to your computer and use it in GitHub Desktop.
get list of patches present on EC2 instance
This file contains hidden or 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
import csv | |
import boto3 | |
ec2 = boto3.client('ec2', region_name='us-west-1') | |
ssm = boto3.client('ssm', region_name='us-west-1') | |
response = ec2.describe_instances( | |
Filters=[ | |
{'Name':'tag:workload','Values':['somethin']}, | |
{'Name':'tag:environment','Values':['prod']} | |
] | |
) | |
instances = [] | |
for reservation in response['Reservations']: | |
for inst in reservation['Instances']: | |
instances.append({ | |
'instance_id': inst['InstanceId'], | |
'tags': inst['Tags'] | |
}) | |
for instance in instances: | |
print(f'Querying instance {instance["instance_id"]}') | |
paginator = ssm.get_paginator('describe_instance_patches') | |
instance['patches'] = [] | |
for result in paginator.paginate( | |
InstanceId=instance['instance_id'] | |
): | |
instance['patches'].extend(result['Patches']) | |
for instance in instances: | |
instance['patches'] = [p for p in instance['patches'] if p['State'] == 'Missing'] | |
results = [] | |
for instance in instances: | |
for patch in instance['patches']: | |
results.append([instance['instance_id'], patch['Title'], patch['KBId']]) | |
with open('results.csv', 'w') as f: | |
cw = csv.writer(f) | |
cw.writerows(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment