Class: Ronin::Support::Network::IP
- Defined in:
- lib/ronin/support/network/ip.rb,
lib/ronin/support/network/ip/mixin.rb
Overview
Represents a single IP address.
Examples
ip = IP.new('192.30.255.113')
Reverse DNS lookup:
ip.get_name
# => "lb-192-30-255-113-sea.github.com"
ip.get_names
# => ["lb-192-30-255-113-sea.github.com"]
ip.get_host
# => #<Ronin::Support::Network::Host: lb-192-30-255-113-sea.github.com>
ip.get_hosts
# => [#<Ronin::Support::Network::Host: lb-192-30-255-113-sea.github.com>]
ip.host
# => #<Ronin::Support::Network::Host: lb-192-30-255-113-sea.github.com>
ip.hosts
# => [#<Ronin::Support::Network::Host: lb-192-30-255-113-sea.github.com>]
Get ASN information:
ip.asn
# => #<Ronin::Support::Network::ASN::DNSRecord:0x00007f34142de598
# @country_code="US",
# @name=nil,
# @number=15133,
# @range=#<Ronin::Support::Network::IPRange::CIDR: 93.184.216.0/24>>
Defined Under Namespace
Modules: Mixin
Constant Summary collapse
- IPINFO_URI =
The URI for https://ipinfo.io/ip
URI::HTTPS.build(host: 'ipinfo.io', path: '/ip')
Constants inherited from IPAddr
Class Method Summary collapse
-
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
-
.local_address ⇒ String
Determines the local IP address.
-
.local_addresses ⇒ Array<String>
Determines all local IP addresses.
-
.local_ip ⇒ IP
Determines the local IP.
-
.local_ips ⇒ Array<IP>
Determines all local IPs.
-
.public_address ⇒ String?
Determines the current public IP address.
-
.public_ip ⇒ IP?
Determines the current public IP.
Instance Method Summary collapse
-
#address ⇒ String
The IP address.
-
#asn ⇒ ASN::Record
The Autonomous System Number (ASN) information for the IP address.
-
#broadcast? ⇒ Boolean
Determines if the address is a IPv4 broadcast addresses.
-
#get_host(**kwargs) ⇒ Host?
Looks up the host for the IP.
-
#get_hosts(**kwargs) ⇒ Array<Host>
Looks up all hosts associated with the IP.
-
#get_name(**kwargs) ⇒ String?
(also: #reverse_lookup)
Looks up the hostname of the address.
-
#get_names(**kwargs) ⇒ Array<String>
Looks up all hostnames associated with the IP.
-
#get_ptr_name(**kwargs) ⇒ String?
Queries the
PTR
host name for the IP address. -
#get_ptr_names(**kwargs) ⇒ Array<String>
Queries all
PTR
names for the IP address. -
#get_ptr_record(**kwargs) ⇒ Resolv::DNS::Resource::PTR?
Queries the first
PTR
DNS record for the IP address. -
#get_ptr_records(**kwargs) ⇒ Array<Resolv::DNS::Resource::PTR>
Queries all
PTR
DNS records for the IP address. -
#host ⇒ Host?
The primary host name of the address.
-
#hosts ⇒ Array<Host>
The host names of the address.
-
#initialize(address, family = Socket::AF_UNSPEC) ⇒ IP
constructor
Initializes the IP address.
-
#inspect ⇒ String
Inspects the IP.
-
#logical? ⇒ Boolean
Determines if the address is a "logical" IPv4 address.
-
#set(addr, *family) ⇒ self
protected
private
Sets the IP address using the numeric IP address value.
Methods inherited from IPAddr
Methods included from Enumerable
Constructor Details
#initialize(address, family = Socket::AF_UNSPEC) ⇒ IP
If the IP address has an %iface
suffix, it will be removed from
the IP address.
Initializes the IP address.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ronin/support/network/ip.rb', line 89 def initialize(address,family=Socket::AF_UNSPEC) case address when String # XXX: remove the %iface suffix for ruby < 3.1.0 if address =~ /%.+$/ address = address.sub(/%.+$/,'') end # pre-cache the given IP address String @address = address end begin super(address,family) rescue IPAddr::InvalidAddressError raise(InvalidIP,"invalid IP address: #{address.inspect}") end end |
Class Method Details
.extract(text, version = nil) {|ip| ... } ⇒ Array<String>
Extracts IP Addresses from text.
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/ronin/support/network/ip.rb', line 256 def self.extract(text,version=nil,&block) return enum_for(__method__,text,version).to_a unless block_given? regexp = case version when :ipv4, :v4, 4 then Text::Patterns::IPV4_ADDR when :ipv6, :v6, 6 then Text::Patterns::IPV6_ADDR else Text::Patterns::IP_ADDR end text.scan(regexp,&block) return nil end |
.local_address ⇒ String
Determines the local IP address.
212 213 214 |
# File 'lib/ronin/support/network/ip.rb', line 212 def self.local_address Socket.ip_address_list.first.ip_address end |
.local_addresses ⇒ Array<String>
Determines all local IP addresses.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ronin/support/network/ip.rb', line 175 def self.local_addresses Socket.ip_address_list.map do |addrinfo| address = addrinfo.ip_address if address =~ /%.+$/ address = address.sub(/%.+$/,'') end address end end |
.local_ip ⇒ IP
Determines the local IP.
225 226 227 |
# File 'lib/ronin/support/network/ip.rb', line 225 def self.local_ip new(local_address) end |
.local_ips ⇒ Array<IP>
Determines all local IPs.
199 200 201 |
# File 'lib/ronin/support/network/ip.rb', line 199 def self.local_ips local_addresses.map(&method(:new)) end |
.public_address ⇒ String?
Determines the current public IP address.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ronin/support/network/ip.rb', line 142 def self.public_address response = begin Net::HTTP.get_response(IPINFO_URI) rescue # ignore any network failures end if response && response.code == '200' return response.body end end |
.public_ip ⇒ IP?
Determines the current public IP.
160 161 162 163 164 |
# File 'lib/ronin/support/network/ip.rb', line 160 def self.public_ip if (address = public_address) new(address) end end |
Instance Method Details
#address ⇒ String
The IP address.
316 317 318 |
# File 'lib/ronin/support/network/ip.rb', line 316 def address @address ||= to_s end |
#asn ⇒ ASN::Record
The Autonomous System Number (ASN) information for the IP address.
334 335 336 |
# File 'lib/ronin/support/network/ip.rb', line 334 def asn @asn ||= ASN.query(self) end |
#broadcast? ⇒ Boolean
Determines if the address is a IPv4 broadcast addresses.
285 286 287 288 |
# File 'lib/ronin/support/network/ip.rb', line 285 def broadcast? # NOTE: IPv6 does not have broadcast addresses ipv4? && (@addr & 0xff) == 0xff end |
#get_host(**kwargs) ⇒ Host?
Looks up the host for the IP.
408 409 410 411 412 |
# File 'lib/ronin/support/network/ip.rb', line 408 def get_host(**kwargs) if (name = get_name(**kwargs)) Host.new(name) end end |
#get_hosts(**kwargs) ⇒ Array<Host>
Looks up all hosts associated with the IP.
434 435 436 |
# File 'lib/ronin/support/network/ip.rb', line 434 def get_hosts(**kwargs) get_names(**kwargs).map { |name| Host.new(name) } end |
#get_name(**kwargs) ⇒ String? Also known as: reverse_lookup
Looks up the hostname of the address.
358 359 360 |
# File 'lib/ronin/support/network/ip.rb', line 358 def get_name(**kwargs) DNS.get_name(@address,**kwargs) end |
#get_names(**kwargs) ⇒ Array<String>
Looks up all hostnames associated with the IP.
384 385 386 |
# File 'lib/ronin/support/network/ip.rb', line 384 def get_names(**kwargs) DNS.get_names(@address,**kwargs) end |
#get_ptr_name(**kwargs) ⇒ String?
Queries the PTR
host name for the IP address.
508 509 510 |
# File 'lib/ronin/support/network/ip.rb', line 508 def get_ptr_name(**kwargs) DNS.get_ptr_name(@address,**kwargs) end |
#get_ptr_names(**kwargs) ⇒ Array<String>
Queries all PTR
names for the IP address.
548 549 550 |
# File 'lib/ronin/support/network/ip.rb', line 548 def get_ptr_names(**kwargs) DNS.get_ptr_names(@address,**kwargs) end |
#get_ptr_record(**kwargs) ⇒ Resolv::DNS::Resource::PTR?
Queries the first PTR
DNS record for the IP address.
489 490 491 |
# File 'lib/ronin/support/network/ip.rb', line 489 def get_ptr_record(**kwargs) DNS.get_ptr_record(@address,**kwargs) end |
#get_ptr_records(**kwargs) ⇒ Array<Resolv::DNS::Resource::PTR>
Queries all PTR
DNS records for the IP address.
529 530 531 |
# File 'lib/ronin/support/network/ip.rb', line 529 def get_ptr_records(**kwargs) DNS.get_ptr_records(@address,**kwargs) end |
#host ⇒ Host?
The primary host name of the address.
467 468 469 |
# File 'lib/ronin/support/network/ip.rb', line 467 def host hosts.first end |
#hosts ⇒ Array<Host>
This method returns memoized data.
The host names of the address.
452 453 454 |
# File 'lib/ronin/support/network/ip.rb', line 452 def hosts @hosts ||= get_hosts end |
#inspect ⇒ String
Inspects the IP.
562 563 564 |
# File 'lib/ronin/support/network/ip.rb', line 562 def inspect "#<#{self.class}: #{address}>" end |
#logical? ⇒ Boolean
Determines if the address is a "logical" IPv4 address.
306 307 308 |
# File 'lib/ronin/support/network/ip.rb', line 306 def logical? ipv4? && (@addr & 0xff) == 0x00 end |
#set(addr, *family) ⇒ self (protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the IP address using the numeric IP address value.
123 124 125 126 127 128 129 |
# File 'lib/ronin/support/network/ip.rb', line 123 def set(addr,*family) super(addr,*family) # unset the cached IP address since the numeric address has changed @address = nil return self end |