Can you get external IP address from within a Google Compute VM instance?

Clo*_*d66 27 google-compute-engine

Querying the network interface will only return the internal instance IP address info.
Is it possible to get external IP address info from somewhere inside the running instance?

As an example, AWS has a metadata REST endpoint for instance info:
curl http://169.254.169.254/latest/meta-data/public-ipv4

(source: http://tech-queries.blogspot.co.uk/2011/06/how-to-get-public-ip-for-amazon-ec2.html)

Dav*_*vid 20

您可以使用HTTP GET获取公共IP地址http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip.您必须根据文档将Metadata-Flavor标头设置为Google .

  • 仅供参考,在撰写此评论时,在链接文档中找不到“external-ip”短语,但答案仍然按预期工作。 (2认同)

san*_*_23 20

备查.

以下是在各种云上获取公共IPv4的链接.

天蓝

curl -H Metadata:true "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-08-01&format=text"

AWS

curl http://169.254.169.254/latest/meta-data/public-ipv4

GCP

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

由于169.254.169.254是上述云供应商的全局元数据服务器,因此无需使用不同的域名.