Skip to content

Instantly share code, notes, and snippets.

@samullen
Created August 20, 2010 19:37
Show Gist options
  • Save samullen/540976 to your computer and use it in GitHub Desktop.
Save samullen/540976 to your computer and use it in GitHub Desktop.
Testing IP's against CIDR. Whitelisting blocks.
#!/usr/bin/ruby
require 'rubygems'
require 'ipaddr'
cidr = IPAddr.new("172.20.0.0")
# cidr = IPAddr.new("198.6.5.0")
mask = 16
# ip = IPAddr.new("198.6.5.22")
ip = IPAddr.new("172.20.155.101")
puts ip.to_i
puts cidr.to_i
puts ip.to_i & ~((1 << (32 - mask)) - 1) == cidr.to_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment