【发布时间】:2016-12-05 09:11:12
【问题描述】:
使用 Amazon CLI,有没有办法获取当前 EC2 的公共 IP 地址?我只是在寻找单个字符串值,所以不是 json 响应 describe-addresses 返回。
【问题讨论】:
标签: amazon-web-services amazon-ec2
使用 Amazon CLI,有没有办法获取当前 EC2 的公共 IP 地址?我只是在寻找单个字符串值,所以不是 json 响应 describe-addresses 返回。
【问题讨论】:
标签: amazon-web-services amazon-ec2
AWS Command-Line Interface (CLI) 可用于返回任何/所有 Amazon EC2 实例的信息,例如:
$ aws ec2 describe-instances --instance-ids i-0c9c9b44b --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
54.232.200.77
如果您正在查找有关执行命令的 EC2 实例的信息,则可以通过 instance metadata service 获取当前 IP 地址:
$ 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 地址可通过以下方式获得:
$ curl http://169.254.169.254/latest/meta-data/local-ipv4
172.31.10.221
公共 IP 地址可通过以下方式获得:
$ curl http://169.254.169.254/latest/meta-data/public-ipv4
54.232.200.77
【讨论】:
curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/09:34:54:0a:9b:4a/public-ipv4s 的东西来使用每个接口的mac 地址获取每个接口的公共IP(你可以从ifconfig-a 获取)
curl http://checkip.amazonaws.com
这将返回公共 IP 地址。
【讨论】:
如果你在实例内 -
$ curl icanhazip.com
162.202.17.123
还有一种方法
$ curl -s ifconfig.me
162.202.17.123
这些方法不仅限于 AWS。
【讨论】:
使用 PublicIP 获取附加的 InstanceID。
aws ec2 describe-network-interfaces --query NetworkInterfaces[*].[Attachment.[InstanceId],Association.[PublicIp]] --output=json
【讨论】:
从内部实例输入以下命令:
curl -s v4.ident.me
【讨论】: