枚举安全组 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

是否要将 .NET 应用程序部署到Amazon通过几个简单点击即可? 尝试我们的新.NET CLI 工具为了简化部署体验!阅读我们的博客帖子然后提交反馈GitHub

有关更多信息,请参阅部署工具

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

枚举安全组

此示例向您演示如何使用Amazon SDK for .NET以枚举安全组。如果你提供Amazon Virtual Private CloudID,应用程序枚举该特定 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(); } }

代码完成

本节显示了此示例的相关参考文献和完整代码。

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的财产描述安全组请求类。