commit 3933cf6cb7f3dff595b0b1141cacbdb08a324a5b Author: Eliezer Croitoru Date: Tue Sep 24 09:10:26 2024 +0300 1 diff --git a/1.rb b/1.rb new file mode 100644 index 0000000..45f87a7 --- /dev/null +++ b/1.rb @@ -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) diff --git a/2.rb b/2.rb new file mode 100644 index 0000000..0eb945a --- /dev/null +++ b/2.rb @@ -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)