how to retrive aws-ec2 windows instance password using aws sdk in c#

馋奶兔 提交于 2019-12-20 04:13:02

问题


I am using aws sdk to retrieve cloud data from aws sdk. I get all ec2 related data but I am not able to find out how to connect instance.

Here is code of retrieve instance from amazon cloud:

IAmazonEC2 ec2Client = new AmazonEC2Client(accesskey,secretkey, new AmazonEC2Config
{
     Timeout = TimeSpan.FromSeconds(300),
     MaxErrorRetry = 3,
     RegionEndpoint = RegionEndpoint.GetBySystemName(regionName)
 });
 var instanceRequest = new DescribeInstancesRequest();
 DescribeInstancesResponse ec2Response = ec2Client.DescribeInstances(instanceRequest);

回答1:


First of you will need a key/pair file that you've used while creating an instance as it is needed to retrieve windows password.

Following are the steps to retrieve windows instance password using AWS SDK:

#1. You need to pass instanceId as well as RSA key from your .pem file to the following code.

    IAmazonEC2 ec2Client = new AmazonEC2Client(accesskey, secretkey, new AmazonEC2Config
     {
                Timeout = TimeSpan.FromSeconds(300),
                MaxErrorRetry = 3,
                RegionEndpoint = region,
     });

     var passwordRequest = new GetPasswordDataRequest();
     passwordRequest.InstanceId = instanceId;
     var passwordResponse = ec2Client.GetPasswordData(passwordRequest);
     var password = passwordResponse.GetDecryptedPassword(rsaKey);
     return password;
});

Note: You have to wait at least 4 minutes after launching an instance to get the windows password.



来源:https://stackoverflow.com/questions/38499769/how-to-retrive-aws-ec2-windows-instance-password-using-aws-sdk-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!