Class: Inspec::Resources::AixPorts
Instance Attribute Summary
Attributes inherited from PortsInfo
#inspec
Instance Method Summary
collapse
Methods inherited from PortsInfo
#initialize
Instance Method Details
#info ⇒ Object
267
268
269
|
# File 'lib/resources/port.rb', line 267
def info
ports_via_netstat || ports_via_lsof
end
|
#parse_net_address(net_addr, protocol) ⇒ Object
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/resources/port.rb', line 329
def parse_net_address(net_addr, protocol)
address, _sep, port = net_addr.rpartition('.')
if protocol.eql?('tcp6') || protocol.eql?('udp6')
ip6addr = address
ip6addr = '::' if ip6addr =~ /^\*$/
ip6addr += ':' if ip6addr =~ /\w:$/
begin
ip_parser = IPAddr.new(ip6addr)
rescue IPAddr::InvalidAddressError
return [nil, nil]
end
if ip_parser.ipv4?
ip_addr = URI("addr://#{ip6addr}:#{port}")
host = ip_addr.host
else
ip_addr = URI("addr://[#{ip6addr}]:#{port}")
host = ip_addr.host[1..ip_addr.host.size-2]
end
else
ip4addr = address
ip4addr = '0.0.0.0' if ip4addr =~ /^\*$/
ip_addr = URI("addr://#{ip4addr}:#{port}")
host = ip_addr.host
end
[host, port.to_i]
end
|
#parse_netstat_line(line) ⇒ Object
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/resources/port.rb', line 295
def parse_netstat_line(line)
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)?\s+(\S+)/.match(line)
return {} if parsed.nil?
protocol = parsed[2].downcase
protocol += '6' if parsed[5].count(':') > 1 && %w{tcp udp}.include?(protocol)
protocol.chop! if %w{tcp4 upd4}.include?(protocol)
host, port = parse_net_address(parsed[5], protocol)
return {} if host.nil?
cmd = inspec.command("rmsock #{parsed[1]} tcpcb")
parsed_pid = /^The socket (\S+) is being held by proccess (\d+) \((\S+)\)/.match(cmd.stdout)
return {} if parsed_pid.nil?
process = parsed_pid[3]
pid = parsed_pid[2]
pid = pid.to_i if pid =~ /^\d+$/
{
'port' => port,
'address' => host,
'protocol' => protocol,
'process' => process,
'pid' => pid,
}
end
|
#ports_via_lsof ⇒ Object
271
272
273
274
|
# File 'lib/resources/port.rb', line 271
def ports_via_lsof
return nil unless inspec.command('lsof').exist?
LsofPorts.new(inspec).info
end
|
#ports_via_netstat ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/resources/port.rb', line 276
def ports_via_netstat
return nil unless inspec.command('netstat').exist?
cmd = inspec.command('netstat -Aan | grep LISTEN')
return nil unless cmd.exit_status.to_i.zero?
ports = []
cmd.stdout.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
ports
end
|