class ForemanProbingCore::Actions::UseProbe

Public Instance Methods

finish_run(update) click to toggle source
Calls superclass method
# File lib/foreman_probing_core/actions/use_probe.rb, line 28
def finish_run(update)
  super
  output[:facts] = process_output(output[:result])
  output.delete(:result)
end
initiate_runner() click to toggle source
# File lib/foreman_probing_core/actions/use_probe.rb, line 18
def initiate_runner
  if input.fetch('options', {})['subnet_discovery']
    input['local_addresses'] = get_local_addrs
    input['targets'] = input['local_addresses'].keys
  end
  output[:targets] = input[:targets]
  output[:local_addresses] = input[:local_addresses] if input.key? :local_addresses
  CommandRunner.new(*probe.command)
end

Private Instance Methods

get_local_addrs() click to toggle source
# File lib/foreman_probing_core/actions/use_probe.rb, line 65
def get_local_addrs
  locals = Socket.getifaddrs.select { |ifaddr| ifaddr.addr && ifaddr.addr.ipv4_private? }
  locals.reduce({}) do |acc, ifaddr|
    # TODO: This is ugly
    cidr = 32 - ifaddr.netmask.ip_address.to_i.to_s(2).count('1')

    acc.merge("#{ifaddr.addr.ip_address}/#{cidr}" => { :addr => ifaddr.addr.ip_address,
                                                       :netmask => ifaddr.netmask.ip_address,
                                                       :cidr => cidr })
  end
end
probe() click to toggle source
# File lib/foreman_probing_core/actions/use_probe.rb, line 46
def probe
  @probe ||= probe_class.new(input[:targets],
                             input[:ports],
                             input[:options])
end
probe_class() click to toggle source
# File lib/foreman_probing_core/actions/use_probe.rb, line 52
def probe_class
  case input[:scan_type].downcase
  when 'tcp'
    ForemanProbingCore::Probes::TCP
  when 'udp'
    ForemanProbingCore::Probes::UDP
  when 'icmp'
    ForemanProbingCore::Probes::ICMP
  else
    raise "Unknown scan_type '#{input[:scan_type]}'"
  end
end
process_output(output) click to toggle source
# File lib/foreman_probing_core/actions/use_probe.rb, line 36
def process_output(output)
  stdout, stderr = output.partition { |out| out[:output_type] == 'stdout' }
  if stderr.any?
    raise stderr.map { |out| out[:output] }.join('')
  else
    output = stdout.map { |out| out[:output] }.join('')
    probe.parse_result(output)
  end
end