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

时间:2014-04-07 21:07:03

标签: java amazon-web-services amazon-ec2 ip

我正在尝试使用Java SDK获取所有Amazon Ec2实例的Public IP。我一直在搜索documentation,发现我需要使用DescribeInstanceRequestDescribeInstanceResultFilter来实现我的目的。

但是,我不明白如何完成圆圈。 DescribeInstanceResult似乎没有我需要的东西,我不知道如何有效地打印我想要的实例IP。

到目前为止,这是我的代码:

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;
    }

我该如何完成?我错过了什么?

3 个答案:

答案 0 :(得分:2)

问题是过滤器。显然使用它们是一个糟糕的决定,没有任何意义这样做。

解决方案:

    /**
     * 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;
    }

答案 1 :(得分:1)

以下代码只是解释了这个想法,并没有通过编译器。

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());

     }     
}

答案 2 :(得分:0)

请参阅以下用于获取实例的公共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());
         }     
    }
}