【问题标题】:Is there any way to get the platform and OS from the instances有没有办法从实例中获取平台和操作系统
【发布时间】:2017-05-01 13:02:04
【问题描述】:

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

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

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

【问题讨论】:

  • 无法使用 SDK 和 CLI。如果您有ssh 访问实例的权限,那么Ansible 可以获得您想要的。
  • 是的,这是一个选项,但我无法使用 Ansible。好吧,谢谢您的快速回答@helloV
  • 您可以获得的关闭是找出每个实例的 AMI 映像 ID。然后查询 AMI。但这仅与 AMI 的版本一样好。例如我的更新版本的补丁。
  • 我为你做了一个boto3

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


【解决方案1】:

除非您将该信息存储为 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

【讨论】:

    【解决方案2】:

    如果您使用的是 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) 也可以使用。

    【讨论】:

      【解决方案3】:

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

      aws ec2 describe-images --image-ids $(aws ec2 describe-instances --query 'Reservations[*].Instances[*].ImageId' --output text) --query 'Images[*].Name' 该命令的输出如下: [ “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]' --输出文本 |种类 该命令的输出如下: i-xxxxxxxxxxxx Linux Amazon Linux AMI i-xxxxxxxxxxxx Linux CentOS Linux i-xxxxxxxxxxxx Linux 亚马逊 Linux i-xxxxxxxxxxxx Windows Microsoft Windows Server 2016 数据中心

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2017-01-02
        • 2016-10-31
        • 2014-02-08
        • 2019-06-10
        • 2011-07-31
        • 2021-07-22
        • 2021-06-07
        • 2015-11-16
        • 1970-01-01
        相关资源
        最近更新 更多