Leticia Esperon
Leticia Esperon

Reputation: 2841

Using the public ip in a bash script

I'm writing a bash script to send in the user-data of an ec2 instance that I will launch dynamically. The script needs to use the instance ip, which I don't have until the instance is launched and runs the script. So the script should be something like

"#!/bin/bash
curl -X POST .....blablabla.........
{
     "serverIp": HERE I NEED THE CURRENT INSTANCE IP,
}'

I know that doing curl to http://127.0.0.1/latest/meta-data/public-ipv4 you get the ip but given other factors, I can't use an http call to itself.

Is there any easy way of retrieving the ip from the bash script?

Thank you!!

Upvotes: 0

Views: 827

Answers (3)

John Rotenstein
John Rotenstein

Reputation: 269540

Insert call call-out to curl:

#!/bin/bash
curl -X POST .....blablabla.........
{
     "serverIp": `curl -s http://169.254.169.254/latest/meta-data/public-ipv4`,
}'

Upvotes: 2

ibstevieb123
ibstevieb123

Reputation: 96

using curl

IP_ADDR=`curl -s http://whatismyip.akamai.com/` echo $IP_ADDR

other answers here

Upvotes: 1

Cyrus
Cyrus

Reputation: 88674

With dig:

#!/bin/bash

serverIp=$(dig +time=1 +tries=1 +retry=1 +short myip.opendns.com @resolver1.opendns.com)
echo $serverIp

Upvotes: 0

Related Questions