module NetSpace

Public Class Methods

ip2num(ip) click to toggle source

Utility for converting a string IP (e.g. “127.0.0.1”) to it's 32-bit integer equivalent (e.g., 2130706433).

# File lib/netspace.rb, line 36
def self.ip2num(ip)
  return ip.split(/\./).map {|i| i.to_i}.pack("C*").unpack("N")[0]
end
load(file) click to toggle source

Load a file containing CIDR subnet definitions, one per line. The result will be an array of Subnet objects.

# File lib/netspace.rb, line 9
def self.load(file)
  NetSpace::parse(File.read(file))
end
num2ip(num) click to toggle source

Utility for converting a 32-bit integer IP (e.g. 2130706433) to it's string equivalent (e.g., “127.0.0.1”).

# File lib/netspace.rb, line 45
def self.num2ip(num)
  return [num].pack("N").unpack("C*").join(".")
end
parse(string) click to toggle source

Parse a string containing CIDR subnet definitions, one per line. The result will be an array of Subnet objects.

# File lib/netspace.rb, line 18
def self.parse(string)
  nets = string.split(/\n/).map do |line|
    begin
      line.strip!
      line =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/
      Subnet.new($1, $2.to_i)
    rescue
      nil
    end
  end
  return nets - [nil]
end