Class: Inspec::Resources::HpuxPorts

Inherits:
FreeBsdPorts show all
Defined in:
lib/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



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/resources/port.rb', line 735

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



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

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