0

I have an Amazon instance. This instance has two interfaces. Each interface has two elastic ips. So, in total, this instance has 4 elastic ips.

I would like to know how I can get the associated private ip given a public ip or the assoicate public ip given a private ip.

For example, say my instance has these public - private ips:

80.0.0.0 - 10.0.0.0
80.0.0.1 - 10.0.0.1
80.0.0.2 - 10.0.0.2
80.0.0.3 - 10.0.0.3

I would like to know if there is a way to do something like

aws ip-mapping 80.0.0.0

With this returning 10.0.0.0

Or alternatively, something like:

curl http://169.254.169.254/latest/meta-data/network/interfaces/ip-mapping/80.0.0.0/

What is the best way to find the mapping between a private ip and a public ip or a public ip and a private ip for amazon instances using elastic ips in vpc?

Thanks!

mayk93
  • 1,489
  • 3
  • 17
  • 31

2 Answers2

0

Yes you can use below curl to achieve your use case.

curl --interface 80.0.0.0 ifconfig.me
Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
0

Give an ec2 the rights to describe ec2 instances, ensure that boto3 is installed and then this program should be able to figure out a private given a public or a public given a private.

Comes with no warrantee, not extensively tested

#!/usr/bin/python

import sys

(theother) = (sys.argv[1])
import boto3

e = boto3.client('ec2').describe_instances()
possible = {}

for r in e['Reservations']:
  for i in r['Instances']:
    if 'PrivateIpAddress' in i:
     p = i['PrivateIpAddress']
     if 'NetworkInterfaces' in i:
        for n in i['NetworkInterfaces']:
          if 'Association' in n and 'PublicIp' in n['Association']:
            u = n['Association']['PublicIp']
            if u == theother or p == theother:
              print "found"
              possible['id'] = i['InstanceId']
              possible['public'] = u
              possible['private'] = p
              break

print possible

To run the program, give it a command line arg of a dotted quad which represents the ip - public or private - that you want to find the match for

Alternatively to find this information in the AWS Console GUI go to the VPC page for elastic IPs. The table lists the publically visible IP and the private address it is associated with

Vorsprung
  • 32,923
  • 5
  • 39
  • 63