Class: Specinfra::Command::Darwin::Base::Host

Inherits:
Base::Host show all
Defined in:
lib/specinfra/command/darwin/base/host.rb

Class Method Summary collapse

Methods inherited from Base

create, escape

Class Method Details

.check_is_reachable(host, port, proto, timeout) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/specinfra/command/darwin/base/host.rb', line 15

def check_is_reachable(host, port, proto, timeout)
  if port.nil?
    "ping -t #{escape(timeout)} -c 2 -n #{escape(host)}"
  else
    "nc -vvvvz#{escape(proto[0].chr)} #{escape(host)} #{escape(port)} -w #{escape(timeout)} -G #{escape(timeout)}"
  end
end

.check_is_resolvable(name, type) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/specinfra/command/darwin/base/host.rb', line 3

def check_is_resolvable(name, type)
  if type == "dns"
    ## try to resolve either A or AAAA record; grep is used to return the appropriate exit code
    %Q{dig +search +short +time=1 -q #{escape(name)} a #{escape(name)} aaaa | grep -qie '^[0-9a-f:.]*$'}
  elsif type == "hosts"
    "sed 's/#.*$//' /etc/hosts | grep -w -- #{escape(name)}"
  else
    ## grep is required as dscacheutil always returns exit code 0
    "dscacheutil -q host -a name #{escape(name)} | grep -q '_address:'"
  end
end

.get_ipaddress(name) ⇒ Object



23
24
25
26
27
28
# File 'lib/specinfra/command/darwin/base/host.rb', line 23

def get_ipaddress(name)
  # If the query returns multiple records the most likey match is returned.
  # Generally this means IPv6 wins over IPv4.
  %Q{dscacheutil -q host -a name #{escape(name)} | } + 
  %Q{awk '/^ipv6_/{ ip = $2 }; /^$/{ exit }; /^ip_/{ ip = $2; exit}; END{ print ip }'}
end

.get_ipv4_address(name) ⇒ Object



29
30
31
32
# File 'lib/specinfra/command/darwin/base/host.rb', line 29

def get_ipv4_address(name)
  ## With dscacheutil multiple IPs can be returned for IPv4 just pick the first one
  %Q{dscacheutil -q host -a name #{escape(name)} | awk '/^ip_/{ print $2; exit }'}
end

.get_ipv6_address(name) ⇒ Object



33
34
35
36
37
# File 'lib/specinfra/command/darwin/base/host.rb', line 33

def get_ipv6_address(name)
  ## With dscacheutil multiple IPs can be returned. For IPv6 the link-local is displayed first
  ## hence the last entry is picked.
  %Q{dscacheutil -q host -a name #{escape(name)} | awk '/^ipv6_/{ ip = $2 } END{ print ip }'}
end