如何从VM获取VM的外部IP

How to get external IP of VM from with VM

我有一个 GCP 计算实例 VM,我想从其中获取实例的外部 IP?

PS:实例上未设置 gcloud

在 shell 脚本中:

PUBLIC_IP=$(curl -s ifconfig.co)

来自网站:

Please limit automated requests to 1 request per minute. No guarantee is made for requests that exceed this limit. They may be rate-limited, with a 429 status code, or dropped entirely.

对于 GCP 还有更可靠和快速的:

curl -H "Metadata-Flavor:Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip