bash 列出 EC2 实例的公有 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24938971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List public IP addresses of EC2 instances
提问by Bas Peeters
I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or a new-line).
我想使用 Bash 列出我的 EC2 实例的公共 IP 地址,用分隔符(空格或换行符)分隔。
I tried to pipe the output to jqwith aws ec2 describe-instances | jq
, but can't seem to isolate just the IP addresses.
我试图管道输出到JQ有aws ec2 describe-instances | jq
,但似乎不能只是孤立的IP地址。
Can this be done by aws
alone, specifying arguments to jq
, or something else entirely?
这可以aws
单独完成,为 指定参数jq
,还是完全其他的东西?
回答by Julio Faerman
回答by Brad Giaccio
- Filter on running instances (you can drop that part if you don't need it)
- Query for each PublicIPaddress and the Name Tag, handling when Name isn't set
- 过滤正在运行的实例(如果不需要,可以删除该部分)
- 查询每个 PublicIPaddress 和 Name Tag,在未设置 Name 时处理
aws ec2 describe-instances \
--filter "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" \
--output text
回答by A Null Pointer
The below command would list the IP addresses of all your running EC2 instances
以下命令将列出所有正在运行的 EC2 实例的 IP 地址
aws ec2 describe-instances | grep PublicIpAddress | grep -o -P "\d+\.\d+\.\d+\.\d+" | grep -v '^10\.'
Hope that answers your query...
希望能解答您的疑问...
But this works without all the errors about access:
但这可以在没有关于访问的所有错误的情况下工作:
wget -qO- http://instance-data/latest/meta-data/public-ipv4/|grep .
回答by Frederic Henri
You can use instance metadataso you can run the following command from the ec2 instance:
您可以使用实例元数据,以便从 ec2 实例运行以下命令:
curl http://169.254.169.254/latest/meta-data/public-ipv4
and it will give you the public IP of the instance. If you want the private IP, you will run
它将为您提供实例的公共 IP。如果你想要私有IP,你将运行
curl http://169.254.169.254/latest/meta-data/local-ipv4
回答by Sarat Chandra
aws ec2 describe-instances --query "Reservations[].Instances[][PublicIpAddress]"
Refer: http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html
参考:http: //docs.aws.amazon.com/cli/latest/userguide/controlling-output.html