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

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

DescribeRouteTables 搭配 a AWS SDK 或 CLI 使用

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

CLI
AWS CLI

描述您的路由表

下列describe-route-tables範例會擷取路由表的詳細資訊

aws ec2 describe-route-tables

輸出:

{ "RouteTables": [ { "Associations": [ { "Main": true, "RouteTableAssociationId": "rtbassoc-0df3f54e06EXAMPLE", "RouteTableId": "rtb-09ba434c1bEXAMPLE" } ], "PropagatingVgws": [], "RouteTableId": "rtb-09ba434c1bEXAMPLE", "Routes": [ { "DestinationCidrBlock": "10.0.0.0/16", "GatewayId": "local", "Origin": "CreateRouteTable", "State": "active" }, { "DestinationCidrBlock": "0.0.0.0/0", "NatGatewayId": "nat-06c018cbd8EXAMPLE", "Origin": "CreateRoute", "State": "blackhole" } ], "Tags": [], "VpcId": "vpc-0065acced4EXAMPLE", "OwnerId": "111122223333" }, { "Associations": [ { "Main": true, "RouteTableAssociationId": "rtbassoc-9EXAMPLE", "RouteTableId": "rtb-a1eec7de" } ], "PropagatingVgws": [], "RouteTableId": "rtb-a1eec7de", "Routes": [ { "DestinationCidrBlock": "172.31.0.0/16", "GatewayId": "local", "Origin": "CreateRouteTable", "State": "active" }, { "DestinationCidrBlock": "0.0.0.0/0", "GatewayId": "igw-fEXAMPLE", "Origin": "CreateRoute", "State": "active" } ], "Tags": [], "VpcId": "vpc-3EXAMPLE", "OwnerId": "111122223333" }, { "Associations": [ { "Main": false, "RouteTableAssociationId": "rtbassoc-0b100c28b2EXAMPLE", "RouteTableId": "rtb-07a98f76e5EXAMPLE", "SubnetId": "subnet-0d3d002af8EXAMPLE" } ], "PropagatingVgws": [], "RouteTableId": "rtb-07a98f76e5EXAMPLE", "Routes": [ { "DestinationCidrBlock": "10.0.0.0/16", "GatewayId": "local", "Origin": "CreateRouteTable", "State": "active" }, { "DestinationCidrBlock": "0.0.0.0/0", "GatewayId": "igw-06cf664d80EXAMPLE", "Origin": "CreateRoute", "State": "active" } ], "Tags": [], "VpcId": "vpc-0065acced4EXAMPLE", "OwnerId": "111122223333" } ] }

如需詳細資訊,請參閱 AWS VPC 使用者指南中的使用路由表

PHP
適用於 PHP 的 SDK
注意

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

/** * @param array $routeTableIds * @param array $filters * @return array */ public function describeRouteTables(array $routeTableIds = [], array $filters = []): array { $parameters = []; if($routeTableIds){ $parameters['RouteTableIds'] = $routeTableIds; } if($filters){ $parameters['Filters'] = $filters; } try { $paginator = $this->ec2Client->getPaginator("DescribeRouteTables", $parameters); $contents = []; foreach ($paginator as $result) { foreach ($result['RouteTables'] as $object) { $contents[] = $object['RouteTableId']; } } }catch (Ec2Exception $caught){ echo "There was a problem paginating the results of DescribeRouteTables: {$caught->getAwsErrorMessage()}\n"; throw $caught; } return $contents; }
PowerShell
for PowerShell 工具

範例 1:此範例說明所有路由表。

Get-EC2RouteTable

輸出:

DestinationCidrBlock : 10.0.0.0/16 DestinationPrefixListId : GatewayId : local InstanceId : InstanceOwnerId : NetworkInterfaceId : Origin : CreateRouteTable State : active VpcPeeringConnectionId : DestinationCidrBlock : 0.0.0.0/0 DestinationPrefixListId : GatewayId : igw-1a2b3c4d InstanceId : InstanceOwnerId : NetworkInterfaceId : Origin : CreateRoute State : active VpcPeeringConnectionId :

範例 2:此範例會傳回指定路由表的詳細資訊。

Get-EC2RouteTable -RouteTableId rtb-1a2b3c4d

範例 3:此範例說明指定 VPC 的路由表。

Get-EC2RouteTable -Filter @{ Name="vpc-id"; Values="vpc-1a2b3c4d" }

輸出:

Associations : {rtbassoc-12345678} PropagatingVgws : {} Routes : {, } RouteTableId : rtb-1a2b3c4d Tags : {} VpcId : vpc-1a2b3c4d
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DescribeRouteTables