从JAVA API获取Amazon EC2实例的公共DNS
我已成功通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个可以解决问题的方法。在调用此方法之前最好检查实例是否处于运行状态。
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.
现在,您可以在使用
describeInstances
时使用过滤器,这样您就不必提取所有实例的信息。使用
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.Using
aws-java-sdk-1.9.35.jar
.