如何使用AWS CLI查找在EC2实例上运行的操作系统。
ec2 describe-instance
命令吐出了大量信息,但没有任何信息表明操作系统。
我还尝试了特定图像上的ec2 describe-images
。再一次,似乎没有任何OS的迹象。
救命..?
这是列出Platform字段的快速方法,至少可以区分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
试试这个命令:
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。
您无法从AWS cli查询实例的特定操作系统,但可以查询该实例所基于的AMI。此外,您无法获得'OS'属性,但您可以获得AMI的Description
或Name
,因此如果您创建具有有意义描述的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
。
如果您在实例上安装了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