Class: IPAddr
- Includes:
- Enumerable
- Defined in:
- lib/ronin/extensions/ip_addr.rb
Constant Summary collapse
- MASKS =
Socket families and IP address masks
{ Socket::AF_INET => IN4MASK, Socket::AF_INET6 => IN6MASK }
Class Method Summary collapse
-
.each(cidr_or_glob) {|ip| ... } ⇒ nil
Iterates over each IP address within the IP Address range.
-
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
Instance Method Summary collapse
-
#each {|ip| ... } ⇒ Object
Iterates over each IP address that is included in the addresses netmask.
-
#lookup(nameserver = nil) ⇒ Array<String>
Resolves the host-names for the IP address.
Methods included from Enumerable
Class Method Details
.each(cidr_or_glob) {|ip| ... } ⇒ nil
Iterates over each IP address within the IP Address range. Supports both IPv4 and IPv6 address ranges.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ronin/extensions/ip_addr.rb', line 115 def IPAddr.each(cidr_or_glob,&block) unless (cidr_or_glob.include?('*') || cidr_or_glob.include?(',') || cidr_or_glob.include?('-')) return IPAddr.new(cidr_or_glob).each(&block) end return enum_for(__method__,cidr_or_glob) unless block if cidr_or_glob.include?('::') separator = '::' base = 16 prefix = if cidr_or_glob.start_with?('::') then '::' else '' end format = lambda { |address| prefix + address.map { |i| '%.2x' % i }.join('::') } else separator = '.' base = 10 format = lambda { |address| address.join('.') } end # split the address segments = cidr_or_glob.split(separator) ranges = [] # map the components of the address to numeric ranges segments.each do |segment| next if segment.empty? ranges << if segment == '*' (1..254) else segment.split(',').map { |octet| if octet.include?('-') start, stop = octet.split('-',2) (start.to_i(base)..stop.to_i(base)).to_a else octet.to_i(base) end }.flatten end end # cycle through the address ranges ranges.comprehension { |address| yield format[address] } return nil end |
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ronin/extensions/ip_addr.rb', line 65 def IPAddr.extract(text,version=nil,&block) return enum_for(__method__,text,version).to_a unless block_given? regexp = case version when :ipv4, :v4, 4 then Regexp::IPv4 when :ipv6, :v6, 6 then Regexp::IPv6 else Regexp::IP end text.scan(regexp) do |match| yield match end return nil end |
Instance Method Details
#each {|ip| ... } ⇒ Object
Iterates over each IP address that is included in the addresses netmask. Supports both IPv4 and IPv6 addresses.
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ronin/extensions/ip_addr.rb', line 204 def each return enum_for(__method__) unless block_given? family_mask = MASKS[@family] (0..((~@mask_addr) & family_mask)).each do |i| yield _to_string(@addr | i) end return self end |