class Inspec::Resources::Host
def connection
def connection ping[:connection] end
def initialize(hostname, params = {})
def initialize(hostname, params = {}) @hostname = hostname @port = params[:port] if params[:proto] warn '[DEPRECATION] The `proto` parameter is deprecated. Use `protocol` instead.' @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) return skip_resource 'The following requirements are not met for this resource: ' \ "#{missing_requirements.join(', ')}" unless missing_requirements.empty? end
def ipaddress
def ipaddress resolve.nil? || resolve.empty? ? nil : resolve end
def ping
def ping return @ping_cache if defined?(@ping_cache) return {} if @host_provider.nil? @ping_cache = @host_provider.ping(hostname, port, protocol) end
def proto
def proto warn '[DEPRECATION] The `proto` method is deprecated. Use `protocol` instead.' protocol end
def reachable?
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
def resolvable?(type = nil)
def resolvable?(type = nil) warn "The `host` resource ignores #{type} parameters. Continue to resolve host." if !type.nil? resolve.nil? || resolve.empty? ? false : true end
def resolve
def resolve return @ip_cache if defined?(@ip_cache) @ip_cache = @host_provider.resolve(hostname) if !@host_provider.nil? end
def socket
def socket ping[:socket] end
def to_s
def to_s resource_name = "Host #{hostname}" resource_name += " port #{port} proto #{protocol}" if port resource_name end