How to find the IP address of an amazon EC2 instan

2019-01-21 01:02发布

On reboot, the IP address of an amazon instance changes. How to find the new IP address using java API?

10条回答
▲ chillily
2楼-- · 2019-01-21 01:19

In order to fetch the public IP of the instance you first need to get the instance id of that instance. You can get the instance id of the instance by using the following java code.

 List<Instance> instances = runInstancesResult.getReservation().getInstances();

 String instanceId = instances.get(0).toString().substring(13, 23);

And now in order to get the public IP you can use the following java code.

public void fetchInstancePublicIP() {
    DescribeInstancesRequest request = new   DescribeInstancesRequest().withInstanceIds("i-d99ae7d2");
    DescribeInstancesResult result= ec2.describeInstances(request);
    List <Reservation> list  = result.getReservations();

    for (Reservation res:list) {
         List <Instance> instanceList= res.getInstances();

         for (Instance instance:instanceList){

                 System.out.println("Public IP :" + instance.getPublicIpAddress());     
                 System.out.println("Public DNS :" + instance.getPublicDnsName());
                 System.out.println("Instance State :" + instance.getState());
                 System.out.println("Instance TAGS :" + instance.getTags());
         }     
    }
}
查看更多
狗以群分
3楼-- · 2019-01-21 01:23
curl http://169.254.169.254/latest/meta-data/public-ipv4
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-21 01:24

You can use CLI CRUD aplication to AWS resources. AWS-CRUD-Manager

To find all ec2 instances

./awsconsole.py ec2 all

To find all of data for one ec2 instances (including IP priv and public)

./awsconsole.py ec2 find -i <id-insta`ce>
查看更多
forever°为你锁心
5楼-- · 2019-01-21 01:26

Assuming you don't want to assign an Elastic IP address (and there are reasons why this is not always a solution) then simply call DescribeInstances on the rebooted instance, which returns a bunch of information including Public IP Address.

Here's the AWS EC2 Java API Documentation on the topic.

查看更多
Lonely孤独者°
6楼-- · 2019-01-21 01:28

you Can Use This.

                var ipofnewServer = string.Empty;    
                while (string.IsNullOrEmpty(ipofnewServer))
                {
                    var statusRequest1 = new DescribeInstancesRequest
                    {
                        InstanceIds = new List<string>() { instanceId }
                    };

                    var result = amazonEc2client.DescribeInstances(statusRequest1);
                    var status = result.Reservations[0].Instances.FirstOrDefault();
                    if (status != null)
                    {
                        ipofnewServer = status.PublicIpAddress;
                    }
                }
查看更多
Root(大扎)
7楼-- · 2019-01-21 01:29

On reboot, the IP addresses of an EC2 instance do not change. They do generally change on stop/start of a non-VPC EBS boot instance.

See my answer to your related question here:

https://stackoverflow.com/questions/7533871/difference-between-rebooting-and-stop-starting-an-amazon-ec2-instance

That said, you can find the private and public IP addresses through the API call for DescribeInstances in your particular language.

If you are on the instance itself, you can also find the IP addresses through the user-data API using simple HTTP:

http://instance-data/latest/meta-data/local-ipv4
http://instance-data/latest/meta-data/public-ipv4

For example,

wget -qO- http://instance-data/latest/meta-data/public-ipv4

Elastic IP addresses are recommended for keeping a consistent (static) externally facing IP address for a particular service or server. These need to be re-assigned to an instance after a stop/start (but not after a reboot).

查看更多
登录 后发表回答