aibangjuxin / groovy

study groovy
1 stars 0 forks source link

Shared vpc #7

Open aibangjuxin opened 1 year ago

aibangjuxin commented 1 year ago

好的,为了列出 GCP 项目中使用 Shared VPC 的 GCE 实例,可以使用以下方法:

  1. 列出项目中的所有 GCE 实例:

gcloud compute instances list

  1. 对每个实例查看它连接的网络:

gcloud compute instances describe [INSTANCE_NAME] --format="value(networkInterfaces[].network)"

  1. 对显示为 Shared VPC 网络的实例,进一步查看该网络的信息:

gcloud compute networks describe [NETWORK_NAME]

  1. 在网络信息中查找“x_gcloud_mode”字段,如果它的值为“MANAGED”,则表示这是一个 Shared VPC 网络。

  2. 也可以直接过滤只查看使用 Shared VPC 的实例:

gcloud compute instances list --filter="networkInterfaces[].network~MANAGED"

这种方法可以帮助列出 GCP 项目中使用 Shared VPC 网络的 GCE 实例,并查看该 Shared VPC 网络的具体信息。

aibangjuxin commented 1 year ago

您的理解是对的,使用gcloud compute instances describe直接获取networkInterfaces[]会将所有网络一次性输出在一行。

要实现获取实例的每个网络接口及网络信息,并按多行输出,可以这样做:

bash for instance in $(gcloud compute instances list --format='value(name)'); do interfaces=$(gcloud compute instances describe $instance --format='value(networkInterfaces[].name)')

for interface in $interfaces; do network=$(gcloud compute instances describe $instance --format="value(networkInterfaces[name='$interface'].network)") echo "$instance;$network" done done

这里我们首先获取每个实例的网络接口名称,然后循环每个接口,根据其名称再获取对应的网络,这样就可以实现按接口和网络逐行输出了。

另外,也可以直接用jmespath查询语言过滤实现:

bash gcloud compute instances describe [INSTANCE_NAME] --format="jsonpath_query(networkInterfaces[*].network)"

这种方法可以避免多层循环,直接获取网络接口数组并逐行输出。

请让我知道如果这样的处理逻辑可以满足您的需求。

aibangjuxin commented 1 year ago

你可以使用--format参数的--format="json"选项来实现多行输出。这将以 JSON 格式输出结果,然后你可以使用其他工具或脚本来处理 JSON 数据并将多个网络结果分隔成多行。

例如,在终端中运行以下命令:

gcloud compute instances describe [INSTANCE_NAME] --format="json(networkInterfaces[].network)"

然后你可以处理输出并将多个网络结果分隔成多行。

aibangjuxin commented 1 year ago

如果您希望多个网络的结果可以换行,可以使用 gcloud 命令行工具的 --format 标志来格式化输出。例如,您可以使用以下命令来获取实例的所有网络接口,并将每个网络接口的 IP 地址输出到新行中:

gcloud compute instances describe [INSTANCE_NAME] --format="value(networkInterfaces[].network.ipAddress)" --format="table[no-heading](data)"

这将输出一个表格,其中每个 IP 地址都在新行中。如果您希望输出每个网络接口的所有信息,并将每个网络接口的信息输出到新行中,可以使用以下命令:

gcloud compute instances describe [INSTANCE_NAME] --format="table[box](networkInterfaces)"

这将输出一个表格,其中每个网络接口的信息都在新行中。您可以根据需要修改格式化字符串,以满足您的需求。

Sources [1] gcloud compute instances describe | Google Cloud CLI Documentation https://cloud.google.com/sdk/gcloud/reference/compute/instances/describe [2] gcloud compute 使用提示 https://cloud.google.com/compute/docs/gcloud-compute/tips?hl=zh-cn [3] gcp gcloud cheat sheet - gists · GitHub https://gist.github.com/pydevops/cffbd3c694d599c6ca18342d3625af97?permalink_comment_id=3053111 [4] 4.9 Ensure that Compute instances do not have public IP addresses | Tenable® https://www.tenable.com/audits/items/CIS_Google_Cloud_Platform_v1.2.0_L2.audit:9003c521ba0a8d070dddf7512593b962 [5] Check for Virtual Machine Instances with Public IP Addresses - Trend Micro https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/ComputeEngine/instances-with-public-ip-addresses.html [6] How to get a list of internal IP addresses of GCE instances - Stack Overflow https://stackoverflow.com/questions/27994033/how-to-get-a-list-of-internal-ip-addresses-of-gce-instances

By Perplexity at https://www.perplexity.ai/?s=m&uuid=a6c1eac6-339f-4ef3-8d1c-ca7135928f0e