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)