Created
June 29, 2021 05:36
-
-
Save nirbhabbarat/07ef8f830cd7359b5dc54580a729c11a to your computer and use it in GitHub Desktop.
Search ec2 by private_ip
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
from collections import defaultdict | |
import boto3 | |
""" | |
A tool for retrieving basic information from the running EC2 instances. | |
""" | |
# Connect to EC2 | |
ec2 = boto3.resource('ec2') | |
# Get information for all running instances | |
running_instances = ec2.instances.filter(Filters=[{ | |
'Name': 'private-ip-address', | |
'Values': ['xxxxxx', 'xxxxxx']}]) | |
ec2info = defaultdict() | |
for instance in running_instances: | |
for tag in instance.tags: | |
if 'Name'in tag['Key']: | |
name = tag['Value'] | |
# Add instance info to a dictionary | |
ec2info[instance.id] = { | |
'Name': name, | |
'Type': instance.instance_type, | |
'State': instance.state['Name'], | |
'Private IP': instance.private_ip_address | |
} | |
attributes = ['Name', 'Type', 'State', 'Private IP'] | |
for instance_id, instance in ec2info.items(): | |
for key in attributes: | |
print("{0}: {1}".format(key, instance[key])) | |
print("------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment