View a markdown version of this page

列舉安全群組 - AWS SDK for .NET (V4)

第 4 版 (V4) AWS SDK for .NET 已發行!

如需有關中斷變更和遷移應用程式的資訊,請參閱遷移主題

Orange button with text "Click here for details".

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

列舉安全群組

此範例說明如何使用 AWS SDK for .NET 列舉安全群組。如果您提供 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 套件:

程式設計元素:

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"。此名稱來自 DescribeSecurityGroupsRequest 類別Filters屬性的描述。