Method: Inspec::Resources::LsofPorts#info

Defined in:
lib/resources/port.rb

#infoObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/resources/port.rb', line 168

def info
  ports = []

  # check that lsof is available, otherwise fail
  fail 'Please ensure `lsof` is available on the machine.' if !inspec.command(@lsof.to_s).exist?

  # -F p=pid, c=command, P=protocol name, t=type, n=internet addresses
  # see 'OUTPUT FOR OTHER PROGRAMS' in LSOF(8)
  lsof_cmd = inspec.command("#{@lsof} -nP -i -FpctPn")
  return nil if lsof_cmd.exit_status.to_i != 0

  # map to desired return struct
  lsof_parser(lsof_cmd).each do |process, port_ids|
    pid, cmd = process.split(':')
    port_ids.each do |port_str|
      # should not break on ipv6 addresses
      ipv, proto, port, host = port_str.split(':', 4)
      ports.push({ 'port'     => port.to_i,
                   'address'  => host,
                   'protocol' => ipv == 'ipv6' ? proto + '6' : proto,
                   'process'  => cmd,
                   'pid'      => pid.to_i })
    end
  end

  ports
end