Ipcalc         IP calculation with Ruby



Documentation



Create an IP


require "ipcalc"

ip = Ip.new("192.168.12.10")	
ip = Ip.new("192.168.120.100.12.10")	
ip = Ip.new(90032000)
ip = Ip.new(90032000, 6) # Force IP to be IPV6
ip = Ip.new(0xAABBCCDD)
ip = Ip.new(0xAABBCCDD, 6) # Force IP to be IPV6
ip = Ip.new(0b10101101010101011111010101000001)
ip = Ip.new(0b10101101010101011111010101000001, 6) # Force IP to be IPV6

Print an IP


require "ipcalc"

ip = Ip.new("192.168.12.10")
puts "Ip(\"#{myip}\").to_i            = #{ip.to_i}"
puts "Ip(\"#{myip}\").to_bin          = #{ip.to_bin}"   
puts "Ip(\"#{myip}\").to_hex          = #{ip.to_hex}"
puts "Ip(\"#{myip}\").to_bin (pretty) = #{ip.to_bin(true)}"
puts "Ip(\"#{myip}\").to_hex (pretty) = #{ip.to_hex(true)}"

Increment/decrement (+, -, +=, -=)


require "ipcalc"

ip = Ip.new("192.168.12.10")
puts "#{ip.to_s.ljust(16)} +1    => #{ip+1}"
puts "#{ip.to_s.ljust(16)} +2    => #{ip+2}"
puts "#{ip.to_s.ljust(16)} -10   => #{ip-10}"
puts "#{ip.to_s.ljust(16)} += 1  => #{ip += 1}"
puts "#{ip.to_s.ljust(16)} -= 1  => #{ip -= 1}"

Comparing IPs (>, <, >=, <=, <=>, ==, !=)


require "ipcalc"

ip = Ip.new("192.168.12.10")
ip1 = ip+1
ip2 = ip-5

puts "#{ip1.to_s.ljust(16)} ==  #{ip2.to_s.ljust(16)} => #{ip1 ==  ip2}"
puts "#{ip1.to_s.ljust(16)} !=  #{ip2.to_s.ljust(16)} => #{ip1 !=  ip2}"
puts "#{ip1.to_s.ljust(16)} >   #{ip2.to_s.ljust(16)} => #{ip1 >   ip2}"
puts "#{ip1.to_s.ljust(16)} <   #{ip2.to_s.ljust(16)} => #{ip1 <   ip2}"
puts "#{ip1.to_s.ljust(16)} >=  #{ip2.to_s.ljust(16)} => #{ip1 >=  ip2}"
puts "#{ip1.to_s.ljust(16)} <=  #{ip2.to_s.ljust(16)} => #{ip1 <=  ip2}"
puts "#{ip1.to_s.ljust(16)} <=> #{ip2.to_s.ljust(16)} => #{ip1 <=> ip2}"

IP as array (IP[n])


require "ipcalc"

ip = Ip.new("192.168.12.10")
puts "ip = #{ip}"
puts "  0: #{ip[0]}"
puts "  1: #{ip[1]}"
puts "  2: #{ip[2]}"
puts "  3: #{ip[3]}"
puts

ip[2] = 10
puts "Changed the third dot into 10"
puts "Now ip = #{ip}"
puts

Bitwise operations (>>, <<, |, & ^)


require "ipcalc"

ip   = Ip.new('192.168.12.10') 
ipv6 = Ip.new('192.168.120.100.12.10')

puts "Testing >> on IPV#{ip.version} on #{ip}"
puts "#{ip.to_bin}"
puts "#{(ip >> 1).to_bin}"
puts "#{(ip >> 2).to_bin}"
puts

puts "Testing >> on IPV#{ipv6.version}"
puts "#{ipv6.to_bin}"
puts "#{(ipv6 >> 1).to_bin}"
puts "#{(ipv6 >> 2).to_bin}"
puts

puts "Testing <<"
puts "#{ip.to_bin} on IPV#{ip.version} in #{ip}"
puts "#{(ip << 1).to_bin}"
puts "#{(ip << 2).to_bin}"
puts

puts "Testing << on IPV#{ipv6.version}"
puts "#{ipv6.to_bin}"
puts "#{(ipv6 << 1).to_bin}"
puts "#{(ipv6 << 2).to_bin}"
puts  

puts "Testing | on IPV#{ip.version}"
ip1 = ip << 1
puts "#{ip.to_bin} #{ip}"
puts "#{ip1.to_bin} #{ip1}"
puts "#{(ip | ip1).to_bin} #{ip | ip1}"
puts

puts "Testing | on IPV#{ipv6.version}"
ip1v6 = ipv6 << 1
puts "#{ipv6.to_bin} #{ipv6}"
puts "#{ip1v6.to_bin} #{ip1v6}"
puts "#{(ipv6 | ip1v6).to_bin} #{ipv6 | ip1v6}"
puts

puts "Testing & on IPV#{ip.version}"
ip1 = ip << 1
puts "#{ip.to_bin}"
puts "#{ip1.to_bin}"
puts "#{(ip & ip1).to_bin}"
puts

puts "Testing & on IPV#{ipv6.version}"
ip1v6 = ipv6 << 1
puts "#{ipv6.to_bin}"
puts "#{ip1v6.to_bin}"
puts "#{(ipv6 & ip1v6).to_bin}"
puts

puts "Testing ^ on IPV#{ip.version}"
ip1 = ip << 1
puts "#{ip.to_bin}"
puts "#{ip1.to_bin}"
puts "#{(ip ^ ip1).to_bin}"
puts

puts "Testing ^ on IPV#{ipv6.version}"
ip1v6 = ipv6 << 1
puts "#{ipv6.to_bin}"
puts "#{ip1v6.to_bin}"
puts "#{(ipv6 ^ ip1v6).to_bin}"
puts 

Subnet discovery and subnet testing


require "ipcalc"

ip   = Ip.new('192.168.12.10')
ipv6 = Ip.new('192.168.120.100.12.10')

# --Example for IPV4--

# Discovery
puts "List of subnets that contain the IP #{ip}:"
32.downto(1) do |i|
  puts "   " + ip.cidr(i)  
end
puts

# Testing
puts "Is #{ip} included in \"1.2.3.128/25\" ? #{ip.included_in?("1.2.3.128/25")}"
puts

# --Example for IPV6--

# Discovery
puts "List of subnets that contain the IP #{ipv6}:"
48.downto(1) do |i|
  puts "   " + ipv6.cidr(i)  
end
puts

# Testing
puts "Is #{ipv6} included in \"1.2.3.128.0.1/25\" ? #{ipv6.included_in?("1.2.3.128.0.1/25")}"
puts