简体   繁体   English

使用Java查找我所有EC2实例的公共IP?

[英]Find the Public IP of all my EC2 instances using Java?

I am trying to get the Public IP of all my Amazon Ec2 instances using the Java SDK. 我正在尝试使用Java SDK获得所有Amazon Ec2实例的公共IP。 I have been searching through the documentation and found that I need to use a DescribeInstanceRequest , DescribeInstanceResult and a Filter to achieve my purpose. 我一直在搜索文档 ,发现我需要使用DescribeInstanceRequestDescribeInstanceResultFilter来实现我的目的。

However, I do not understand how to complete the circle. 但是,我不知道如何完成圈子。 The DescribeInstanceResult does not seem to have what I need and I do not know how to effectively print the instance IPs that I want. DescribeInstanceResult似乎没有我需要的东西,我也不知道如何有效地打印所需的实例IP。

So far, this is my code: 到目前为止,这是我的代码:

public List<String> getPublicIPs(){
        DescribeInstancesRequest request =  new DescribeInstancesRequest();
        request.setInstanceIds(instanceIds);

        List<Filter> filters = new LinkedList<Filter>();
        filters.add(new Filter("ip-address"));
        request.setFilters(filters);


        DescribeInstancesResult result = ec2.describeInstances(request);

        //what now!?
        return null;
    }

How do I complete it? 我该如何完成? What am I missing? 我想念什么?

The problem were the filters. 问题出在过滤器上。 Apparently using them is a bad decision and there is no point whatsoever in doing so. 显然,使用它们是一个错误的决定,这样做毫无意义。

Solution: 解:

    /**
     * Returns a list with the public IPs of all the active instances, which are
     * returned by the {@link #getActiveInstances()} method.
     * 
     * @return  a list with the public IPs of all the active instances.
     * @see     #getActiveInstances()
     * */
    public List<String> getPublicIPs(){
        List<String> publicIpsList = new LinkedList<String>();

        //if there are no active instances, we return immediately to avoid extra 
        //computations.
        if(!areAnyActive())
            return publicIpsList;

        DescribeInstancesRequest request =  new DescribeInstancesRequest();
        request.setInstanceIds(instanceIds);

        DescribeInstancesResult result = ec2.describeInstances(request);
        List<Reservation> reservations = result.getReservations();

        List<Instance> instances;
        for(Reservation res : reservations){
            instances = res.getInstances();
            for(Instance ins : instances){
                LOG.info("PublicIP from " + ins.getImageId() + " is " + ins.getPublicIpAddress());
                publicIpsList.add(ins.getPublicIpAddress());
            }
        }

        return publicIpsList;
    }

The below codes just explain the idea and have not gone through the compiler. 下面的代码仅说明了这一思想,并未经过编译器。

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("Instance Public IP :" + instance.getPublicIpAddress());

     }     
}

Refer the following code that fetches the public IP of instances. 请参考以下获取实例的公共IP的代码。

public void fetchInstancePublicIP(){
    DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds(instanceIDs);
    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("Instance Public IP :" + instance.getPublicIpAddress());
                 System.out.println("Instance Public DNS :" + instance.getPublicDnsName());
                 System.out.println("Instance State :" + instance.getState());
                 System.out.println("Instance TAGS :" + instance.getTags());
         }     
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM