check-cidr-list/1.rb

51 lines
1011 B
Ruby
Raw Normal View History

2024-09-24 09:10:26 +03:00
#!/usr/bin/env ruby
require 'net/http'
require 'resolv'
def validate_cidr?(cidr)
# Check for IPv4 CIDR format
return false unless cidr =~ /^([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$/
# Parse network address and prefix length
network_address, prefix_length = cidr.split('/')
# Validate network address
begin
Resolv::DNS.new.getaddress(network_address)
rescue Resolv::ResolvError
return false
end
# Validate prefix length
return false if prefix_length.to_i > 32
true
end
def process_url(url)
begin
uri = URI(url)
response = Net::HTTP.get(uri)
rescue URI::InvalidURIError, Net::HTTPError => e
puts "Error fetching URL: #{e.message}"
return
end
cidrs = response.split("\n")
cidrs.each do |cidr|
if validate_cidr?(cidr)
puts "Valid CIDR: #{cidr}"
else
puts "Invalid CIDR: #{cidr}"
end
end
end
# Get the URL from the user
puts "Enter the URL containing the IPv4 CIDR list:"
url = gets.chomp
# Process the URL
process_url(url)