Reputation: 3354
I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or a new-line).
I tried to pipe the output to jq with aws ec2 describe-instances | jq
, but can't seem to isolate just the IP addresses.
Can this be done by aws
alone, specifying arguments to jq
, or something else entirely?
Upvotes: 53
Views: 61161
Reputation: 325
aws ec2 describe-instances \
--filter "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" \
--output text
Upvotes: 21
Reputation: 2277
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...
But this works without all the errors about access:
wget -qO- http://instance-data/latest/meta-data/public-ipv4/|grep .
Upvotes: 9
Reputation: 53813
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
Upvotes: 6
Reputation: 6140
aws ec2 describe-instances --query "Reservations[].Instances[][PublicIpAddress]"
Refer: http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html
Upvotes: 5
Reputation: 13501
Directly from the aws cli:
aws ec2 describe-instances \
--query "Reservations[*].Instances[*].PublicIpAddress" \
--output=text
Upvotes: 122