如何使用AWS CLI查找EC2实例的操作系统

时间:2014-04-09 00:43:05

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

如何使用AWS CLI查找在EC2实例上运行的操作系统。

ec2 describe-instance命令会发出大量信息,但没有任何信息表明操作系统。

我还尝试了ec2 describe-images特定图片。同样,似乎没有任何OS的迹象。

帮助..?

4 个答案:

答案 0 :(得分:6)

这是列出平台字段的快速方法,至少可以区分Windows和Linux:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text
i-78b4ef47  windows
i-b8ae3386  windows
i-9d3611a2  None
i-1c57c651  windows
i-a241ec91  None
i-7d26b630  None

答案 1 :(得分:3)

尝试此命令:

aws ec2 describe-images --image-ids $(aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxx --query 'Reservations[0].Instances[0].ImageId' --output text) --query 'Images[0].Name'

$()部分使用InstanceId获取ImageId。

答案 2 :(得分:1)

您无法从AWS cli查询实例的特定操作系统,但您可以查询该实例所基于的AMI。此外,您无法获得“操作系统”属性,但您可以获得AMI的DescriptionName,因此如果您创建具有有意义描述的AMI,则可以使其正常工作。

$ aws ec2 describe-images --image-ids "ami-xxxxxxxx"
{
    "Images": [
        {
            "VirtualizationType": "paravirtual", 
            "Name": "amazon-linux-20130509", 
            "Tags": [
                {
                    "Value": "amazon-linux-20130509", 
                    "Key": "Name"
                }
            ], 
            "Hypervisor": "xen", 
            "ImageId": "ami-xxxxxxxx", 
            "RootDeviceType": "ebs", 
            "State": "available", 
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/sda1", 
                    "Ebs": {
                        "DeleteOnTermination": true, 
                        "SnapshotId": "snap-xxxxxxxx", 
                        "VolumeSize": 100, 
                        "VolumeType": "standard"
                    }
                }
            ], 
            "Architecture": "x86_64", 
            "ImageLocation": "123456789012/amazon-linux-20130509", 
            "KernelId": "aki-fc37bacc", 
            "OwnerId": "123456789012", 
            "RootDeviceName": "/dev/sda1", 
            "Public": false, 
            "ImageType": "machine", 
            "Description": "Amazon Linux"
        }
    ]
}

如果您想要更详细,可以随时编写自己的脚本ssh到机器中并在每个机器中运行cat /etc/issue

答案 3 :(得分:1)

如果您在实例上安装了System Manager代理,则可以使用DescribeInstanceInformation API查找该信息:

$ aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformType,PlatformName]' --output text | sort

i-016073859e4b31111 Linux   Amazon Linux AMI
i-01fa3efe71e4b1111 Linux   Amazon Linux AMI
i-03d437d24f7341111 Windows Microsoft Windows Server 2012 R2 Standard
i-048fa3ba0aa151111 Windows Microsoft Windows Server 2012 R2 Standard
i-05e27c562eb881111 Linux   Amazon Linux AMI
i-09283c3c05d551111 Windows Microsoft Windows Server 2012 R2 Standard
i-0a51eb40351911111 Linux   Amazon Linux AMI
i-0a5aeab8f56ba1111 Linux   Amazon Linux AMI
i-0a61968dc51ba1111 Linux   Amazon Linux AMI
i-0a84d5b23e5251111 Linux   Amazon Linux AMI
i-0b057729594791111 Windows Microsoft Windows Server 2012 R2 Standard
i-0b1d0a7fb339b1111 Linux   Amazon Linux AMI
i-0da2fefde50351111 Linux   Amazon Linux AMI
i-0eafb22a9581a1111 Linux   Amazon Linux AMI