搭DescribeAddresses配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeAddresses配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用DescribeAddresses

C++
SDK對於 C ++
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! Describe all Elastic IP addresses. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeAddresses( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeAddressesRequest request; Aws::EC2::Model::DescribeAddressesOutcome outcome = ec2Client.DescribeAddresses(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(20) << "InstanceId" << std::setw(15) << "Public IP" << std::setw(10) << "Domain" << std::setw(30) << "Allocation ID" << std::setw(25) << "NIC ID" << std::endl; const Aws::Vector<Aws::EC2::Model::Address> &addresses = outcome.GetResult().GetAddresses(); for (const auto &address: addresses) { Aws::String domainString = Aws::EC2::Model::DomainTypeMapper::GetNameForDomainType( address.GetDomain()); std::cout << std::left << std::setw(20) << address.GetInstanceId() << std::setw(15) << address.GetPublicIp() << std::setw(10) << domainString << std::setw(30) << address.GetAllocationId() << std::setw(25) << address.GetNetworkInterfaceId() << std::endl; } } else { std::cerr << "Failed to describe Elastic IP addresses:" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 如需詳API細資訊,請參閱AWS SDK for C++ API參考DescribeAddresses中的。

CLI
AWS CLI

範例 1:擷取有關您所有彈性 IP 地址的詳細資訊

以下 describe addresses 範例顯示有關您彈性 IP 地址的詳細資訊。

aws ec2 describe-addresses

輸出:

{ "Addresses": [ { "InstanceId": "i-1234567890abcdef0", "PublicIp": "198.51.100.0", "PublicIpv4Pool": "amazon", "Domain": "standard" }, { "Domain": "vpc", "PublicIpv4Pool": "amazon", "InstanceId": "i-1234567890abcdef0", "NetworkInterfaceId": "eni-12345678", "AssociationId": "eipassoc-12345678", "NetworkInterfaceOwnerId": "123456789012", "PublicIp": "203.0.113.0", "AllocationId": "eipalloc-12345678", "PrivateIpAddress": "10.0.1.241" } ] }

示例 2:要檢索您的彈性 IP 地址的詳細信息 EC2-VPC

下列describe-addresses範例會顯示有關彈性 IP 位址的詳細資訊,以便與中的執行個體搭配使用VPC。

aws ec2 describe-addresses \ --filters "Name=domain,Values=vpc"

輸出:

{ "Addresses": [ { "Domain": "vpc", "PublicIpv4Pool": "amazon", "InstanceId": "i-1234567890abcdef0", "NetworkInterfaceId": "eni-12345678", "AssociationId": "eipassoc-12345678", "NetworkInterfaceOwnerId": "123456789012", "PublicIp": "203.0.113.0", "AllocationId": "eipalloc-12345678", "PrivateIpAddress": "10.0.1.241" } ] }

範例 3:擷取有關透過配置 ID 所指定的彈性 IP 地址的詳細資訊

下列describe-addresses範例會顯示有關具有指定配置 ID 的彈性 IP 位址的詳細資訊,這些 ID 與 EC2-中的執行個體相關聯VPC。

aws ec2 describe-addresses \ --allocation-ids eipalloc-282d9641

輸出:

{ "Addresses": [ { "Domain": "vpc", "PublicIpv4Pool": "amazon", "InstanceId": "i-1234567890abcdef0", "NetworkInterfaceId": "eni-1a2b3c4d", "AssociationId": "eipassoc-123abc12", "NetworkInterfaceOwnerId": "1234567891012", "PublicIp": "203.0.113.25", "AllocationId": "eipalloc-282d9641", "PrivateIpAddress": "10.251.50.12" } ] }

範例 4:擷取由其VPC私有 IP 位址指定的彈性 IP 位址的詳細資訊

下列describe-addresses範例會在-中顯示與特定私有 IP 位址相關聯的彈性 IP 位址的詳細資EC2訊VPC。

aws ec2 describe-addresses \ --filters "Name=private-ip-address,Values=10.251.50.12"

示例 5:在 EC2-經典中檢索有關彈性 IP 地址的詳細信息

TThe以下示describe-addresses例顯示有關您的彈性 IP 地址在 EC2-Classic 使用的詳細信息。

aws ec2 describe-addresses \ --filters "Name=domain,Values=standard"

輸出:

{ "Addresses": [ { "InstanceId": "i-1234567890abcdef0", "PublicIp": "203.0.110.25", "PublicIpv4Pool": "amazon", "Domain": "standard" } ] }

範例 6:擷取有關透過其公有 IP 地址所指定的彈性 IP 地址的詳細資訊

下列describe-addresses範例會顯示有關彈性 IP 位址及值的詳細資訊203.0.110.25,該值與 EC2-Classic 中的執行個體相關聯。

aws ec2 describe-addresses \ --public-ips 203.0.110.25

輸出:

{ "Addresses": [ { "InstanceId": "i-1234567890abcdef0", "PublicIp": "203.0.110.25", "PublicIpv4Pool": "amazon", "Domain": "standard" } ] }
JavaScript
SDK對於 JavaScript (3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import { DescribeAddressesCommand, EC2Client } from "@aws-sdk/client-ec2"; /** * Describes the specified Elastic IP addresses or all of your Elastic IP addresses. * @param {{ allocationId: string }} options */ export const main = async ({ allocationId }) => { const client = new EC2Client({}); const command = new DescribeAddressesCommand({ // You can omit this property to show all addresses. AllocationIds: [allocationId], }); try { const { Addresses } = await client.send(command); const addressList = Addresses.map((address) => ` • ${address.PublicIp}`); console.log("Elastic IP addresses:"); console.log(addressList.join("\n")); } catch (caught) { if ( caught instanceof Error && caught.name === "InvalidAllocationID.NotFound" ) { console.warn(`${caught.message}. Please provide a valid AllocationId.`); } else { throw caught; } } };
  • 如需詳API細資訊,請參閱AWS SDK for JavaScript API參考DescribeAddresses中的。

PowerShell
用於的工具 PowerShell

範例 1:此範例說明 EC2-Classic 中執行個體的指定彈性 IP 位址。

Get-EC2Address -AllocationId eipalloc-12345678

輸出:

AllocationId : eipalloc-12345678 AssociationId : eipassoc-12345678 Domain : vpc InstanceId : i-87654321 NetworkInterfaceId : eni-12345678 NetworkInterfaceOwnerId : 12345678 PrivateIpAddress : 10.0.2.172 PublicIp : 198.51.100.2

範例 2:此範例說明您的彈性 IP 位址,適用於VPC. 此語法需要 PowerShell 版本 3 或更新版本。

Get-EC2Address -Filter @{ Name="domain";Values="vpc" }

範例 3:此範例說明 EC2-Classic 中執行個體的指定彈性 IP 位址。

Get-EC2Address -PublicIp 203.0.113.17

輸出:

AllocationId : AssociationId : Domain : standard InstanceId : i-12345678 NetworkInterfaceId : NetworkInterfaceOwnerId : PrivateIpAddress : PublicIp : 203.0.113.17

範例 4:此範例說明您在 EC2-Classic 中執行個體的彈性 IP 位址。此語法需要 PowerShell 版本 3 或更新版本。

Get-EC2Address -Filter @{ Name="domain";Values="standard" }

範例 5:此範例說明您的所有彈性 IP 位址。

Get-EC2Address

範例 6:此範例會針對篩選器中提供的執行個體 ID 傳回公用和私有 IP

Get-EC2Address -Region eu-west-1 -Filter @{Name="instance-id";Values="i-0c12d3f4f567ffb89"} | Select-Object PrivateIpAddress, PublicIp

輸出:

PrivateIpAddress PublicIp ---------------- -------- 10.0.0.99 63.36.5.227

範例 7:此範例會擷取所有 Elastic 及其IPs配置識別碼、關聯識別碼和執行個體識別碼

Get-EC2Address -Region eu-west-1 | Select-Object InstanceId, AssociationId, AllocationId, PublicIp

輸出:

InstanceId AssociationId AllocationId PublicIp ---------- ------------- ------------ -------- eipalloc-012e3b456789e1fad 17.212.120.178 i-0c123dfd3415bac67 eipassoc-0e123456bb7890bdb eipalloc-01cd23ebf45f7890c 17.212.124.77 eipalloc-012345678eeabcfad 17.212.225.7 i-0123d405c67e89a0c eipassoc-0c123b456783966ba eipalloc-0123cdd456a8f7892 37.216.52.173 i-0f1bf2f34c5678d09 eipassoc-0e12934568a952d96 eipalloc-0e1c23e4d5e6789e4 37.218.222.278 i-012e3cb4df567e8aa eipassoc-0d1b2fa4d67d03810 eipalloc-0123f456f78a01b58 37.210.82.27 i-0123bcf4b567890e1 eipassoc-01d2345f678903fb1 eipalloc-0e1db23cfef5c45c7 37.215.222.270

範例 8:此範例擷取符合標籤索引鍵「類別」的 EC2 IP 位址清單,其值為「Prod」

Get-EC2Address -Filter @{Name="tag:Category";Values="Prod"}

輸出:

AllocationId : eipalloc-0123f456f81a01b58 AssociationId : eipassoc-0d1b23a456d103810 CustomerOwnedIp : CustomerOwnedIpv4Pool : Domain : vpc InstanceId : i-012e3cb4df567e1aa NetworkBorderGroup : eu-west-1 NetworkInterfaceId : eni-0123f41d5a60d5f40 NetworkInterfaceOwnerId : 123456789012 PrivateIpAddress : 192.168.1.84 PublicIp : 34.250.81.29 PublicIpv4Pool : amazon Tags : {Category, Name}
  • 如需詳API細資訊,請參閱AWS Tools for PowerShell 指令程DescribeAddresses式參考中的。

SAP ABAP
SDK對於 SAP ABAP
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

TRY. oo_result = lo_ec2->describeaddresses( ) . " oo_result is returned for testing purposes. " DATA(lt_addresses) = oo_result->get_addresses( ). MESSAGE 'Retrieved information about Elastic IP addresses.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • 如需詳API細資訊,請參閱DescribeAddressesAWS SDK的以供SAPABAPAPI參考