如何在重新启动时查找亚马逊 EC2 实例的 IP 地址

发布于 2024-12-05 20:24:05 字数 52 浏览 0 评论 0原文

重新启动后,亚马逊实例的 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 技术交流群。

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

发布评论

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

评论(11

眼泪也成诗 2024-12-12 20:24:05

重新启动时,EC2 实例的 IP 地址不会更改。它们通常会在非 VPC EBS 启动实例的停止/启动时发生变化。

在这里查看我对您的相关问题的回答:

https://stackoverflow.com/questions /7533871/重新启动和停止启动亚马逊 EC2 实例之间的差异

可以通过 API 调用找到私有和公共 IP 地址以您的特定语言DescribeInstances

如果您位于实例本身,您还可以使用简单的 HTTP 通过用户数据 API 查找 IP 地址:

http://instance-data/latest/meta-data/local-ipv4
http://instance-data/latest/meta-data/public-ipv4

例如,

wget -qO- http://instance-data/latest/meta-data/public-ipv4

建议使用弹性 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:

https://stackoverflow.com/questions/7533871/difference-between-rebooting-and-stop-starting-an-amazon-ec2-instance

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:

http://instance-data/latest/meta-data/local-ipv4
http://instance-data/latest/meta-data/public-ipv4

For example,

wget -qO- http://instance-data/latest/meta-data/public-ipv4

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).

辞慾 2024-12-12 20:24:05
curl http://169.254.169.254/latest/meta-data/public-ipv4
curl http://169.254.169.254/latest/meta-data/public-ipv4
余生共白头 2024-12-12 20:24:05

公共 IPv4 地址也可以从 EC2 实例获取,如下所示:

curl checkip.amazonaws.com

公共主机名是:

dig -x $(curl -s checkip.amazonaws.com) +short

The public IPv4 address is also available from the EC2 instance like so:

curl checkip.amazonaws.com

And the public hostname is:

dig -x $(curl -s checkip.amazonaws.com) +short
葬花如无物 2024-12-12 20:24:05

假设您不想分配弹性 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.

我爱人 2024-12-12 20:24:05

这就是我在 VPC 内的 Ubuntu 12.04 EC2 实例上执行此操作的方法:

curl ifconfig.me

不知道为什么,但在元数据 url 下找不到 public-ipv4

this is how I did it on an Ubuntu 12.04 EC2 instance within a VPC:

curl ifconfig.me

not sure why but public-ipv4 was not found under the meta-data url

耳钉梦 2024-12-12 20:24:05

为了获取实例的公共 IP,您首先需要获取该实例的实例 ID。您可以使用以下java代码获取实例的实例id。

 List<Instance> instances = runInstancesResult.getReservation().getInstances();

 String instanceId = instances.get(0).toString().substring(13, 23);

现在为了获取公共IP,您可以使用以下java代码。

public void fetchInstancePublicIP() {
    DescribeInstancesRequest request = new   DescribeInstancesRequest().withInstanceIds("i-d99ae7d2");
    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("Public IP :" + instance.getPublicIpAddress());     
                 System.out.println("Public DNS :" + instance.getPublicDnsName());
                 System.out.println("Instance State :" + instance.getState());
                 System.out.println("Instance TAGS :" + instance.getTags());
         }     
    }
}

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.

 List<Instance> instances = runInstancesResult.getReservation().getInstances();

 String instanceId = instances.get(0).toString().substring(13, 23);

And now in order to get the public IP you can use the following java code.

public void fetchInstancePublicIP() {
    DescribeInstancesRequest request = new   DescribeInstancesRequest().withInstanceIds("i-d99ae7d2");
    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("Public IP :" + instance.getPublicIpAddress());     
                 System.out.println("Public DNS :" + instance.getPublicDnsName());
                 System.out.println("Instance State :" + instance.getState());
                 System.out.println("Instance TAGS :" + instance.getTags());
         }     
    }
}
肥爪爪 2024-12-12 20:24:05

他们有 util 类,可以帮助访问 EC2 元数据。
例如

EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicHostname();
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicIPv4s();

They have util class which facilitates access to EC2 metadata.
For instance

EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicHostname();
EC2MetadataUtils.getNetworkInterfaces().get(0).getPublicIPv4s();
凤舞天涯 2024-12-12 20:24:05

假设您重新启动实例并且不是从头开始启动,那么您可以分配一个弹性 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

简美 2024-12-12 20:24:05

你可以使用这个。

                var ipofnewServer = string.Empty;    
                while (string.IsNullOrEmpty(ipofnewServer))
                {
                    var statusRequest1 = new DescribeInstancesRequest
                    {
                        InstanceIds = new List<string>() { instanceId }
                    };

                    var result = amazonEc2client.DescribeInstances(statusRequest1);
                    var status = result.Reservations[0].Instances.FirstOrDefault();
                    if (status != null)
                    {
                        ipofnewServer = status.PublicIpAddress;
                    }
                }

you Can Use This.

                var ipofnewServer = string.Empty;    
                while (string.IsNullOrEmpty(ipofnewServer))
                {
                    var statusRequest1 = new DescribeInstancesRequest
                    {
                        InstanceIds = new List<string>() { instanceId }
                    };

                    var result = amazonEc2client.DescribeInstances(statusRequest1);
                    var status = result.Reservations[0].Instances.FirstOrDefault();
                    if (status != null)
                    {
                        ipofnewServer = status.PublicIpAddress;
                    }
                }
自由范儿 2024-12-12 20:24:05

您可以对 AWS 资源使用 CLI CRUD 应用程序。
AWS-CRUD-Manager

查找所有 ec2 实例

./awsconsole.py ec2 all

查找一个实例的所有数据ec2 实例(包括 IP 私有和公共)

./awsconsole.py ec2 find -i <id-insta`ce>

You can use CLI CRUD aplication to AWS resources.
AWS-CRUD-Manager

To find all ec2 instances

./awsconsole.py ec2 all

To find all of data for one ec2 instances (including IP priv and public)

./awsconsole.py ec2 find -i <id-insta`ce>
辞取 2024-12-12 20:24:05

我使用的是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 use hostname -i

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