使用boto新建的ec2实例的ssh密钥 [英] ssh key of newly created ec2 instance using boto
本文介绍了使用boto新建的ec2实例的ssh密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用boto连接到EC2并启动实例.创建实例后,我需要使用它.我需要服务器的公共ssh密钥才能将其添加到我的已知主机文件中.如何使用Boto获取密钥?我不想绕过密钥验证.我使用了boto命令外壳程序,但是从源代码来看,它看起来像boto使用paramiko并绕过了ssh密钥检查.谁能帮忙吗?
I am using boto to connect to EC2 and launch an instance. After creating the instance, I need to ssh to it. I need the public ssh key of the server to add that to my known hosts file. How do I get the key using boto? I do not want to bypass the key verification. I have used boto command shell, but looking at source, it looks like boto uses paramiko and bypasses checking the ssh key. Can anyone please help?
推荐答案
# 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
这篇关于使用boto新建的ec2实例的ssh密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文