如何在命令行上查找 Google Cloud VM 实例的 IP 地址
问题:
你有一个 VM 实例(在我们的示例中为 my-instance),你想使用 gcloud 命令行工具获取其外部或内部 IP。
解决方案
如果你只想查看实例的外部 IP(记住将 my-instance 替换为你的实例名称!),使用
gcloud-instances-list-box.sh
gcloud compute instances list --filter="name=my-instance" --format "[box]"这将格式化输出并显示有关你实例的更多信息。示例输出:
gcloud-instances-box-output.txt
┌─────────────┬────────────────┬─────────────────────────────┬─────────────┬─────────────┬───────────────┬─────────┐
│ NAME │ ZONE │ MACHINE_TYPE │ PREEMPTIBLE │ INTERNAL_IP │ EXTERNAL_IP │ STATUS │
├─────────────┼────────────────┼─────────────────────────────┼─────────────┼─────────────┼───────────────┼─────────┤
│ my-instance │ europe-west3-c │ custom (16 vCPU, 32.00 GiB) │ │ 10.156.0.1 │ 35.207.77.101 │ RUNNING │
└─────────────┴────────────────┴─────────────────────────────┴─────────────┴─────────────┴───────────────┴─────────┘在此示例中,外部 IP 地址为 35.207.77.101。
如果你想只查看 IP 地址,请使用此命令:
gcloud-get-external-ip.sh
gcloud compute instances list --filter="name=my-instance" --format "get(networkInterfaces[0].accessConfigs[0].natIP)"示例输出:
gcloud-external-ip.txt
35.207.77.101要仅查看内部 IP 地址(仅可从 Google Cloud 访问),使用
gcloud-get-internal-ip.sh
gcloud compute instances list --filter="name=my-instance" --format "get(networkInterfaces[0].networkIP)"在 linux shell 中,此命令的结果可以轻松用作其他命令的输入。例如,要 ping my-instance,使用
ping-gcloud-instance.sh
ping $(gcloud compute instances list --filter="name=katc-main" --format "get(networkInterfaces[0].accessConfigs[0].natIP)")另请参见我们的相关文章如何在命令行上查找 Google Cloud VM 实例的区域
要查看你可以以类似方式查看有关实例的其他信息,使用
gcloud-instances-list-text.sh
gcloud compute instances list --filter="name=my-instance" --format "text"Check out similar posts by category:
Cloud
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow