有没有办法从实例中获取平台和 OS
Is there any way to get the platform and OS from the instances
我正在尝试从我的 AWS EC2 实例中获取一些信息。我想知道是否有办法提取像这样的信息:
| Platform | Version |
|-----------|---------------:|
| CentOS | 6.0 or 7.0 |
| Ubuntu | 10.04 or 12.04 |
| Windows | |
我想知道是否可以使用 SDK。我尝试使用 Python SDK Boto3 但没有结果。
除非您将该信息存储为 tags
,否则无法使用 SDK 或 CLI。 AWS 开发工具包和 CLI 可以帮助您获取在管理程序级别可用的信息。但是您要问的是在 VM 内部可用,而不是在虚拟机管理程序中可用。
虽然以下 CLI 命令可以为您提供一些帮助,但不能保证您将获得所有实例的平台信息。
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text
i-07843115f653771c8 windows
i-e34364c87d4cebd12 None
i-0493b6a67b31df018 None
我们可以获取@helloV已经回答的平台,但是没有办法直接获取OS。但是我们可以使用图像 id 通过一些字符串操作来确定 OS 名称。
使用下面的命令我们可以获得图像名称,其中我们在某种程度上具有操作系统名称。
aws ec2 describe-images --image-ids $(aws ec2 describe-instances --query 'Reservations[*].Instances[*].ImageId' --output text) --query 'Images[*].Name'
The output of the command is as follows:
[
"RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2",
"CentOS_7.5_Private",
"amzn2-ami-hvm-2.0.20191024.3-x86_64-gp2",
"amzn-ami-hvm-2018.03.0.20190826-x86_64-gp2",
"Windows_Server-2016-English-Full-Base-2019.11.13",
"Windows_Server-2019-English-Full-Base-2019.10.09"
]
此外,如果您的实例上安装了 SSM agent,您可以 运行 下面的命令来获取准确的操作系统名称。
aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformType,PlatformName]' --output text | sort
The output of this command is as follows:
i-xxxxxxxxxxxx Linux Amazon Linux AMI
i-xxxxxxxxxxxx Linux CentOS Linux
i-xxxxxxxxxxxx Linux Amazon Linux
i-xxxxxxxxxxxx Windows Microsoft Windows Server 2016 Datacenter
希望对您有所帮助!
如果您使用的是 boto3 sdk,文档会指出 Reservations[].Instances[].Platform
不会出现,除非 Platform='Windows'
。此函数将为您构建一个包含 InstanceId
键和 Platform
值的字典:
from boto3 import client
from botocore.exceptions import ClientError
def get_instance_platform(instance_ids:list) -> dict:
ec2 = client('ec2')
paginator = ec2.get_paginator('describe_instances')
try:
# paginator in case you have a lot of instances in account
pages = paginator.paginate(
InstanceIds=instance_ids
)
instances_with_platform = {}
for page in pages:
for reservation in page['Reservations']:
for instance in reservation['Instances']:
instances_with_platform[instance['InstanceId'] = instance.get('Platform','Linux/UNIX')
except ClientError as err:
print('BIG OOF')
# handle this in your own way, I just raise original exception
raise err
return instances_with_platform
我决定使用内置的分页器,这意味着即使帐户中有很多实例 (>50),它也能正常工作。
我正在尝试从我的 AWS EC2 实例中获取一些信息。我想知道是否有办法提取像这样的信息:
| Platform | Version |
|-----------|---------------:|
| CentOS | 6.0 or 7.0 |
| Ubuntu | 10.04 or 12.04 |
| Windows | |
我想知道是否可以使用 SDK。我尝试使用 Python SDK Boto3 但没有结果。
除非您将该信息存储为 tags
,否则无法使用 SDK 或 CLI。 AWS 开发工具包和 CLI 可以帮助您获取在管理程序级别可用的信息。但是您要问的是在 VM 内部可用,而不是在虚拟机管理程序中可用。
虽然以下 CLI 命令可以为您提供一些帮助,但不能保证您将获得所有实例的平台信息。
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text
i-07843115f653771c8 windows
i-e34364c87d4cebd12 None
i-0493b6a67b31df018 None
我们可以获取@helloV已经回答的平台,但是没有办法直接获取OS。但是我们可以使用图像 id 通过一些字符串操作来确定 OS 名称。 使用下面的命令我们可以获得图像名称,其中我们在某种程度上具有操作系统名称。
aws ec2 describe-images --image-ids $(aws ec2 describe-instances --query 'Reservations[*].Instances[*].ImageId' --output text) --query 'Images[*].Name' The output of the command is as follows: [ "RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2", "CentOS_7.5_Private", "amzn2-ami-hvm-2.0.20191024.3-x86_64-gp2", "amzn-ami-hvm-2018.03.0.20190826-x86_64-gp2", "Windows_Server-2016-English-Full-Base-2019.11.13", "Windows_Server-2019-English-Full-Base-2019.10.09" ]
此外,如果您的实例上安装了 SSM agent,您可以 运行 下面的命令来获取准确的操作系统名称。
aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformType,PlatformName]' --output text | sort The output of this command is as follows: i-xxxxxxxxxxxx Linux Amazon Linux AMI i-xxxxxxxxxxxx Linux CentOS Linux i-xxxxxxxxxxxx Linux Amazon Linux i-xxxxxxxxxxxx Windows Microsoft Windows Server 2016 Datacenter
希望对您有所帮助!
如果您使用的是 boto3 sdk,文档会指出 Reservations[].Instances[].Platform
不会出现,除非 Platform='Windows'
。此函数将为您构建一个包含 InstanceId
键和 Platform
值的字典:
from boto3 import client
from botocore.exceptions import ClientError
def get_instance_platform(instance_ids:list) -> dict:
ec2 = client('ec2')
paginator = ec2.get_paginator('describe_instances')
try:
# paginator in case you have a lot of instances in account
pages = paginator.paginate(
InstanceIds=instance_ids
)
instances_with_platform = {}
for page in pages:
for reservation in page['Reservations']:
for instance in reservation['Instances']:
instances_with_platform[instance['InstanceId'] = instance.get('Platform','Linux/UNIX')
except ClientError as err:
print('BIG OOF')
# handle this in your own way, I just raise original exception
raise err
return instances_with_platform
我决定使用内置的分页器,这意味着即使帐户中有很多实例 (>50),它也能正常工作。