获取当前EC2实例上的公共IP地址 [英] Get Public IP Address on current EC2 Instance

查看:637
本文介绍了获取当前EC2实例上的公共IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Amazon CLI,有没有办法获取当前EC2的公共IP地址?我只是在寻找单个字符串值,而不是json响应describe-addresses返回。

Using Amazon CLI, is there a way to get the public ip address of the current EC2? I'm just looking for the single string value, so not the json response describe-addresses returns.

推荐答案

AWS命令行界面(CLI)可用于返回任何/所有Amazon EC2实例的信息,例如:

The AWS Command-Line Interface (CLI) can be used to return information on any/all Amazon EC2 instances, eg:

$ aws ec2 describe-instances --instance-ids i-0c9c9b44b --query 'Reservations[*].Instances[*].PublicIpAddress' --output text

54.232.200.77

如果要查找有关从中执行命令的EC2实例的信息,则当前IP地址可以是通过实例元数据服务

If you are seeking information about the EC2 instance from which you are executing the command, then the current IP address can be obtained via the instance metadata service:

$ curl http://169.254.169.254/latest/meta-data/

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
iam/
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/

因此,私有IP地址可通过以下方式获得:

So, the private IP address is available via:

$ curl http://169.254.169.254/latest/meta-data/local-ipv4

172.31.10.221

公共IP地址可通过以下方式获得:

The public IP address is available via:

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

54.232.200.77

这篇关于获取当前EC2实例上的公共IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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