Example #1
0
        public static void CreateKeyPair(AmazonEC2Client ec2Client, string keyPairName, string privateKeyFile)
        {
            var request = new CreateKeyPairRequest();

            request.KeyName = keyPairName;

            try
            {
                var response = ec2Client.CreateKeyPair(request);
                Console.WriteLine();
                Console.WriteLine("New key: " + keyPairName);

                // Save the private key in a .pem file
                using (FileStream s = new FileStream(privateKeyFile, FileMode.Create))
                    using (StreamWriter writer = new StreamWriter(s))
                    {
                        writer.WriteLine(response.KeyPair.KeyMaterial);
                    }
            }
            catch (AmazonEC2Exception ex)
            {
                // Check the ErrorCode to see if the key already exists.
                if ("InvalidKeyPair.Duplicate" == ex.ErrorCode)
                {
                    Console.WriteLine("The key pair \"{0}\" already exists.", keyPairName);
                }
                else
                {
                    // The exception was thrown for another reason, so re-throw the exception.
                    throw;
                }
            }
        }
Example #2
0
        public static KeyPair CreateKeyPair()
        {
            var ec2Client = new AmazonEC2Client();
            CreateKeyPairRequest request = new CreateKeyPairRequest("MyNewKeyPair");

            return(ec2Client.CreateKeyPair(request).KeyPair);
        }
Example #3
0
        public KeyPair CreateKeyPair(string keyName)
        {
            AWSModel.CreateKeyPairRequest request = new AWSModel.CreateKeyPairRequest();
            request.KeyName = keyName;

            AWSModel.CreateKeyPairResponse response = ec2.CreateKeyPair(request);
            KeyPair keyPair = ModelUtil.CreateInstance(response.CreateKeyPairResult.KeyPair);

            return(keyPair);
        }
        public static KeyPair CreateKeyPair(AmazonEC2Client ec2Client)
        {
            var keyName = Args.Value("KeyName");
            var keyPair = ec2Client.CreateKeyPair(new CreateKeyPairRequest {
                KeyName = keyName
            }).KeyPair;
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"{keyName}.pem");

            File.WriteAllText(path, keyPair.KeyMaterial);
            Log.Debug($"They key pair was save to your desktop: {path}");
            return(keyPair);
        }
Example #5
0
        public void CreateaKeyPair()
        {
            //Create an Amazon EC2 Client Using the the SDK
            var ec2Client = new AmazonEC2Client();
            //enumerate the  key pairs
            string      keyPairName = "my-sample-key";
            KeyPairInfo myKeyPair   = null;

            var dkpRequest  = new DescribeKeyPairsRequest();
            var dkpResponse = ec2Client.DescribeKeyPairs(dkpRequest);
            List <KeyPairInfo> myKeyPairs = dkpResponse.KeyPairs;

            foreach (KeyPairInfo item in myKeyPairs)
            {
                Console.WriteLine("Existing key pair: " + item.KeyName);
                if (item.KeyName == keyPairName)
                {
                    myKeyPair = item;
                }
            }
            //create a key pair and save the private key
            if (myKeyPair == null)
            {
                var newKeyRequest = new CreateKeyPairRequest()
                {
                    KeyName = keyPairName
                };
                var ckpResponse = ec2Client.CreateKeyPair(newKeyRequest);
                Console.WriteLine();
                Console.WriteLine("New key: " + keyPairName);

                // Save the private key in a .pem file
                using (FileStream s = new FileStream(keyPairName + ".pem", FileMode.Create))
                    using (StreamWriter writer = new StreamWriter(s))
                    {
                        writer.WriteLine(ckpResponse.KeyPair.KeyMaterial);
                    }
            }
        }
        public void CreateKeyPair(string keyName, string fileName, string filePath)
        {
            AmazonEC2Client ec2 = new AmazonEC2Client();

            CreateKeyPairRequest keyReq = new CreateKeyPairRequest();

            keyReq.KeyName = "DevKey";

            CreateKeyPairResponse keyResp = ec2.CreateKeyPair(keyReq);

            var keyvalue1 = keyResp.KeyPair.KeyFingerprint;

            var k2 = keyResp.KeyPair.KeyMaterial;

            var k3 = keyResp.KeyPair.KeyName;

            string str = keyvalue1 + k2 + k3;

            using (FileStream fs = new FileStream(filePath + @"\" + fileName, FileMode.Append, FileAccess.Write))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(str);
                }
        }