Module: Assemblr::Network

Defined in:
lib/assemblr/network.rb

Overview

Defines methods for common network functions.

Class Method Summary collapse

Class Method Details

.included(_) ⇒ Object



14
15
16
17
# File 'lib/assemblr/network.rb', line 14

def self.included(_)
  expose_method :local_ip
  expose_method :ip_reachable?
end

.ip_reachable?(ip) ⇒ Boolean

Check if a remote ip can be contacted.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/assemblr/network.rb', line 40

def ip_reachable?(ip)
  external = Net::Ping::External.new(ip)

  log_info %(attempting to contact host "#{ip}")
  reachable = external.ping || external.ping6
  if reachable
    log_success %(host "#{ip}" is reachable)
  else
    log_error %(unable to contact host "#{ip}")
  end

  reachable
end

.local_ip(pattern = //) ⇒ String

Retrieve the first ip address that is not ‘127.0.0.1’ on the local machine.

Parameters:

  • pattern (Regexp) (defaults to: //)

    a pattern to test the IPs against

Returns:

  • (String)


27
28
29
30
31
32
33
34
# File 'lib/assemblr/network.rb', line 27

def local_ip(pattern = //)
  Socket.ip_address_list.each do |ip|
    address = ip.ip_address
    next if address == '127.0.0.1'
    return address if address.match?(pattern)
  end
  ''
end