我正在使用boto连接到EC2并启动一个实例。创建实例后,我需要ssh到它。我需要服务器的公共ssh密钥将其添加到我的已知主机文件。如何使用boto获取密钥?我不想绕过密钥验证。我使用过boto命令shell,但是查看源代码,看起来boto使用paramiko并绕过检查ssh密钥。有人可以帮忙吗?
答案 0 :(得分:1)
# Check to see if specified keypair already exists.
# If we get an InvalidKeyPair.NotFound error back from EC2,
# it means that it doesn't exist and we need to create it.
try:
key = ec2.get_all_key_pairs(keynames=[key_name])[0]
except ec2.ResponseError, e:
if e.code == 'InvalidKeyPair.NotFound':
print 'Creating keypair: %s' % key_name
# Create an SSH key to use when logging into instances.
key = ec2.create_key_pair(key_name)
# AWS will store the public key but the private key is
# generated and returned and needs to be stored locally.
# The save method will also chmod the file to protect
# your private key.
key.save(key_dir)
else:
raise