本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
枚举授权组
此示例向您展示如何使用 适用于 .NET 的 AWS 开发工具包 列举授权组。如果您提供 Amazon Virtual Private Cloud ID,应用程序枚举该特定VPC的安全组。否则,应用程序只需显示所有可用授权组的列表。
以下部分提供了此示例的片段。的 示例的完整代码 之后显示,并且可以按原样构建和运行。
枚举安全组
以下代码段枚举了您的安全组。它列举特定VPC的所有组或组(如果给定)。
示例 在本主题结束时 显示此片段正在使用。
// // Method to enumerate the security groups private static async Task EnumerateGroups(IAmazonEC2 ec2Client, string vpcID) { // A request object, in case we need it. var request = new DescribeSecurityGroupsRequest(); // Put together the properties, if needed if(!string.IsNullOrEmpty(vpcID)) { // We have a VPC ID. Find the security groups for just that VPC. Console.WriteLine($"\nGetting security groups for VPC {vpcID}...\n"); request.Filters.Add(new Filter { Name = "vpc-id", Values = new List<string>() { vpcID } }); } // Get the list of security groups DescribeSecurityGroupsResponse response = await ec2Client.DescribeSecurityGroupsAsync(request); // Display the list of security groups. foreach (SecurityGroup item in response.SecurityGroups) { Console.WriteLine("Security group: " + item.GroupId); Console.WriteLine("\tGroupId: " + item.GroupId); Console.WriteLine("\tGroupName: " + item.GroupName); Console.WriteLine("\tVpcId: " + item.VpcId); Console.WriteLine(); } }
完整代码
本节显示此示例的相关参考和完整代码。
NuGet 程序包:
编程元素:
-
命名空间 亚马逊.EC2
职业 AmazonEC2客户
-
命名空间 Amazon.EC2.型号
职业 描述安全组请求
职业 描述安全组响应
职业 过滤器
职业 安全组
using System; using System.Threading.Tasks; using System.Collections.Generic; using Amazon.EC2; using Amazon.EC2.Model; namespace EC2EnumerateSecGroups { class Program { static async Task Main(string[] args) { // Parse the command line string vpcID = string.Empty; if(args.Length == 0) { Console.WriteLine("\nEC2EnumerateSecGroups [vpc_id]"); Console.WriteLine(" vpc_id - The ID of the VPC for which you want to see security groups."); Console.WriteLine("\nSince you specified no arguments, showing all available security groups."); } else { vpcID = args[0]; } if(vpcID.StartsWith("vpc-") || string.IsNullOrEmpty(vpcID)) { // Create an EC2 client object var ec2Client = new AmazonEC2Client(); // Enumerate the security groups await EnumerateGroups(ec2Client, vpcID); } else { Console.WriteLine("Could not find a valid VPC ID in the command-line arguments:"); Console.WriteLine($"{args[0]}"); } } // // Method to enumerate the security groups private static async Task EnumerateGroups(IAmazonEC2 ec2Client, string vpcID) { // A request object, in case we need it. var request = new DescribeSecurityGroupsRequest(); // Put together the properties, if needed if(!string.IsNullOrEmpty(vpcID)) { // We have a VPC ID. Find the security groups for just that VPC. Console.WriteLine($"\nGetting security groups for VPC {vpcID}...\n"); request.Filters.Add(new Filter { Name = "vpc-id", Values = new List<string>() { vpcID } }); } // Get the list of security groups DescribeSecurityGroupsResponse response = await ec2Client.DescribeSecurityGroupsAsync(request); // Display the list of security groups. foreach (SecurityGroup item in response.SecurityGroups) { Console.WriteLine("Security group: " + item.GroupId); Console.WriteLine("\tGroupId: " + item.GroupId); Console.WriteLine("\tGroupName: " + item.GroupName); Console.WriteLine("\tVpcId: " + item.VpcId); Console.WriteLine(); } } } }
其他注意事项
-
请注意,VPC案例的筛选器是用
Name
名称-值对的一部分设置为“vpc-id”。此名称来自Filters
的 描述安全组请求 类。
-
要获取授权组的完整列表,您还可以使用 描述安全组与无参数的同步.
-
您可以通过检查中的授权组列表来验证结果 Amazon EC2 控制台
.