Class: Ronin::Support::Network::IPRange
- Inherits:
-
Object
- Object
- Ronin::Support::Network::IPRange
- Defined in:
- lib/ronin/support/network/ip_range.rb,
lib/ronin/support/network/ip_range/cidr.rb,
lib/ronin/support/network/ip_range/glob.rb,
lib/ronin/support/network/ip_range/range.rb
Overview
Represents an IP range.
Examples
Enumerating over a CIDR range:
IPRange.each('10.0.0.1/24') { |ip| puts ip }
# 10.0.0.0
# 10.0.0.1
# 10.0.0.2
# ...
# 10.0.0.254
# 10.0.0.255
Enumerating over a IP-glob range:
IPRange.each('10.0.1-3.*') { |ip| puts ip }
# 10.0.1.0
# 10.0.1.1
# ...
# 10.0.1.254
# 10.0.1.255
# ...
# 10.0.2.0
# 10.0.2.1
# ...
# 10.0.2.254
# 10.0.2.255
# ...
# 10.0.3.0
# 10.0.3.1
# ...
# 10.0.3.254
# 10.0.3.255
Defined Under Namespace
Constant Summary collapse
- REGEX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regular expression to match CIDR ranges or IP-glob ranges.
/#{CIDR::REGEX}|#{Glob::REGEX}/
Instance Attribute Summary collapse
-
#range ⇒ CIDR, Glob
readonly
private
The parsed IP range.
Class Method Summary collapse
-
.cidr?(string) ⇒ Boolean
Determines if the IP range is a CIDR range.
-
.each(string) {|ip| ... } ⇒ Enumerator
Enumerates over each IP address that is included in the addresses netmask.
-
.glob?(string) ⇒ Boolean
Determines if the IP range is a IP-glob range.
-
.parse(string) ⇒ IPRange
Alias for new.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the IP range to another IP range.
-
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP range.
-
#each {|ip| ... } ⇒ Enumerator
Enumerates over each IP address that is included in the addresses netmask.
-
#first ⇒ String
The first IP address in the IP range.
-
#include?(ip) ⇒ Boolean
Determines whether the IP address exists within the IP range.
-
#initialize(string) ⇒ IPRange
constructor
Initializes the IP range.
-
#inspect ⇒ String
Inspects the IP range.
-
#ipv4? ⇒ Boolean
Determines if the IP range is IPv4.
-
#ipv6? ⇒ Boolean
Determines if the IP range is IPv6.
-
#last ⇒ String
The last IP address in the IP range.
-
#size ⇒ Integer
Calculates the size of the IP range.
-
#string ⇒ String
The IP range string.
-
#to_s ⇒ String
Converts the IP range back to a String.
Constructor Details
#initialize(string) ⇒ IPRange
Initializes the IP range.
99 100 101 102 103 104 105 106 |
# File 'lib/ronin/support/network/ip_range.rb', line 99 def initialize(string) @range = case string when CIDR::REGEX then CIDR.new(string) when Glob::REGEX then Glob.new(string) else raise(ArgumentError,"invalid IP range: #{string.inspect}") end end |
Instance Attribute Details
Class Method Details
.cidr?(string) ⇒ Boolean
Determines if the IP range is a CIDR range.
184 185 186 |
# File 'lib/ronin/support/network/ip_range.rb', line 184 def self.cidr?(string) string =~ CIDR::REGEX end |
.each(string) {|ip| ... } ⇒ Enumerator
Enumerates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.
171 172 173 |
# File 'lib/ronin/support/network/ip_range.rb', line 171 def self.each(string,&block) new(string).each(&block) end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the IP range to another IP range.
262 263 264 |
# File 'lib/ronin/support/network/ip_range.rb', line 262 def ==(other) other.kind_of?(self.class) && @range == other.range end |
#===(other) ⇒ Boolean
Determines if the given IP range is a sub-set of the IP range.
279 280 281 282 283 284 285 286 |
# File 'lib/ronin/support/network/ip_range.rb', line 279 def ===(other) case other when IPRange @range === other.range else @range === other end end |
#each {|ip| ... } ⇒ Enumerator
Enumerates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.
336 337 338 |
# File 'lib/ronin/support/network/ip_range.rb', line 336 def each(&block) @range.each(&block) end |
#first ⇒ String
The first IP address in the IP range.
350 351 352 |
# File 'lib/ronin/support/network/ip_range.rb', line 350 def first @range.first end |
#include?(ip) ⇒ Boolean
Determines whether the IP address exists within the IP range.
248 249 250 |
# File 'lib/ronin/support/network/ip_range.rb', line 248 def include?(ip) @range.include?(ip) end |
#inspect ⇒ String
Inspects the IP range.
399 400 401 |
# File 'lib/ronin/support/network/ip_range.rb', line 399 def inspect "#<#{self.class}: #{@range.string}>" end |
#ipv4? ⇒ Boolean
Determines if the IP range is IPv4.
221 222 223 |
# File 'lib/ronin/support/network/ip_range.rb', line 221 def ipv4? @range.ipv4? end |
#ipv6? ⇒ Boolean
Determines if the IP range is IPv6.
233 234 235 |
# File 'lib/ronin/support/network/ip_range.rb', line 233 def ipv6? @range.ipv6? end |
#last ⇒ String
The last IP address in the IP range.
364 365 366 |
# File 'lib/ronin/support/network/ip_range.rb', line 364 def last @range.last end |
#size ⇒ Integer
Calculates the size of the IP range.
378 379 380 |
# File 'lib/ronin/support/network/ip_range.rb', line 378 def size @range.size end |
#string ⇒ String
The IP range string.
209 210 211 |
# File 'lib/ronin/support/network/ip_range.rb', line 209 def string @range.string end |
#to_s ⇒ String
Converts the IP range back to a String.
390 391 392 |
# File 'lib/ronin/support/network/ip_range.rb', line 390 def to_s @range.to_s end |