How to get a list of internal IP addresses of GCE instances

穿精又带淫゛_ 提交于 2019-12-04 21:59:11

问题


I have a bunch of instances running in GCE. I want to programmatically get a list of the internal IP addresses of them without logging into the instances (locally).

I know I can run:

gcloud compute instances list

But are there any flags I can pass to just get the information I want? e.g.

gcloud compute instances list --internal-ips

or similar? Or am I going to have to dust off my sed/awk brain and parse the output?

I also know that I can get the output in JSON using --format=json, but I'm trying to do this in a bash script.


回答1:


The simplest way to programmatically get a list of internal IPs (or external IPs) without a dependency on any tools other than gcloud is:

$ gcloud --format="value(networkInterfaces[0].networkIP)" compute instances list
$ gcloud --format="value(networkInterfaces[0].accessConfigs[0].natIP)" compute instances list

This uses --format=value which also requires a projection which is a list of resource keys that select resource data values. For any command you can use --format=flattened to get the list of resource key/value pairs:

$ gcloud --format=flattened compute instances list



回答2:


A few things here.

First gcloud's default output format for listing is not guaranteed to be stable, and new columns may be added in the future. Don't script against this!

The three output modes are three output modes that are accessible with the format flag, --format=json, --format=yaml, and format=text, are based on key=value pairs and can scripted against even if new fields are introduced in the future.

Two good ways to do what you want are to use JSON and the jq tool,

gcloud compute instances list --format=json \
    | jq '.[].networkInterfaces[].networkIP'

or text format and grep + line-oriented using tools,

gcloud compute instances list --format=text \
    | grep '^networkInterfaces\[[0-9]\+\]\.networkIP:' | sed 's/^.* //g'



回答3:


I hunted around and couldn't find a straight answer, probably because efficient tools weren't available when others replied to the original question. GCP constantly updates their libraries & APIs and we can use the filter and projections to extract targeted attributes.

Here I outline how to reserve an external static IP, see how it's attributes are named & organised, and then export the external IP address so that I can use it in other scripts (e.g. assign this to a VM instance or authorise this network (IP address) on a Cloud SQL instance.

Reserve a static IP in a region of your choice

gcloud compute --project=[PROJECT] addresses create [NAME] --region=[REGION]

[Informational] View the details of the regional static IP that was reserved

gcloud compute addresses describe [NAME] --region [REGION] --format=flattened

[Informational] List the attributes of the static IP in the form of key-value pairs

gcloud compute addresses describe [NAME] --region [REGION] --format='value(address)'

Extract the desired value (e.g. external IP address) as a parameter

export STATIC_IP=$(gcloud compute addresses describe [NAME] --region [REGION] --format='value(address)’)

Use the exported parameter in other scripts

echo $STATIC_IP



回答4:


As far as I know you can't filter on specific fields in the gcloud tool. Something like this will work for a Bash script, but it still feels a bit brittle:

gcloud compute instances list --format=yaml | grep "  networkIP:" | cut -c 14-100



回答5:


I agree with @Christiaan. Currently there is no automated way to get the internal IPs using the gcloud command.

You can use the following command to print the internal IPs (4th column):

gcloud compute instances list | tail -n+2 | awk '{print $4}'

or the following one if you want to have the pair <instance_name> <internal_ip> (1st and 4th column)

gcloud compute instances list | tail -n+2 | awk '{print $1, $4}'  

I hope it helps.



来源:https://stackoverflow.com/questions/27994033/how-to-get-a-list-of-internal-ip-addresses-of-gce-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!