From a9bfdc9e237096698e961be34c3065ed8401d451 Mon Sep 17 00:00:00 2001 From: Eliezer Croitoru Date: Tue, 9 Apr 2024 01:24:28 +0300 Subject: [PATCH] added ipv4 cidr and addresses filter --- spf-to-ip4-list.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spf-to-ip4-list.rb b/spf-to-ip4-list.rb index 0eb2fd6..cab54af 100755 --- a/spf-to-ip4-list.rb +++ b/spf-to-ip4-list.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby #require 'spf/query' +require 'ipaddr' domain = ARGV[0] @@ -173,7 +174,30 @@ def getAllSpfIp4(domain) base_dom["ipv4"].each { |cidr| ipv4 << cidr } - return ipv4 + # filter ipv4 list + new_ipv4 = [] + ipv4.each do |ip| + if valid_cidr?(ip) or valid_ip_address?(ip) + new_ipv4 << ip + end + end + + return new_ipv4 +end + + +def valid_cidr?(input) + IPAddr.new(input) + true +rescue IPAddr::InvalidAddressError + false +end + +def valid_ip_address?(input) + IPAddr.new(input) + true +rescue IPAddr::InvalidAddressError + false end puts getAllSpfIp4(domain)