Module: NetSpace
- Defined in:
- lib/netspace.rb,
lib/netspace/subnet.rb
Defined Under Namespace
Classes: Subnet
Class Method Summary collapse
-
.ip2num(ip) ⇒ Object
Utility for converting a string IP (e.g. “127.0.0.1”) to it’s 32-bit integer equivalent (e.g., 2130706433).
-
.load(file) ⇒ Object
Load a file containing CIDR subnet definitions, one per line.
-
.num2ip(num) ⇒ Object
Utility for converting a 32-bit integer IP (e.g. 2130706433) to it’s string equivalent (e.g., “127.0.0.1”).
-
.parse(string) ⇒ Object
Parse a string containing CIDR subnet definitions, one per line.
Class Method Details
.ip2num(ip) ⇒ Object
Utility for converting a string IP (e.g. “127.0.0.1”) to it’s 32-bit integer equivalent (e.g., 2130706433).
36 37 38 |
# 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) ⇒ Object
Load a file containing CIDR subnet definitions, one per line. The result will be an array of Subnet objects.
9 10 11 |
# File 'lib/netspace.rb', line 9 def self.load(file) NetSpace::parse(File.read(file)) end |
.num2ip(num) ⇒ Object
Utility for converting a 32-bit integer IP (e.g. 2130706433) to it’s string equivalent (e.g., “127.0.0.1”).
45 46 47 |
# File 'lib/netspace.rb', line 45 def self.num2ip(num) return [num].pack("N").unpack("C*").join(".") end |
.parse(string) ⇒ Object
Parse a string containing CIDR subnet definitions, one per line. The result will be an array of Subnet objects.
18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |