有没有办法从实例中获取平台和操作系统

时间:2016-12-15 20:00:11

标签: python amazon-web-services amazon-ec2 aws-cli boto3

我正在尝试从我的AWS EC2实例中获取一些信息。我想知道是否有办法提取信息:

| Platform  | Version        |
|-----------|---------------:|
| CentOS    |  6.0 or 7.0    |
| Ubuntu    | 10.04 or 12.04 |
| Windows   |                |

我想知道使用SDK是否可行。我尝试使用Python SDK Boto3,但没有结果。

2 个答案:

答案 0 :(得分:2)

除非您将该信息存储为tags,否则无法使用SDK或CLI。 AWS开发工具包和CLI可以帮助您获取虚拟机管理程序级别提供的信息。但是你所要求的是在虚拟机内部,而不是在虚拟机管理程序中。

虽然以下CLI命令可以为您提供帮助,但无法保证您将获得所有实例的平台信息。

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text

i-07843115f653771c8 windows
i-e34364c87d4cebd12 None
i-0493b6a67b31df018 None

答案 1 :(得分:0)

我们可以获得@helloV已经回答的平台,但是没有办法直接获取操作系统。但是我们可以使用图像ID通过一些字符串操作来确定操作系统名称。 使用以下命令,我们可以获取映像名称,其中在某种程度上具有操作系统名称。

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

希望这会有所帮助!