从正在运行的实例获取弹性IP [英] Get Elastic IP from running instance
本文介绍了从正在运行的实例获取弹性IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从运行在EC2实例上的Java应用程序中,我想知道从管理控制台手动分配的我自己的弹性IP地址。有没有办法为此查询EC2 API?
From a java application running on an EC2 instance, I'd like to know what my own elastic IP address that was manually assigned from the management console. Is there a way to query EC2 API for this?
推荐答案
如果您使用的是Linux ec2实例,则应该可以:
If you using a linux ec2 instance this should work:
命令:
curl http://169.254.169.254/latest/meta-data/public-ipv4
Java代码:
public static String getIP() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("curl http://169.254.169.254/latest/meta-data/public-ipv4");
int returnCode = p.waitFor();
if ( returnCode == 0 ) {
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String ip = r.readLine();
r.close();
return ip;
}
else {
//handle error
return null;
}
}
这篇关于从正在运行的实例获取弹性IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文