Class: ASAConsole::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/asa_console/config.rb

Overview

A parser for ASA running/startup config.

Each instance represents a single config entry which may include nested lines of config that can be queried with #select or #select_all.

Examples:

Print ACEs with line numbers for each access list

# Start by fetching a top-level Config object
config = asa.running_config('access-list')
config.names_of('access-list').each do |acl|
  puts "Access list #{acl}:"
  counter = 0
  config.select('access-list', acl) do |ace|
    counter += 1
    puts " Line #{counter}: #{ace.line}"
  end
  puts
end

List all interfaces that participate in EIGRP

# Start by fetching a top-level Config object
config = asa.running_config('all router eigrp')
router = config.select('router eigrp')
router.select('passive-interface') do |passive_interface|
  ifname = passive_interface.config_data
  next if ifname == 'default'
  if passive_interface.negated?
    puts "Interface '#{ifname}' is sending routing updates"
  else
    puts "Interface '#{ifname}' is passive"
  end
end if router

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :keystr (String)

    class attribute

  • :config_name (String)

    class attribute

  • :config_data (String)

    class attribute

  • :negated (Boolean)

    true if the config line began with "no", or false otherwise

  • :nested_config (String)

    a multiline string of config (indentation stripped)

See Also:



60
61
62
63
64
65
66
# File 'lib/asa_console/config.rb', line 60

def initialize(opts = {})
  @keystr         = opts[:keystr]
  @config_name    = opts[:config_name]
  @config_data    = opts[:config_data]
  @negated        = opts.fetch(:negated, false)
  @nested_config  = opts.fetch(:nested_config, '')
end

Instance Attribute Details

#config_dataString? (readonly)

Remainder of the config line following the #keystr and #config_name.

Returns:

  • (String, nil)

    the current value of config_data



47
48
49
# File 'lib/asa_console/config.rb', line 47

def config_data
  @config_data
end

#config_nameString? (readonly)

An identifier, such as an access list name, used to distinguish between config entries of the same type.

Returns:

  • (String, nil)

    the current value of config_name



47
48
49
# File 'lib/asa_console/config.rb', line 47

def config_name
  @config_name
end

#keystrString? (readonly)

Key used to select the line of config represented by this object, or nil if the object contains nested top-level config.

Returns:

  • (String, nil)

    the current value of keystr



47
48
49
# File 'lib/asa_console/config.rb', line 47

def keystr
  @keystr
end

Instance Method Details

#empty?Boolean

Returns true if there is no nested config, or false otherwise.

Returns:

  • (Boolean)

    true if there is no nested config, or false otherwise



87
88
89
# File 'lib/asa_console/config.rb', line 87

def empty?
  @nested_config.empty?
end

#lineString?

Returns the selected line, or nil if this is a top-level object.

Returns:

  • (String, nil)

    the selected line, or nil if this is a top-level object



70
71
72
73
74
75
76
77
# File 'lib/asa_console/config.rb', line 70

def line
  parts = []
  parts << 'no' if @negated
  parts << @keystr if @keystr
  parts << @config_name if @config_name
  parts << @config_data if @config_data
  parts.empty? ? nil : parts.join(' ')
end

#names_of(keystr) ⇒ Array<String>

Returns a unique list of config element names matched by keystr.

Parameters:

  • keystr (String)

Returns:

  • (Array<String>)

    a unique list of config element names matched by keystr

See Also:



159
160
161
162
163
164
165
166
167
168
# File 'lib/asa_console/config.rb', line 159

def names_of(keystr)
  names = []
  regex = /^(?:no )?#{Regexp.escape(keystr)} (?<name>\S+)/
  @nested_config.each_line do |line|
    m = regex.match(line)
    next unless m
    names << m[:name] unless names.include? m[:name]
  end
  names
end

#negated?Boolean

Returns true if the selected line began with "no", or false otherwise.

Returns:

  • (Boolean)

    true if the selected line began with "no", or false otherwise



81
82
83
# File 'lib/asa_console/config.rb', line 81

def negated?
  @negated ? true : false
end

#select(keystr = nil, config_name = nil) {|config| ... } ⇒ Config

Select the first matching line of the nested config or yield all matching lines if a block is given.

Parameters:

  • keystr (String, nil) (defaults to: nil)
  • config_name (String, nil) (defaults to: nil)

Yield Parameters:

Yield Returns:

  • (nil)

    if a block is given

Returns:

  • (Config)

    if no block given



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/asa_console/config.rb', line 117

def select(keystr = nil, config_name = nil)
  prefix = [keystr, config_name].join(' ').strip
  regex = /^(?<no>no )?#{Regexp.escape(prefix)} ?(?<data>.+)?/

  io = StringIO.open(@nested_config)
  lines = io.readlines
  io.close

  loop do
    break if lines.empty?

    m = regex.match(lines.shift)
    next unless m

    nested_config = ''
    loop do
      break unless lines[0] && lines[0].start_with?(' ')
      nested_config << lines.shift.sub(/^ /, '')
    end

    config = Config.new(
      keystr:         keystr,
      config_name:    config_name,
      config_data:    m[:data],
      negated:        !m[:no].nil?,
      nested_config:  nested_config
    )

    if block_given?
      yield config
    else
      return config
    end
  end

  nil
end

#select_all {|config| ... } ⇒ Array<Config>

Select all lines of nested config. Equivalent to #select with no arguments.

Yield Parameters:

Yield Returns:

  • (nil)

    if a block is given

Returns:

  • (Array<Config>)

    if no block given



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/asa_console/config.rb', line 97

def select_all
  result = []
  select do |config|
    if block_given?
      yield config
    else
      result << config
    end
  end
  result unless block_given?
end