This commit is contained in:
Eliezer Croitoru 2024-09-24 09:10:26 +03:00
commit 3933cf6cb7
2 changed files with 89 additions and 0 deletions

50
1.rb Normal file
View File

@ -0,0 +1,50 @@
#!/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)

39
2.rb Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env ruby
require 'net/http'
require 'netaddr'
def validate_cidr?(cidr)
begin
IPAddr.new(cidr)
return true
rescue IPAddr::InvalidAddressError
return false
end
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)