【发布时间】:2018-06-03 22:54:56
【问题描述】:
我有以下 Groovy 代码,它查询 AWS 以接收正在使用的 CIDR 块列表并用它填充数组:
#!/usr/local/bin/groovy
def regions = ['us-west-2', 'us-east-1', 'eu-west-1']
def output = []
regions.each { region ->
def p = ['/usr/bin/aws', 'ec2', 'describe-vpcs', '--region', region].execute() | 'grep -w CidrBlock'.execute() | ['awk', '{print $2}'].execute() | ['tr', '-d', '"\\"\\|,\\|\\{\\|\\\\["'].execute() | 'uniq'.execute()
p.waitFor()
p.text.eachLine { line ->
output << line
}
}
output = output.sort { a, b ->
def aparts = a.split('[./]').collect { it as short }
def bparts = b.split('[./]').collect { it as short }
(0..4).collect { aparts[it] <=> bparts[it] }.find() ?: 0
}
output.each {
println it
}
在某些地区,CIDR 块为 172.31.0.0/16,而在其他地区为 10.100.0.0/16。
我希望脚本的输出仅包含 10.100.* CIDR 块,并且我不希望 172.* 网络出现在输出中。
当前输出如下所示:
itai@Itais-MacBook-Pro ~ - $ groovy populate_jenkins_parameters_cidr_blocks.groovy
172.30.0.0/16
172.31.0.0/16
10.100.0.0/16
10.105.0.0/16
怎么做?
【问题讨论】:
标签: regex amazon-web-services sorting groovy filtering