如何将合并的IP地址与其他数组项匹配

时间:2017-04-21 17:09:38

标签: arrays ruby ip

我有三个数组,每个数组都包含IP地址:

.org

第四个看起来像这样的数组:

a = ['157.154.64.71/32', '156.154.64.71/32','156.154.64.72/32', '156.154.64.73/32', '156.154.64.76/24']
b = ['155.154.64.71/32', '158.154.64.71/32','158.154.64.72/32', '158.154.64.73/32', '158.154.64.76/24']
c = ['159.154.64.71/32', '159.154.64.71/32','159.154.64.72/32', '159.154.64.73/32', '159.154.64.76/24']

我需要将所有IP地址与gem NetAddr合并,所以首先我将三个数组合并,然后合并IP地址。

asn = [1,2,3]

其中给出了以下输出:

require 'netaddr'

combined_arrays = a + b + c

merged_ips = NetAddr.merge(combined_arrays)

我要做的是将数组puts merged_ips 155.154.64.71/32 156.154.64.0/24 157.154.64.71/32 158.154.64.0/24 159.154.64.0/24 ab中的合并IP与数组c的项匹配。< / p>

所以最终输出应如下所示:

asn

到目前为止我的代码看起来像这样:

155.154.64.71/32    2
156.154.64.0/24     1
157.154.64.71/32    1 
158.154.64.0/24     2
159.154.64.0/24     3

其中给出了以下输出:

merged_ips.each do |ip|
  asn.each do |a|
    puts "#{ip}\t #{a}\n"
  end
end

它重复所有内容三次,我不知道如何将所有IP地址与155.154.64.71/32 1 155.154.64.71/32 2 155.154.64.71/32 3 156.154.64.0/24 1 156.154.64.0/24 2 156.154.64.0/24 3 157.154.64.71/32 1 157.154.64.71/32 2 157.154.64.71/32 3 158.154.64.0/24 1 158.154.64.0/24 2 158.154.64.0/24 3 159.154.64.0/24 1 159.154.64.0/24 2 159.154.64.0/24 3 数组的正确项匹配。

0 个答案:

没有答案