module Shells::PfSenseCommon

Common functionality for interacting with a pfSense device.

Constants

BASE_SHELL

The base shell used when possible.

The prompt text for the main menu.

Attributes

pf_sense_host[RW]

Gets the hostname of the pfSense device.

pf_sense_user[RW]

Gets the user currently logged into the pfSense device.

pf_sense_version[RW]

Gets the version of the pfSense firmware.

Public Instance Methods

line_ending() click to toggle source
# File lib/shells/pf_sense_common.rb, line 113
def line_ending
  @line_ending ||= "\n"
end
pf_shell(&block) click to toggle source

Executes the code block in the pfSense PHP shell.

# File lib/shells/pf_sense_common.rb, line 161
def pf_shell(&block)
  ::Shells::PfShellWrapper.new(self, &block).output
end

Private Instance Methods

get_menu_option(option_text, delay = true) click to toggle source

Processes the pfSense console menu to determine the option to send.

# File lib/shells/pf_sense_common.rb, line 168
def get_menu_option(option_text, delay = true)
  option_regex = /\s(\d+)\)\s*#{option_text}\s/i

  temporary_prompt MENU_PROMPT do
    # give the prompt a few seconds to draw.
    if delay
      wait_for_prompt(nil, 4, false)
    end

    # See if we have a menu already.
    menu_regex = /(?<MENU>\s0\)(?:.|\r|\n(?!\s0\)))*)#{MENU_PROMPT}[ \t]*$/
    match = menu_regex.match(output)
    menu = match ? match['MENU'] : nil

    discard_local_buffer do
      if menu.nil?
        # We want to redraw the menu.
        # In order to do that, we need to send a command that is not valid.
        # A blank line equates to a zero, which is (probably) the logout option.
        # So we'll send a -1 to redraw the menu without actually running any commands.
        debug 'Redrawing menu...'
        menu = exec('-1', command_timeout: 5, timeout_error: false)

        if last_exit_code == :timeout
          # If for some reason the shell is/was running, we need to exit it to return to the menu.
          # This time we will raise an error.
          menu = exec('exit', command_timeout: 5)
        end
      end

      # Ok, so now we have our menu options.
      debug "Locating 'XX) #{option_text}' menu option..."
      match = option_regex.match(menu)
      if match
        return match[1].to_i
      else
        return nil
      end
    end
  end
end