Class: Inspec::Resources::HpuxPorts

Inherits:
FreeBsdPorts show all
Defined in:
lib/inspec/resources/port.rb

Overview

extracts information from netstat for hpux

Instance Attribute Summary

Attributes inherited from PortsInfo

#inspec

Instance Method Summary collapse

Methods inherited from FreeBsdPorts

#parse_net_address, #parse_sockstat_line

Methods inherited from PortsInfo

#initialize

Constructor Details

This class inherits a constructor from Inspec::Resources::PortsInfo

Instance Method Details

#infoObject



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/inspec/resources/port.rb', line 752

def info
  ## Can't use 'netstat -an -f inet -f inet6' as the latter -f option overrides the former one and return only inet ports
  cmd1 = inspec.command("netstat -an -f inet")
  return nil if cmd1.exit_status.to_i != 0

  cmd2 = inspec.command("netstat -an -f inet6")
  return nil if cmd2.exit_status.to_i != 0

  cmd = cmd1.stdout + cmd2.stdout
  ports = []
  # parse all lines
  cmd.each_line do |line|
    port_info = parse_netstat_line(line)
    next unless %w{tcp tcp6 udp udp6}.include?(port_info["protocol"])

    ports.push(port_info)
  end
  # select all ports, where we `listen`
  ports.select { |val| val if "listen".casecmp(val["state"]) == 0 }
end

#parse_netstat_line(line) ⇒ Object



773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/inspec/resources/port.rb', line 773

def parse_netstat_line(line)
  # parse each line
  # 1 - Proto, 2 - Recv-Q, 3 - Send-Q, 4 - Local Address, 5 - Foreign Address, 6 - (state)
  parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)?/.match(line)

  return {} if parsed.nil? || line.match(/^proto/i) || line.match(/^active/i)

  protocol = parsed[1].downcase
  state = parsed[6].nil? ? " " : parsed[6].downcase
  local_addr = parsed[4]
  local_addr[local_addr.rindex(".")] = ":"
  # extract host and port information
  host, port = parse_net_address(local_addr, protocol)
  return {} if host.nil?

  # map data
  {
    "port" => port,
    "address" => host,
    "protocol" => protocol,
    "state" => state,
  }
end