两者都不能创建新的密钥对,那我们为什么要拥有它们呢? 打印key_pair_info.key材料
谁能指出我Boto3
(资源级)KeyPair
和KeypairInfo
对象的目的是什么?文档再次模糊不清。
import boto3
ec2 = boto3.resource('ec2')
key_pair = ec2.KeyPair('name')
key_pair = ec2.KeyPairInfo('name')
重要的是,KeyPairInfo
根本不起作用:AttributeError: 'ec2.ServiceResource' object has no attribute 'KeyPairInfo'
此外:
AttributeError: 'ec2.KeyPairInfo' object has no attribute 'key_material'
.这是因为key_material
在创建时会提供给用户一次。仅供参考,我主要与botocore(client)
合作
ec2 = boto3.resource('ec2')
ec2.KeyPair('name') # Get the key fingerprint AND the private key
ec2.KeyPairInfo('name') # Get the key fingerprint ONLY
ec2 = boto3.client('ec2')
mykeypair = ec2.create_key_pair(KeyName='name') # Create a new keypair
print mykeypair['KeyMaterial']
你不能直接获取KeyPairInfo,但你可以使用这个来获取它:
key_pairs = EC2_RESOURCE.key_pairs.filter(
KeyNames=[
'my-ssh-key-pair',
],
)
for key in key_pairs:
print(f'SSH key "{key.key_name}" fingerprint: {key.key_fingerprint}')