如何在重新启动时查找亚马逊 EC2 实例的 IP 地址
重新启动后,亚马逊实例的 IP 地址会发生变化。如何使用java API找到新的IP地址?
On reboot, the IP address of an amazon instance changes. How to find the new IP address using java API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
重新启动时,EC2 实例的 IP 地址不会更改。它们通常会在非 VPC EBS 启动实例的停止/启动时发生变化。
在这里查看我对您的相关问题的回答:
可以通过 API 调用找到私有和公共 IP 地址以您的特定语言DescribeInstances。
如果您位于实例本身,您还可以使用简单的 HTTP 通过用户数据 API 查找 IP 地址:
例如,
建议使用弹性 IP 地址,以便为特定服务或服务器保持一致(静态)的面向外部的 IP 地址。这些需要在停止/启动后(但不是在重新启动后)重新分配给实例。
On reboot, the IP addresses of an EC2 instance do not change. They do generally change on stop/start of a non-VPC EBS boot instance.
See my answer to your related question here:
That said, you can find the private and public IP addresses through the API call for DescribeInstances in your particular language.
If you are on the instance itself, you can also find the IP addresses through the user-data API using simple HTTP:
For example,
Elastic IP addresses are recommended for keeping a consistent (static) externally facing IP address for a particular service or server. These need to be re-assigned to an instance after a stop/start (but not after a reboot).
公共 IPv4 地址也可以从 EC2 实例获取,如下所示:
公共主机名是:
The public IPv4 address is also available from the EC2 instance like so:
And the public hostname is:
假设您不想分配弹性 IP 地址(并且出于某些原因这并不总是解决方案),则只需在重新启动的实例上调用
DescribeInstances
即可,该操作会返回一系列信息,包括公共 IP地址。以下是有关该主题的 AWS EC2 Java API 文档。
Assuming you don't want to assign an Elastic IP address (and there are reasons why this is not always a solution) then simply call
DescribeInstances
on the rebooted instance, which returns a bunch of information including Public IP Address.Here's the AWS EC2 Java API Documentation on the topic.
这就是我在 VPC 内的 Ubuntu 12.04 EC2 实例上执行此操作的方法:
不知道为什么,但在元数据 url 下找不到 public-ipv4
this is how I did it on an Ubuntu 12.04 EC2 instance within a VPC:
not sure why but public-ipv4 was not found under the meta-data url
为了获取实例的公共 IP,您首先需要获取该实例的实例 ID。您可以使用以下java代码获取实例的实例id。
现在为了获取公共IP,您可以使用以下java代码。
In order to fetch the public IP of the instance you first need to get the instance id of that instance. You can get the instance id of the instance by using the following java code.
And now in order to get the public IP you can use the following java code.
他们有 util 类,可以帮助访问 EC2 元数据。
例如
They have util class which facilitates access to EC2 metadata.
For instance
假设您重新启动实例并且不是从头开始启动,那么您可以分配一个弹性 IP,该 IP 始终保留在 ec2 实例中(除非您将该 IP 移动到另一台服务器)。这允许您将所有 DNS 指向该一个 IP,而不必担心重新启动会给您带来问题。
我想这就是你的要求,但你还可以问其他事情。服务器的内部 IP 发生变化(如果您重新启动实例而不是重新启动),并且您无法“保留”它,因此您可能需要创建一个脚本来保留新 IP(如果您将内部服务指向它)。
希望有帮助
Assuming your rebooting the instance and not launching from scratch than you can assign an elastic IP which always remains with the ec2 instance(unless you move the IP to another server). This allows you to point all your DNS to that one IP and not worry that a reboot will cause you issues.
I think thats what your asking but there are other things you could be asking. The internal IP of the server changes(if you relaunch the instance not reboot) and you can't 'reserve' it so you may need to create a script to keep you the new IP(if your pointing internal services to it).
hope that helps
你可以使用这个。
you Can Use This.
您可以对 AWS 资源使用 CLI CRUD 应用程序。
AWS-CRUD-Manager
查找所有 ec2 实例
查找一个实例的所有数据ec2 实例(包括 IP 私有和公共)
You can use CLI CRUD aplication to AWS resources.
AWS-CRUD-Manager
To find all ec2 instances
To find all of data for one ec2 instances (including IP priv and public)
我使用的是
domain=$(hostname | cut -d "." -f1)
,但是得到的ip为ip-1-2-3-4。您是否无法使用hostname -i
I was using
domain=$(hostname | cut -d "." -f1)
however that was getting the ip as ip-1-2-3-4. Are you not able to usehostname -i