0

我正在使用 boto 连接到 EC2 并启动一个实例。创建实例后,我需要 ssh 到它。我需要服务器的公共 ssh 密钥将其添加到我已知的主机文件中。如何使用 boto 获取密钥?我不想绕过密钥验证。我使用过 boto 命令 shell,但查看源代码,看起来 boto 使用 paramiko 并绕过检查 ssh 密钥。有人可以帮忙吗?

4

1 回答 1

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
于 2014-01-03T16:51:49.007 回答