I am writing a Python program which spins up an AWS EC3 instance, which I then use Paramiko to SSH into and run some code. The problem is that about 50% of the time that I run the code I get this error:
paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted
Then when I run it again, it works.
Here is my code:
import sys
import os
import boto3
import time
import paramiko
import pexpect
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
# create a local file to store keypair
outfile = open('ec2-keypair.pem','w')
# use boto ec2 to create new keypair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')
# store keypair in a file
key_pair_to_write = str(key_pair.key_material)
outfile.write(key_pair_to_write)
pexpect.run("chmod 400 ec2-keypair.pem")
outfile.close()
instances = ec2.create_instances(
ImageId='ami-0be057a22c63962cb',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName='ec2-keypair',
SecurityGroupIds=[
'<my sec groyp>',
],
)
instance = instances[0]
# Wait for the instance to enter the running state
instance.wait_until_running()
# Reload the instance attributes
instance.load()
dns = instance.public_dns_name
time.sleep(30)
try:
if len(sys.argv) > 2 or len(sys.argv) < 2:
print("Yah dun it wrong")
else:
difficulty = int(sys.argv[1])
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(dns, username="ubuntu", key_filename=os.path.expanduser('ec2-keypair.pem'))
ftp_client=ssh_client.open_sftp()
ftp_client.put('steps_pow.py','/home/ubuntu/steps_pow.py')
ftp_client.close()
command = "python3 steps_pow.py " + str(difficulty) + " 10"
stdin,stdout,stderr=ssh_client.exec_command(command)
print(stdout.readlines())
finally:
client.delete_key_pair(KeyName='ec2-keypair')
os.remove("ec2-keypair.pem")
client.terminate_instances(InstanceIds=[instance.id])
I did a silly fix just by using a try-except statement which reruns the connection, but this still returns the error sometimes.