从JAVA API获取Amazon EC2实例的公共DNS

发布于 2025-01-04 05:49:56 字数 974 浏览 1 评论 0原文

我已成功通过 JAVA API 启动、停止和检查之前创建的 EC2 实例的状态。但是,我很难获取此实例的公共 DNS 地址。由于我使用 StartInstancesRequest 启动实例并使用 StartInstancesResponse 获取响应,因此我无法检索实际的 Instance 对象。我的起始代码如下所示,它可以工作:

BasicAWSCredentials oAWSCredentials = new BasicAWSCredentials(sAccessKey, sSecretKey);
AmazonEC2 ec2 = new AmazonEC2Client(oAWSCredentials);
ec2.setEndpoint("https://eu-west-1.ec2.amazonaws.com");
List<String> instanceIDs = new ArrayList<String>();
instanceIDs.add("i-XXXXXXX");

StartInstancesRequest startInstancesRequest = new StartInstancesRequest(instanceIDs);
try {
        StartInstancesResult response = ec2.startInstances(startInstancesRequest);
        System.out.println("Sent! "+response.toString());
    }catch (AmazonServiceException ex){
        System.out.println(ex.toString());
        return false;
    }catch(AmazonClientException ex){
        System.out.println(ex.toString());
        return false;
    }

此外,通过 JSch 连接到此实例的任何帮助都将受到赞赏。

I have managed to start, stop and check the status of a previously created EC2 instance from JAVA API. However, i'm having difficulty on getting the public dns address of this instance. Since I start the instance with StartInstancesRequest and get the response with StartInstancesResponse, i couldn't be able to retrieve the actual Instance object. My starting code is given below, it works:

BasicAWSCredentials oAWSCredentials = new BasicAWSCredentials(sAccessKey, sSecretKey);
AmazonEC2 ec2 = new AmazonEC2Client(oAWSCredentials);
ec2.setEndpoint("https://eu-west-1.ec2.amazonaws.com");
List<String> instanceIDs = new ArrayList<String>();
instanceIDs.add("i-XXXXXXX");

StartInstancesRequest startInstancesRequest = new StartInstancesRequest(instanceIDs);
try {
        StartInstancesResult response = ec2.startInstances(startInstancesRequest);
        System.out.println("Sent! "+response.toString());
    }catch (AmazonServiceException ex){
        System.out.println(ex.toString());
        return false;
    }catch(AmazonClientException ex){
        System.out.println(ex.toString());
        return false;
    }

Besides any help through connecting to this instance via JSch will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2025-01-11 05:49:56

这是一个可以解决问题的方法。在调用此方法之前最好检查实例是否处于运行状态。

String getInstancePublicDnsName(String instanceId) {
    DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesRequest.getReservations();
    Set<Instance> allInstances = new HashSet<Instance>();
    for (Reservation reservation : reservations) {
      for (Instance instance : reservation.getInstances()) {
        if (instance.getInstanceId().equals(instanceId))
          return instance.getPublicDnsName();
      }
    }
    return null;
}

Here's a method that would do the trick. It would be best to check that the instance is in the running state before calling this.

String getInstancePublicDnsName(String instanceId) {
    DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesRequest.getReservations();
    Set<Instance> allInstances = new HashSet<Instance>();
    for (Reservation reservation : reservations) {
      for (Instance instance : reservation.getInstances()) {
        if (instance.getInstanceId().equals(instanceId))
          return instance.getPublicDnsName();
      }
    }
    return null;
}
一指流沙 2025-01-11 05:49:56

现在,您可以在使用 describeInstances 时使用过滤器,这样您就不必提取所有实例的信息。

private String GetDNS(String aInstanceId)
{
  DescribeInstancesRequest request = new DescribeInstancesRequest();
  request.withInstanceIds(aInstanceId);
  DescribeInstancesResult result = amazonEC2.describeInstances(request);

  for (Reservation reservations : result.getReservations())
  {
    for (Instance instance : reservations.getInstances())
    {
      if (instance.getInstanceId().equals(aInstanceId))
      {
        return instance.getPublicDnsName();
      }
    }
  }

  return null;
}

使用aws-java-sdk-1.9.35.jar

You can now use a filter when using describeInstances, so you don't pull info for all your instances.

private String GetDNS(String aInstanceId)
{
  DescribeInstancesRequest request = new DescribeInstancesRequest();
  request.withInstanceIds(aInstanceId);
  DescribeInstancesResult result = amazonEC2.describeInstances(request);

  for (Reservation reservations : result.getReservations())
  {
    for (Instance instance : reservations.getInstances())
    {
      if (instance.getInstanceId().equals(aInstanceId))
      {
        return instance.getPublicDnsName();
      }
    }
  }

  return null;
}

Using aws-java-sdk-1.9.35.jar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文