Module: Cisco

Defined in:
lib/cisco_node_utils/interface_ospf.rb,
lib/cisco_node_utils/vtp.rb,
lib/cisco_node_utils/yum.rb,
lib/cisco_node_utils/node.rb,
lib/cisco_node_utils/vlan.rb,
lib/cisco_node_utils/snmpuser.rb,
lib/cisco_node_utils/interface.rb,
lib/cisco_node_utils/snmpgroup.rb,
lib/cisco_node_utils/snmpserver.rb,
lib/cisco_node_utils/router_ospf.rb,
lib/cisco_node_utils/snmpcommunity.rb,
lib/cisco_node_utils/tacacs_server.rb,
lib/cisco_node_utils/cisco_cmn_utils.rb,
lib/cisco_node_utils/router_ospf_vrf.rb,
lib/cisco_node_utils/configparser_lib.rb,
lib/cisco_node_utils/tacacs_server_host.rb

Overview

Shared Library to compare configurations.

Copyright © 2013-2015 Cisco and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Current | Target | Configuration Case

no  command |  no  command | to Match Existing

-      -    |  -      a    |        (no match or trans match)
-      -    |  y      a    | no match nor base match
-      a    |  -      a    | match
-      a    |  y      a    |        (base match)
y      a    |  -      a    |        (base match)
y      a    |  y      a    | match
-      b    |  -      a    | trans match
-      b    |  y      a    |        (base match)
y      b    |  -      a    |        (base match)
y      b    |  y      a    | trans match)

Defined Under Namespace

Modules: ConfigParser Classes: ChefUtils, CliError, Encryption, Interface, InterfaceOspf, Node, RouterOspf, RouterOspfVrf, SnmpCommunity, SnmpGroup, SnmpServer, SnmpUser, TacacsServer, TacacsServerHost, Vlan, Vtp, Yum

Constant Summary collapse

VLAN_NAME_SIZE =
33
SNMP_USER_NAME_KEY =
"user"
SNMP_USER_GROUP_KEY =
"group"
SNMP_USER_AUTH_KEY =
"auth"
SNMP_USER_PRIV_KEY =
"priv"
SNMP_USER_ENGINE_ID =
"engineID"
SNMP_USER_ENGINE_ID_PATTERN =
/([0-9]{1,3}(:[0-9]{1,3}){4,31})/
IF_SWITCHPORT_MODE =
{
  :disabled   => "",
  :access     => "access",
  :trunk      => "trunk",
  :fex_fabric => "fex-fabric",
  :tunnel     => "dot1q-tunnel",
}
TACACS_SERVER_ENC_NONE =
0
TACACS_SERVER_ENC_CISCO_TYPE_7 =
7
TACACS_SERVER_ENC_UNKNOWN =
8
DEFAULT_INSTANCE_NAME =

global constants

'default'

Class Method Summary collapse

Class Method Details

.find_ascii(body, regex_query, *parent_cfg) ⇒ [String]?

Method for working with hierarchical show command output such as “show running-config”. Searches the given multi-line string for all matches to the given regex_query. If parents is provided, the matches will be filtered to only those that are located “under” the given parent sequence (as determined by indentation).

Examples:

Find all OSPF router names in the running-config

ospf_names = find_ascii(running_cfg, /^router ospf (\d+)/)

Find all address-family types under the given BGP router

bgp_afs = find_ascii(show_run_bgp, /^address-family (.*)/,
                     /^router bgp #{ASN}/)

Parameters:

  • body (String)

    The body of text to search

  • regex_query (Regex)

    The regular expression to match

  • parents (*Regex)

    zero or more regular expressions defining the parent configs to filter by.

Returns:

  • ([String], nil)

    array of matching (sub)strings, else nil.



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/cisco_node_utils/node.rb', line 625

def find_ascii(body, regex_query, *parent_cfg)
  return nil if body.nil? or regex_query.nil?

  # get subconfig
  parent_cfg.each { |p| body = find_subconfig(body, p) }
  if body.nil?
    return nil
  else
    # find matches and return as array of String if it only does one
    # match in the regex. Otherwise return array of array
    match = body.split("\n").map { |s| s.scan(regex_query) }
    match = match.flatten(1)
    return nil if match.empty?
    match = match.flatten if match[0].is_a?(Array) and match[0].length == 1
    return match
  end
end

.find_one_ascii(body, regex_query, *parent_cfg) ⇒ String

Convenience wrapper for find_ascii. Operates under the assumption that there will be zero or one matches for the given query and returns the match string (or “”) rather than an array.

Examples:

Get the domain name if any

domain_name = find_one_ascii(running_cfg, "ip domain-name (.*)")
=> 'example.com'

Parameters:

  • body (String)

    The body of text to search

  • regex_query (Regex)

    The regular expression to match

  • parents (*Regex)

    zero or more regular expressions defining the parent configs to filter by.

Returns:

  • (String)

    the matching (sub)string or “” if no match.

Raises:

  • (RuntimeError)

    if more than one match is found.



599
600
601
602
603
604
# File 'lib/cisco_node_utils/node.rb', line 599

def find_one_ascii(body, regex_query, *parent_cfg)
  matches = find_ascii(body, regex_query, *parent_cfg)
  return "" if matches.nil?
  raise RuntimeError if matches.length > 1
  matches[0]
end

.find_subconfig(body, regex_query) ⇒ String?

Returns the subsection associated with the given line of config to retrieve the subsection appropriately, or nil if no such subsection exists.

Parameters:

  • the (String)

    body of text to search

  • the (Regex)

    regex key of the config for which

Returns:

  • (String, nil)

    the subsection of body, de-indented



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/cisco_node_utils/node.rb', line 651

def find_subconfig(body, regex_query)
  return nil if body.nil? or regex_query.nil?

  rows = body.split("\n")
  match_row_index = rows.index { |row| regex_query =~ row }
  return nil if match_row_index.nil?

  cur = match_row_index+1
  subconfig = []

  until (/\A\s+.*/ =~ rows[cur]).nil? or cur == rows.length
    subconfig << rows[cur]
    cur += 1
  end
  return nil if subconfig.empty?
  # Strip an appropriate minimal amount of leading whitespace from
  # all lines in the subconfig
  min_leading = subconfig.map { |line| line[/\A */].size }.min
  subconfig = subconfig.map { |line| line[min_leading..-1] }
  subconfig.join("\n")
end