简体   繁体   English

如何从AWS CLI命令过滤结果

[英]How to filter results from AWS CLI command

How can I filter the following so just results with IP starting with 10.* are returned? 我该如何过滤以下内容,以便仅返回IP以10. *开头的结果?

aws ec2 describe-instances --filters "Name=tag-value,Values=mytagavalue" --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,PrivateDnsName:PrivateDnsName,State:State.Name, IP:NetworkInterfaces[0].PrivateIpAddress}'
[
    [
        {
            "InstanceId": "i-12345bnmsdfod",
            "PrivateDnsName": "ip-10-34-24-4.my.there.com",
            "State": "running",
            "IP": "10.10.10.4"
        }
    ],
    [
        {
            "InstanceId": "i-12345bnmsdfop",
            "PrivateDnsName": "",
            "State": "terminated",
            "IP": null
        }
    ],

Option 1) Via Filters 选项1)通过过滤器

Use the network-interface.addresses.private-ip-address filter to select values matching only "10.*", which will match addresses starting with "10.". 使用network-interface.addresses.private-ip-address过滤器选择仅匹配“ 10. *”的值,该值将匹配以“ 10.”开头的地址。

--filters "Name=network-interface.addresses.private-ip-address,Values=10.*"

Simply include a space between different filters to delimit them. 只需在不同的过滤器之间包含一个空格即可对其进行界定。

Full Example 完整的例子

aws ec2 describe-instances --filters "Name=tag-value,Values=mytagavalue" "Name=network-interface.addresses.private-ip-address,Values=10.*" --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,PrivateDnsName:PrivateDnsName,State:State.Name, IP:NetworkInterfaces[0].PrivateIpAddress}'

Option 2) Via Query 选项2)通过查询

Use the JMESPath starts_with() function to perform a partial string comparison of "10." 使用JMESPath starts_with()函数执行部分字符串比较“ 10”。 against each network interface's private IP address. 针对每个网络接口的专用IP地址。

Step By Step 一步步

First, select all instances: 首先,选择所有实例:

Reservations[].Instances[]

Then pipe to filter for only instances containing network interfaces that have a private ip address starting with "10.": 然后通过管道筛选仅包含包含以“ 10”开头的私有IP地址的网络接口的实例:

| [? NetworkInterfaces [? starts_with(PrivateIpAddress, '10.')]]

Then select the fields just like you did before. 然后像以前一样选择字段。 This has not changed. 这没有改变。 (Note that you may want to select for all network interfaces instead of just the first.) (请注意,您可能需要选择所有网络接口,而不仅仅是第一个。)

.{InstanceId:InstanceId,PrivateDnsName:PrivateDnsName,State:State.Name, IP:NetworkInterfaces[0].PrivateIpAddress}"

Full Example 完整的例子

aws ec2 describe-instances --filters "Name=tag-value,Values=mytagavalue" --query "Reservations[].Instances[] | [? NetworkInterfaces [? starts_with(PrivateIpAddress, '10.')]].{InstanceId:InstanceId,PrivateDnsName:PrivateDnsName,State:State.Name, IP:NetworkInterfaces[0].PrivateIpAddress}"

Further Reading 进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM