List public IP addresses of EC2 instances

强颜欢笑 提交于 2019-11-28 20:07:08

Directly from the aws cli:

aws ec2 describe-instances \
  --query "Reservations[*].Instances[*].PublicIpAddress" \
  --output=text
A Null Pointer

The below command would list the IP addresses of all your running EC2 instances

aws ec2 describe-instances | grep PublicIpAddress | grep -o -P "\d+\.\d+\.\d+\.\d+" | grep -v '^10\.'

Hope that answers your query.

  • 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

    aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" --output text

You can use instance metadata so you can run the following command from the ec2 instance:

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

curl http://169.254.169.254/latest/meta-data/local-ipv4
aws ec2 describe-instances --query "Reservations[].Instances[][PublicIpAddress]"

Refer: http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!