Skip to content

Instantly share code, notes, and snippets.

@waiyanwh
Created November 15, 2023 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waiyanwh/2596688f10a6cd20f28887dccb7b82e8 to your computer and use it in GitHub Desktop.
Save waiyanwh/2596688f10a6cd20f28887dccb7b82e8 to your computer and use it in GitHub Desktop.
To list all elastic network interface to get more info
#!/usr/bin/python3
import boto3
import csv
from prettytable import PrettyTable
# Input all regions that you used in your aws accounts
regions = ['us-east-1', 'ap-southeast-1', 'ap-northeast-1']
all_enis = []
for region in regions:
ec2 = boto3.client('ec2', region_name=region)
enis = ec2.describe_network_interfaces()['NetworkInterfaces']
all_enis.extend(enis)
def get_eni_info():
table = PrettyTable(['REGION', 'ENI ID', 'Type', 'Public IP', 'Description', 'Instance ID'])
enis = ec2.describe_network_interfaces()['NetworkInterfaces']
for eni in all_enis:
eni_id = eni['NetworkInterfaceId']
public_ip = eni['Association']['PublicIp'] if 'Association' in eni and 'PublicIp' in eni['Association'] else '-'
description = eni['Description']
instance_id = eni['Attachment']['InstanceId'] if 'Attachment' in eni and 'InstanceId' in eni['Attachment'] else '-'
# Get ENI region
eni_region = ec2.describe_network_interfaces(
Filters=[{
'Name': 'network-interface-id',
'Values': [eni_id]
}]
)['NetworkInterfaces'][0]['AvailabilityZone'][:-1]
table.add_row([ eni_region , eni['NetworkInterfaceId'], eni['InterfaceType'], public_ip, description, instance_id])
print(table)
###############################################
# Uncomment below part if you want csv output #
###############################################
# with open('eni_info.csv', 'w', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(table.field_names)
# for row in table._rows:
# writer.writerow(row)
# print("Results exported to eni_info.csv")
if __name__ == '__main__':
get_eni_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment