如何获取GCE实例的内部IP地址列表 [英] How to get a list of internal IP addresses of GCE instances

查看:72
本文介绍了如何获取GCE实例的内部IP地址列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆在GCE中运行的实例.我想以编程方式获取它们的内部IP地址的列表,而无需登录到本地实例.

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).

我知道我可以跑步:

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

还是类似的?还是我必须清除sed/awk大脑的灰尘并解析输出?

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

我也知道我可以使用--format = json来获取JSON中的输出,但是我正尝试在bash脚本中进行此操作.

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.

推荐答案

以编程方式获取内部IP(或外部IP)列表且不依赖除gcloud以外的任何工具的最简单方法是:

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

这使用 --format=value ,它也需要投影,它是

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

这篇关于如何获取GCE实例的内部IP地址列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆