class Inspec::Resources::Port

Public Class Methods

new(*args) click to toggle source
# File lib/inspec/resources/port.rb, line 25
def initialize(*args)
  args.unshift(nil) if args.length <= 1 # add the ip address to the front
  @ip = args[0]
  @port = if args[1].nil?
            nil
          else
            args[1].to_i
          end

  @cache = nil
  @port_manager = port_manager_for_os
  return skip_resource "The `port` resource is not supported on your OS yet." if @port_manager.nil?
end

Public Instance Methods

to_s() click to toggle source
# File lib/inspec/resources/port.rb, line 48
def to_s
  "Port #{@port}"
end

Private Instance Methods

info() click to toggle source
# File lib/inspec/resources/port.rb, line 78
def info
  return @cache unless @cache.nil?
  # abort if os detection has not worked
  return @cache = [] if @port_manager.nil?

  # query ports
  cache = @port_manager.info || []
  cache.select! { |x| x["port"] == @port } unless @port.nil?
  cache.select! { |x| x["address"] == @ip } unless @ip.nil?
  @cache = cache
end
port_manager_for_os() click to toggle source
# File lib/inspec/resources/port.rb, line 54
def port_manager_for_os
  os = inspec.os
  if os.linux?
    LinuxPorts.new(inspec, @port)
  elsif os.aix?
    # AIX: see http://www.ibm.com/developerworks/aix/library/au-lsof.html#resources
    #      and https://www-01.ibm.com/marketing/iwm/iwm/web/reg/pick.do?source=aixbp
    AixPorts.new(inspec)
  elsif os.darwin?
    # Darwin: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/lsof.8.html
    # Careful: make sure darwin comes before BSD, below
    LsofPorts.new(inspec)
  elsif os.windows?
    WindowsPorts.new(inspec)
  elsif os.bsd?
    # Relies on sockstat, usually present on FreeBSD and NetBSD (but not MacOS X)
    FreeBsdPorts.new(inspec)
  elsif os.solaris?
    SolarisPorts.new(inspec)
  elsif os.hpux?
    HpuxPorts.new(inspec)
  end
end