ssh key of newly created ec2 instance using boto

邮差的信 提交于 2019-12-12 01:45:45

问题


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?


回答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


来源:https://stackoverflow.com/questions/17741382/ssh-key-of-newly-created-ec2-instance-using-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!