Method: Inspec::Resources::LsofPorts#info

Defined in:
lib/resources/port.rb

#infoObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/resources/port.rb', line 153

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