Class: Inspec::Resources::Host
- Inherits:
-
Object
- Object
- Inspec::Resources::Host
- Defined in:
- lib/inspec/resources/host.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(hostname, params = {}) ⇒ Host
constructor
A new instance of Host.
-
#ipaddress ⇒ Object
returns all A records of the IP address, will return an array.
-
#ipv4_address ⇒ Object
returns an array of the ipv4 addresses.
-
#ipv6_address ⇒ Object
returns an array of the ipv6 addresses.
- #proto ⇒ Object
- #reachable? ⇒ Boolean
-
#resolvable?(type = nil) ⇒ Boolean
if we get the IP address, the host is resolvable.
- #resource_id ⇒ Object
- #socket ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hostname, params = {}) ⇒ Host
Returns a new instance of Host.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/inspec/resources/host.rb', line 47 def initialize(hostname, params = {}) @hostname = hostname @port = params[:port] if params[:proto] Inspec.deprecate(:host_resource_proto_usage, "The `host` resource `proto` resource parameter is deprecated. Please use `protocol`.") @protocol = params[:proto] else @protocol = params.fetch(:protocol, "icmp") end @host_provider = nil if inspec.os.linux? @host_provider = LinuxHostProvider.new(inspec) elsif inspec.os.windows? return skip_resource "Invalid protocol: only `tcp` and `icmp` protocols are support for the `host` resource on your OS." unless %w{icmp tcp}.include?(@protocol) @host_provider = WindowsHostProvider.new(inspec) elsif inspec.os.darwin? @host_provider = DarwinHostProvider.new(inspec) else return skip_resource "The `host` resource is not supported on your OS yet." end missing_requirements = @host_provider.missing_requirements(protocol) unless missing_requirements.empty? skip_resource "The following requirements are not met for this resource: " \ "#{missing_requirements.join(", ")}" end end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
45 46 47 |
# File 'lib/inspec/resources/host.rb', line 45 def hostname @hostname end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
45 46 47 |
# File 'lib/inspec/resources/host.rb', line 45 def port @port end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
45 46 47 |
# File 'lib/inspec/resources/host.rb', line 45 def protocol @protocol end |
Instance Method Details
#connection ⇒ Object
103 104 105 |
# File 'lib/inspec/resources/host.rb', line 103 def connection ping[:connection] end |
#ipaddress ⇒ Object
returns all A records of the IP address, will return an array
112 113 114 |
# File 'lib/inspec/resources/host.rb', line 112 def ipaddress resolve.nil? || resolve.empty? ? nil : resolve end |
#ipv4_address ⇒ Object
returns an array of the ipv4 addresses
117 118 119 |
# File 'lib/inspec/resources/host.rb', line 117 def ipv4_address ipaddress.select { |ip| ip.match(Resolv::IPv4::Regex) } end |
#ipv6_address ⇒ Object
returns an array of the ipv6 addresses
122 123 124 |
# File 'lib/inspec/resources/host.rb', line 122 def ipv6_address ipaddress.select { |ip| ip.match(Resolv::IPv6::Regex) } end |
#proto ⇒ Object
79 80 81 82 |
# File 'lib/inspec/resources/host.rb', line 79 def proto Inspec.deprecate(:host_resource_proto_usage, "The host resource `proto` method is deprecated. Please use `protocol`.") protocol end |
#reachable? ⇒ Boolean
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/inspec/resources/host.rb', line 90 def reachable? # ping checks do not require port or protocol return ping.fetch(:success, false) if protocol == "icmp" # if either port or protocol are specified but not both, we cannot proceed. if port.nil? || protocol.nil? raise "Protocol required with port. Use `host` resource with host('#{hostname}', port: 1234, proto: 'tcp') parameters." if port.nil? || protocol.nil? end # perform the protocol-specific reachability test ping.fetch(:success, false) end |
#resolvable?(type = nil) ⇒ Boolean
if we get the IP address, the host is resolvable
85 86 87 88 |
# File 'lib/inspec/resources/host.rb', line 85 def resolvable?(type = nil) warn "The `host` resource ignores #{type} parameters. Continue to resolve host." unless type.nil? resolve.nil? || resolve.empty? ? false : true end |
#resource_id ⇒ Object
133 134 135 |
# File 'lib/inspec/resources/host.rb', line 133 def resource_id port ? "#{hostname}-#{port}-#{protocol}" : hostname end |
#socket ⇒ Object
107 108 109 |
# File 'lib/inspec/resources/host.rb', line 107 def socket ping[:socket] end |
#to_s ⇒ Object
126 127 128 129 130 131 |
# File 'lib/inspec/resources/host.rb', line 126 def to_s resource_name = "Host #{hostname}" resource_name += " port #{port} proto #{protocol}" if port resource_name end |