【问题标题】:ssh key of newly created ec2 instance using boto使用 boto 新建 ec2 实例的 ssh 密钥
【发布时间】:2013-07-18 11:04:16
【问题描述】:

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

【问题讨论】:

  • 你找到答案了吗? - 遇到同样的问题。

标签: python amazon-ec2 boto


【解决方案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

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 2021-05-21
    • 2020-04-04
    • 1970-01-01
    • 2015-08-08
    • 2012-07-24
    • 2017-08-19
    • 1970-01-01
    相关资源
    最近更新 更多