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; } } }
/// <summary> /// Initializes the Amazon EC2 client object and then calls the /// CreateKeyPairAsync method to create the new key pair. /// </summary> public static async Task Main() { string keyName = "sdk-example-key-pair"; // If the default user on your system is not the same as // the region where you want to create the key pair, you // need to supply the AWS Region as a parameter to the // client constructor. var client = new AmazonEC2Client(); var request = new CreateKeyPairRequest { KeyName = keyName, }; var response = await client.CreateKeyPairAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { var kp = response.KeyPair; Console.WriteLine($"{kp.KeyName} with the ID: {kp.KeyPairId}."); } else { Console.WriteLine("Could not create key pair."); } }
public static KeyPair CreateKeyPair() { var ec2Client = new AmazonEC2Client(); CreateKeyPairRequest request = new CreateKeyPairRequest("MyNewKeyPair"); return(ec2Client.CreateKeyPair(request).KeyPair); }
public bool reload_key_pair(string private_key, string tag) { write_log("鍵を生成します。"); try { var client = get_client(); write_log(region + " のキーペアを削除しています。"); var delete_req = new DeleteKeyPairRequest(); delete_req.KeyName = Helper.build_name(setting_, tag); client.DeleteKeyPair(delete_req); var create_req = new CreateKeyPairRequest(); create_req.KeyName = Helper.build_name(setting_, tag); var create_res = client.CreateKeyPair(create_req); write_log("キーペア保存をしています。"); File.WriteAllText(private_key, create_res.KeyPair.KeyMaterial); } catch (Exception ex) { write_log("ERROR: " + ex.ToString()); return(false); } return(true); }
public async Task<string> CreateEC2KeyPair(OrchestratorSession session, string keyName, string saveLocation) { var ec2Client = _awsClientFactory.GetAWSClient<IAmazonEC2>(session.AWSCredentials, session.AWSRegion); var request = new CreateKeyPairRequest() { KeyName = keyName }; var response = await ec2Client.CreateKeyPairAsync(request); File.WriteAllText(Path.Combine(saveLocation, $"{keyName}.pem"), response.KeyPair.KeyMaterial); return response.KeyPair.KeyName; }
public void CreateKeyPair(string keyPairName, string privateKeyFile) { var request = new CreateKeyPairRequest(); request.KeyName = keyPairName; var response = ec2Client.CreateKeyPair(request); using (FileStream s = new FileStream(privateKeyFile, FileMode.Create)) using (StreamWriter writer = new StreamWriter(s)) { writer.WriteLine(response.KeyPair.KeyMaterial); } }
public string CreateKeyPair() { string keyPairName = "Ec2Test"; KeyPairInfo myKeyPair = null; try { var dkpRequest = new DescribeKeyPairsRequest(); var dkpResponse = amazonEC2Client.DescribeKeyPairsAsync(dkpRequest).Result; List <KeyPairInfo> myKeyPairs = dkpResponse.KeyPairs; foreach (KeyPairInfo item in myKeyPairs) { Console.WriteLine("Existing key pair: " + item.KeyName); if (item.KeyName == keyPairName) { myKeyPair = item; } } if (myKeyPair == null) { var newKeyRequest = new CreateKeyPairRequest() { KeyName = keyPairName }; var ckpResponse = amazonEC2Client.CreateKeyPairAsync(newKeyRequest).Result; 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); } } } catch (Exception ex) { throw ex; } return(keyPairName); }
public bool CreateKeyPair(string keyPairName, ref string keyPairPath) { if (!CheckKeyPair(keyPairName)) { var newKeyRequest = new CreateKeyPairRequest() { KeyName = keyPairName }; var ckpResponse = Ec2Client.CreateKeyPairAsync(newKeyRequest).GetAwaiter().GetResult(); string bucketName = Constants.KEY_PAIR_BUCKET_NAME; string bucketkeyName = $"{keyPairName}.pem"; bool successfulUpload = false; S3Helper s3Helper = new S3Helper(this.IsLocalDebug, bucketName, bucketkeyName); s3Helper.PushTextFileToS3Bucket(ckpResponse.KeyPair.KeyMaterial, ref successfulUpload); keyPairPath = $"Private key for this instance is stored in {bucketName} bucket and {bucketkeyName} key . Please download from there for connecting to the instance."; return(true); } return(false); }
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); } }
//once we get here we know the key file doesn't exist private void createKayPair() { try { string keyFileDir = CAwsConfig.getEc2BootstrapperDirectory(); if (Directory.Exists(keyFileDir) == false) { Directory.CreateDirectory(keyFileDir); } string keyFilePath = null; FolderBrowserDialog folder = new FolderBrowserDialog(); folder.ShowNewFolderButton = true; folder.SelectedPath = keyFileDir; folder.Description = "Please select directory where you want to save key file"; DialogResult result = DialogResult.No; while (result == DialogResult.No) { if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) { keyFilePath = folder.SelectedPath + "\\" + _keyPairName + ".pem"; if (File.Exists(keyFilePath)) { result = MessageBox.Show(null, "Key file " + keyFilePath + " exists. Do you want to overwrite it?", "Key File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } else { break; } } } CreateKeyPairRequest request = new CreateKeyPairRequest(); request.KeyName = _keyPairName; CreateKeyPairResponse response = _service.CreateKeyPair(request); if (response.IsSetCreateKeyPairResult()) { CreateKeyPairResult createKeyPairResult = response.CreateKeyPairResult; if (createKeyPairResult.IsSetKeyPair()) { using (FileStream stream = new FileStream( keyFilePath, FileMode.Create, FileAccess.Write)) { KeyPair keyPair = createKeyPairResult.KeyPair; if (keyPair.IsSetKeyMaterial()) { byte[] fileData = new UTF8Encoding(true).GetBytes(keyPair.KeyMaterial); stream.Write(fileData, 0, fileData.Length); CAwsConfig.Instance.setKeyFilePath(_keyPairName, keyFilePath); CAwsConfig.Instance.commit(); } } } } } catch (AmazonEC2Exception ex) { throw new Exception("Caught Exception: " + ex.XML); } }
/// <summary> /// Create Key Pair /// </summary> /// <param name="request">Create Key Pair request</param> /// <returns>Create Key Pair Response from the service</returns> /// <remarks> /// The CreateKeyPair operation creates a new 2048 bit RSA key pair and returns a /// unique ID that can be used to reference this key pair when launching new /// instances. For more information, see RunInstances. /// /// </remarks> public CreateKeyPairResponse CreateKeyPair(CreateKeyPairRequest request) { return(Invoke <CreateKeyPairResponse>("CreateKeyPairResponse.xml")); }