

# 教程：使用 AWS CLI 查看 IP 地址历史记录
<a name="tutorials-historical-insights"></a>

本部分中的场景将向您展示如何使用 AWS CLI 分析和审计 IP 地址使用情况。有关使用 AWS CLI 的一般信息，请参阅 *AWS 命令行界面用户指南*中的[使用 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-using.html)。

**Topics**
+ [概述](#cli-tut-view-hist-ipam-overview)
+ [场景](#cli-tut-view-hist-ipam-analyze)

## 概述
<a name="cli-tut-view-hist-ipam-overview"></a>

IPAM 会自动将您的 IP 地址监控数据最多保留三年。您可以使用历史数据来分析和审核网络安全和路由策略。您可以为以下类型的资源搜索历史见解：
+ VPC
+ VPC 子网
+ 弹性 IP 地址
+ 正在运行的 EC2 实例
+ 连接到实例的 EC2 网络接口

**重要**  
尽管 IPAM 不会监控 Amazon EC2 实例或挂载到实例的 EC2 网络接口，但您可以使用搜索 IP 历史记录功能，来搜索 EC2 实例和网络接口 CIDR 上的历史数据。

**注意**  
本教程中的命令必须使用拥有 IPAM 的账户和托管 IPAM 的 AWS 区域运行。
CIDR 更改的记录会在定期快照中拾取，这意味着记录出现或更新可能需要一些时间，SampledStartTime 和 SampledEndTime 的值可能与实际发生时间不同。

## 场景
<a name="cli-tut-view-hist-ipam-analyze"></a>

本部分中的场景将向您展示如何使用 AWS CLI 分析和审计 IP 地址使用情况。有关本教程中提到的采样结束时间和开始时间等值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

**场景 1：2021 年 12 月 27 日上午 1:00 和晚上 9:00 (UTC) 之间有哪些资源与 `10.2.1.155/32` 关联？**

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 10.2.1.155/32 --ipam-scope-id ipam-scope-05b579a1909c5fc7a --start-time 2021-12-20T01:00:00.000Z --end-time 2021-12-27T21:00:00.000Z
   ```

1. 查看分析结果。在下面的示例中，CIDR 在一段时间内分配给网络接口和 EC2 实例。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {                                                     
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "network-interface",
               "ResourceId": "eni-0b4e53eb1733aba16",
               "ResourceCidr": "10.2.1.155/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "instance",
               "ResourceId": "i-064da1f79baed14f3",
               "ResourceCidr": "10.2.1.155/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           }
       ]
   }
   ```

   如果网络接口连接到的实例的拥有者 ID 与网络接口的所有者 ID 不同（如 NAT 网关、VPC 中的 Lambda 网络接口和其他 AWS 服务的情况），则 `ResourceOwnerId` 为 `amazon-aws`，而不是网络接口拥有者的账户 ID。以下示例显示了与 NAT 网关关联的 CIDR 的记录：

   ```
   {                                                     
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "network-interface",
               "ResourceId": "eni-0b4e53eb1733aba16",
               "ResourceCidr": "10.0.0.176/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           },
           {
               "ResourceOwnerId": "amazon-aws",
               "ResourceRegion": "us-east-1",
               "ResourceType": "instance",
               "ResourceCidr": "10.0.0.176/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           }
       ]
   }
   ```

**场景 2：2021 年 12 月 1 日至 2021 年 12 月 27 日 (UTC) 有哪些资源与 `10.2.1.0/24` 关联？**

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 10.2.1.0/24 --ipam-scope-id ipam-scope-05b579a1909c5fc7a --start-time 2021-12-01T00:00:00.000Z --end-time 2021-12-27T23:59:59.000Z
   ```

1. 查看分析结果。在下面的示例中，CIDR 在一段时间内分配给子网和 VPC。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "subnet",
               "ResourceId": "subnet-0864c82a42f5bffed",
               "ResourceCidr": "10.2.1.0/24",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "vpc",
               "ResourceId": "vpc-0f5ee7e1ba908a378",
               "ResourceCidr": "10.2.1.0/24",
               "ResourceComplianceStatus": "compliant",
               "ResourceOverlapStatus": "nonoverlapping",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           }
       ]
   }
   ```

**场景 3：2021 年 12 月 1 日至 2021 年 12 月 27 日 (UTC) 有哪些资源与 `2605:9cc0:409::/56` 关联？**

1. 运行以下命令，其中 --region 是指 IPAM 主区域：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 2605:9cc0:409::/56 --ipam-scope-id ipam-scope-07cb485c8b4a4d7cc --start-time 2021-12-01T01:00:00.000Z --end-time 2021-12-27T23:59:59.000Z
   ```

1. 查看分析结果。在下面的示例中，在 IPAM 主区域以外的区域，CIDR 在一段时间内分配给两个不同的 VPC。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-2",
               "ResourceType": "vpc",
               "ResourceId": "vpc-01d967bf3b923f72c",
               "ResourceCidr": "2605:9cc0:409::/56",
               "ResourceName": "First example VPC",
               "ResourceComplianceStatus": "compliant",
               "ResourceOverlapStatus": "nonoverlapping",
               "VpcId": "vpc-01d967bf3b923f72c",
               "SampledStartTime": "2021-12-23T20:02:00.701000+00:00",
               "SampledEndTime": "2021-12-23T20:12:59.848000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-2",
               "ResourceType": "vpc",
               "ResourceId": "vpc-03e62c7eca81cb652",
               "ResourceCidr": "2605:9cc0:409::/56",
               "ResourceName": "Second example VPC",
               "ResourceComplianceStatus": "compliant",
               "ResourceOverlapStatus": "nonoverlapping",
               "VpcId": "vpc-03e62c7eca81cb652",
               "SampledStartTime": "2021-12-27T15:11:00.046000+00:00"
           }
       ]
   }
   ```

**场景 4：在过去的 24 小时内，有哪些资源与 `10.0.0.0/24` 关联（假设当前时间是 2021 年 12 月 27 日午夜 (UTC)）？**

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 10.0.0.0/24 --ipam-scope-id ipam-scope-05b579a1909c5fc7a --start-time 2021-12-27T00:00:00.000Z
   ```

1. 查看分析结果。在下面的示例中，CIDR 已在一段时间内分配给多个子网和 VPC。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-2",
               "ResourceType": "subnet",
               "ResourceId": "subnet-0d1b8f899725aa72d",
               "ResourceCidr": "10.0.0.0/24",
               "ResourceName": "Example name",
               "VpcId": "vpc-042b8a44f64267d67",
               "SampledStartTime": "2021-12-11T16:35:59.074000+00:00",
               "SampledEndTime": "2021-12-28T15:34:00.017000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-2",
               "ResourceType": "vpc",
               "ResourceId": "vpc-09754dfd85911abec",
               "ResourceCidr": "10.0.0.0/24",
               "ResourceName": "Example name",
               "ResourceComplianceStatus": "unmanaged",
               "ResourceOverlapStatus": "overlapping",
               "VpcId": "vpc-09754dfd85911abec",
               "SampledStartTime": "2021-12-27T20:07:59.947000+00:00",
               "SampledEndTime": "2021-12-28T15:34:00.017000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-west-2",
               "ResourceType": "vpc",
               "ResourceId": "vpc-0a8347f594bea5901",
               "ResourceCidr": "10.0.0.0/24",
               "ResourceName": "Example name",
               "ResourceComplianceStatus": "unmanaged",
               "ResourceOverlapStatus": "overlapping",
               "VpcId": "vpc-0a8347f594bea5901",
               "SampledStartTime": "2021-12-11T16:35:59.318000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "subnet",
               "ResourceId": "subnet-0af7eadb0798e9148",
               "ResourceCidr": "10.0.0.0/24",
               "ResourceName": "Example name",
               "VpcId": "vpc-03298ba16756a8736",
               "SampledStartTime": "2021-12-14T21:07:22.357000+00:00"
           }
       ]
   }
   ```

**场景 5：当前有哪些资源与 `10.2.1.155/32` 关联？**

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 10.2.1.155/32 --ipam-scope-id ipam-scope-05b579a1909c5fc7a
   ```

1. 查看分析结果。在下面的示例中，CIDR 在一段时间内分配给网络接口和 EC2 实例。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "network-interface",
               "ResourceId": "eni-0b4e53eb1733aba16",
               "ResourceCidr": "10.2.1.155/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "instance",
               "ResourceId": "i-064da1f79baed14f3",
               "ResourceCidr": "10.2.1.155/32",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           }
       ]
   }
   ```

**场景 6：当前有哪些资源与 `10.2.1.0/24` 关联？**

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 10.2.1.0/24 --ipam-scope-id ipam-scope-05b579a1909c5fc7a
   ```

1. 查看分析结果。在下面的示例中，CIDR 在一段时间内分配给 VPC 和子网。只返回与此 `/24` CIDR 完全匹配的结果，而不是 `/24` CIDR 中的所有 `/32 `。请注意，没有 **SampledEndTime** 值表示记录仍处于活动状态。有关以下输出中显示的值的更多信息，请参阅 [查看 IP 地址历史记录](view-history-cidr-ipam.md)。

   ```
   {
       "HistoryRecords": [
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "subnet",
               "ResourceId": "subnet-0864c82a42f5bffed",
               "ResourceCidr": "10.2.1.0/24",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           },
           {
               "ResourceOwnerId": "123456789012",
               "ResourceRegion": "us-east-1",
               "ResourceType": "vpc",
               "ResourceId": "vpc-0f5ee7e1ba908a378",
               "ResourceCidr": "10.2.1.0/24",
               "ResourceComplianceStatus": "compliant",
               "ResourceOverlapStatus": "nonoverlapping",
               "VpcId": "vpc-0f5ee7e1ba908a378",
               "SampledStartTime": "2021-12-27T20:08:46.672000+00:00"
           }
       ]
   }
   ```

**场景 7：当前有哪些资源与 `54.0.0.9/32` 关联？**

在此示例中，`54.0.0.9/32` 将分配给不属于与 IPAM 集成的 AWS 企业的弹性 IP 地址。

1. 运行如下命令：

   ```
   aws ec2 get-ipam-address-history --region us-east-1 --cidr 54.0.0.9/32 --ipam-scope-id ipam-scope-05b579a1909c5fc7a
   ```

1. 由于 `54.0.0.9/32` 分配给不属于此示例中与 IPAM 集成的 AWS 企业的弹性 IP 地址，因此不会返回任何记录。

   ```
   {
       "HistoryRecords": []
   }
   ```