{
  "output": "import boto3\nimport csv\nfrom botocore.exceptions import ClientError, ProfileNotFound\nfrom aws_utils import setup_org_accounts_session, get_boto_session, export_to_sheets\n\n\n# Function to list public IPs, Private IPs, and Instance IDs\ndef list_ec2_instance_info(ec2_client):\n    \"\"\"\n    Lists the public IP, private IP, and Instance ID of EC2 instances.\n    Returns a list of dictionaries.\n    \"\"\"\n    instance_info = []\n    paginator = ec2_client.get_paginator(\"describe_instances\")\n    for page in paginator.paginate():\n        for reservation in page[\"Reservations\"]:\n            for instance in reservation[\"Instances\"]:\n                # Only process if it has a Public IP\n                if \"PublicIpAddress\" in instance:\n                    instance_info.append(\n                        {\n                            \"InstanceId\": instance[\"InstanceId\"],\n                            \"PublicIp\": instance[\"PublicIpAddress\"],\n                            \"PrivateIp\": instance.get(\"PrivateIpAddress\", \"N/A\"),\n                        }\n                    )\n    return instance_info\n\n\n# Function to list Elastic IPs (EIPs)\ndef list_elastic_ips(ec2_client):\n    elastic_ips = []\n    response = ec2_client.describe_addresses()\n    for address in response[\"Addresses\"]:\n        if \"PublicIp\" in address:\n            elastic_ips.append(address[\"PublicIp\"])\n    return elastic_ips\n\n\n# Function to list the DNS names of internet-facing ALBs\ndef list_alb_dns_names(elbv2_client):\n    alb_dns_names = []\n    response = elbv2_client.describe_load_balancers()\n    for lb in response[\"LoadBalancers\"]:\n        if lb[\"Scheme\"] == \"internet-facing\":\n            alb_dns_names.append(lb[\"DNSName\"])\n    return alb_dns_names\n\n\ndef main():\n    get_boto_session()\n    regions_to_check = [\"eu-west-1\", \"eu-west-2\"]\n    rows_for_sheets = []\n\n    # Open CSV file for writing\n    with open(\"aws_external_ips_by_region.csv\", mode=\"w\", newline=\"\") as csvfile:\n        fieldnames = [\n            \"AccountName\",\n            \"AccountID\",\n            \"Region\",\n            \"Type\",\n            \"Instance ID\",\n            \"Public IP/DNS Name\",\n            \"Private IP\",\n            \"Private Hostname\",\n        ]\n        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n        writer.writeheader()\n\n        # Iterate through active accounts and then through the regions\n        for account, profile_name in setup_org_accounts_session():\n            account_id = account[\"Id\"]\n            account_name = account[\"Name\"]\n            try:\n                boto3.setup_default_session(profile_name=profile_name)\n\n                for region in regions_to_check:\n                    try:\n                        ec2_client = boto3.client(\"ec2\", region_name=region)\n                        elbv2_client = boto3.client(\"elbv2\", region_name=region)\n\n                        # Process EC2 Instances\n                        ec2_info = list_ec2_instance_info(ec2_client)\n                        for item in ec2_info:\n                            row = {\n                                \"AccountName\": account_name,\n                                \"AccountID\": account_id,\n                                \"Region\": region,\n                                \"Type\": \"EC2\",\n                                \"Instance ID\": item[\"InstanceId\"],\n                                \"Public IP/DNS Name\": item[\"PublicIp\"],\n                                \"Private IP\": item[\"PrivateIp\"],\n                                \"Private Hostname\": \"N/A\",\n                            }\n                            writer.writerow(row)\n                            rows_for_sheets.append(list(row.values()))\n\n                        # Process EIPs\n                        eips = list_elastic_ips(ec2_client)\n                        for ip in eips:\n                            row = {\n                                \"AccountName\": account_name,\n                                \"AccountID\": account_id,\n                                \"Region\": region,\n                                \"Type\": \"EIP\",\n                                \"Instance ID\": \"N/A\",\n                                \"Public IP/DNS Name\": ip,\n                                \"Private IP\": \"N/A\",\n                                \"Private Hostname\": \"N/A\",\n                            }\n                            writer.writerow(row)\n                            rows_for_sheets.append(list(row.values()))\n\n                        # Process ALBs\n                        albs = list_alb_dns_names(elbv2_client)\n                        for dns in albs:\n                            row = {\n                                \"AccountName\": account_name,\n                                \"AccountID\": account_id,\n                                \"Region\": region,\n                                \"Type\": \"ALB\",\n                                \"Instance ID\": \"N/A\",\n                                \"Public IP/DNS Name\": dns,\n                                \"Private IP\": \"N/A\",\n                                \"Private Hostname\": \"N/A\",\n                            }\n                            writer.writerow(row)\n                            rows_for_sheets.append(list(row.values()))\n\n                    except ClientError as e:\n                        print(f\"Error in {region} for {account_name}: {e}\")\n            except (ClientError, ProfileNotFound) as e:\n                print(f\"Error accessing account {account_name}: {e}\")\n\n    print(\"CSV file 'aws_external_ips_by_region.csv' generated successfully.\")\n    export_to_sheets(\"aws-ips\", fieldnames, rows_for_sheets)\n\n\nif __name__ == \"__main__\":\n    main()\n"
}