'How can I get all External IP Addresses in my GCP organization?
I would like to get ALL external IP addresses in my GCP Organization. This information seems to be available in the GCP Console under VPC network > External IP Addresses, but I can't seem to find an API or way to export this information.
I've tried the GCLOUD command line tool but it only lists STATIC ip addresses. I also want ephemeral addresses: gcloud compute addresses list
The Go GCP Compute API does the same.
Solution 1:[1]
There are two types of public (external) IP addresses in Google Cloud: regional and global.
The following commands use the Windows syntax. Modify each command for the project ID and region list.
This command will list the regional addresses for the specified regions:
gcloud compute addresses list --project development --filter="region:( us-west1, us-west2 )"
This command will list the global addresses:
gcloud compute addresses list --global --project development
Note that this must be repeated for each project. There are no options for organizations.
To list the projects that your credentials have rights to:
gcloud projects list
This does not mean that all projects on this list are part of your organization. Credentials can be added as member accounts in projects outside your organization.
This does not mean that all projects in your organization are listed. Only the ones with permissions to list/access the projects. Your organization could be using Folders.
The following link is for the API:
Solution 2:[2]
As was mentioned in previous responses, there is no easy way to get that information.
I tried with some commands in my own project and I can list all the external ephemeral IPs on my instances:
gcloud compute instances describe INTANCE_NAME --format='get(networkInterfaces[0].accessConfigs[0].natIP)' --zone ZONE
gcloud compute instances list --format='table(EXTERNAL_IP)'
Also I found another public tracker where they are requesting almost the same that you mentioned, may you can take a look and comment something there.
Solution 3:[3]
To achieve this goal, you can use something like this
PROJECTS=$(gcloud projects list --format="value(name)")
for project in $PROJECTS
do
if [[ $(gcloud services list --project $project --format="table(NAME)" | sed '1d') =~ "compute.googleapis.com" ]];then
echo $project
gcloud compute addresses list --project $project --global
fi
done
Solution 4:[4]
Google Cloud uses a wide range of IP addresses that changes overtime. According to this link, the nearest way is using DNS lookup commands (nslookup
, dig
, or host
) to manually scrub through a number of addresses and compare against the static addresses and pick out the ephemeral addresses.
Having said that, there is a public tracker for this feature which is being evaluated if a flag can be added to the command gcloud compute addresses list
to list the ephemeral IPs:
https://issuetracker.google.com/119178618
Here is another related topic: https://stackoverflow.com/a/53650099
Solution 5:[5]
I have managed to get the Ephemeral
IP addresses by running the following commands on Linux.
GCP console shows (VPC network -> External IP addresses):
- XX.XX.XX.XX us-west2 Ephemeral
vmnumber1 XX.XX.XX.XX us-west2 Static
vmnumber2 XX.XX.XX.XX us-west2 Static
These are the Static Public IPs (gcloud does not show Ephemeral IP addresses)
gcloud --project your_project_name compute addresses list --format="value(ADDRESS)" | sort | tee -a static_public_ips
XX.XX.XX.XX
XX.XX.XX.XX
These are the Public IPs assigned to instances, it may include (Static and Ephemeral IP addresses)
gcloud --project your_project_name compute instances list --filter="EXTERNAL_IP!=NULL" --format="value(EXTERNAL_IP)" | sort | tee -a static_public_ips_assigned
XX.XX.XX.XX
XX.XX.XX.XX
Then we can compare the two files, but we want the IPs that are not in `static_public_ips` which are the `Ephemeral` ones.
diff -u static_public_ips static_public_ips_assigned | sed -n '/^+[^+]/ s/^+//p'
XX.XX.XX.XX
Solution 6:[6]
#!/bin/bash -e
PROJECTS=$(gcloud projects list --format="value(project_id)" | sort | uniq)
for project in $PROJECTS
do
if [[ $(gcloud services list --project $project --format="table(NAME)" | sed '1d') =~ "compute.googleapis.com" ]];then
echo $project
gcloud compute addresses list --project $project
gcloud compute addresses list --project $project --global
fi
done
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John Hanley |
Solution 2 | Neo Anderson |
Solution 3 | |
Solution 4 | |
Solution 5 | Pit |
Solution 6 | Matt Cobb |