8 Comments

  1. Manny

    Good day,

    I too am working on a similar script using SCRT. What I am doing is writing a script for a customer that want to compare his ‘running-configs’ with a ‘standard config’. I have been able to do this with arrays, IF/THEN, the objStream to write to a file. However it is not portable in the sense that the script itself has the lines of code I am comparing to. It would be nicer to read a file, run a ‘sho run all’ read that output and then compare. I am writing ‘if the line is found %the-line& & “,Y”. This way the customer could easily change his standard config and the script still work.

    The output is comma separated (only 2 columns)

    If you have anything like that, please share. If not, when I finally get mine cleaned up, I will send it your way.

    cheers

    Reply
    1. DevilWAH

      Hi,

      If you look at my confgen tool in the link above this is a c# application that uses config saved in text files as the master templates.

      If you want to do the same with secure crt you can use a file open dialogue to get the user to chose a template file when they run the script.

      then simple read it in to an array and use it use it as you have in your current script with the hard coded config you are currently using.

      This link below will give you some ideas how to get a file name and path from the user,

      http://www.robvanderwoude.com/vbstech_ui_fileopen.php

      you can then simple read in each line of the file in turn using objStream to an array.

      Hope that helps if you need more info I can give you some code snipits to look at.

      Regards

      DevilWAH

      Reply
  2. Manny

    Thanks for the link.

    Maybe you can help me out. I have a switch that I need to run a ‘sh run | section (^interface_[FG][a-z].+) to only list interfaces that begine with interface and are of Gigabit or Fastethernet flavor.

    The idea is to check every instance of ‘FastEthernet or GigabitEthernet’ and ensure that certain configuration has been made below each interface (e.g. ip access-group %whatever the ACL is% )

    interface FastEthernet1/0/1
    switchport
    switchport access vlan 120
    switchport mode access
    switchport voice vlan 220
    no shutdown
    snmp trap mac-notification change added
    snmp trap mac-notification change removed
    snmp trap link-status
    auto qos voip trust
    spanning-tree portfast

    I would then compare the output of each sh run to a list of expected lines (seen above) if it finds the command under the interface, write it out to a file.
    I am currently capturing the output of the ‘sh run’ into an array:

    crt.Screen.Send “sh run | section (^interface_[FG][a-z].+)” & vbcr
    crt.Screen.WaitForString vbcr
    strResultsDSL = crt.Screen.ReadString(“#”)

    ‘ Create array of expected lines to compare to the output from the Cisco device.
    Dim vExpectedDSL(3)
    vExpectedDSL(0) = ” switchport mode access”
    vExpectedDSL(1) = “switchport access vlan 120″
    vExpectedDSL(2) = ” switchport voice vlan 210″

    nCount = 0
    vConfigLines = Split(strResultsDSL, vbcr)

    For nOuterIndex = 0 To UBound(vExpectedDSL) – 1
    For nInnerIndex = 0 To Ubound(vConfigLines) – 1
    If InStr(vConfigLines(nInnerIndex), vExpectedDSL(nOuterIndex)) Then
    ‘ Write to file vExpectedDSL(nOuterIndex) to file with a YES
    ‘ MsgBox “Found: ” & vbcrlf & vbtab & vExpectedDSL(nOuterIndex)
    objStream.Write vExpectedDSL(nOuterIndex) & “,YES” &vbcrlf
    Exit For
    Else
    If Ubound(vConfigLines) – 1 = nCount Then
    ‘ Write to file vExpectedDSL(nOuterIndex) to file with a NO
    ‘ MsgBox “Not Found: ” & vbcrlf & vbtab & vExpectedDSL(nOuterIndex)
    objStream.Write vExpectedDSL(nOuterIndex) & “,NO” &vbcrlf
    nCount = 0
    Exit For
    End If
    End If
    nCount = nCount + 1
    Next
    Next

    What is your suggestion to looping through the config until it cannot find anymore F/Gethernet ports?

    The output should just be :

    Interface G0/1/23
    switchport mode access, YES ‘ if it matched it to the expected lines
    switchport access vlan 120, NO ‘ If it did not match it
    Interface G0/1/23
    switchport mode access, YES ‘ if it matched it to the expected lines
    switchport access vlan 120, NO ‘ If it did not match it

    Or something like,
    Interface G0/1/23 “missing:”
    switchport access vlan 120

    But not write anything if it is OK.

    Any help would greatly be appreciated.

    Reply
  3. Manny

    Sorry :-) One should be int g0/23 and the other g0/24

    Reply
    1. DevilWAH

      OK My logic would go some thing like this, can’t put this in to code right now.

      Expected array

      teststring(1) = “switch port access Vlan 10″
      teststring(2) = “switchport mode access”

      then I would break my out put from show run in to separate chunks

      intconfig = “” ‘ set varible to null
      if instr(line(x), “interface”)) then ‘interface marks start of code block

      do
      intconfig = intconfig & “,” & line(x)
      x=x+1 ‘ increment to next line

      while line(x) <> “interface or end of file

      arrayintconfig = split(intconfig, “,”)

      So you now have an array containing only that interface config.

      so now you can search this string, using each of the expected strings from your array

      for x = 0 to ubound(arrayintconfig)
      for y = 0 to ubound (teststring)
      if teststring(y) = arrayintconfig(x) then

      matched = matched & “,” y ‘ this will build up the matched variable with a string containing all the test strings that have been matched, that you can use to work out ‘what missing later

      else

      next ‘ this test will have told you if the output string from the device has matched any of the test strings.
      next’ this will end the loop of the arrayintconfig lines so you have now tested each line against each test line

      you will now have a matched string some thing like “1,3,4,5,7” so you would know 0,2,6 are missing and can output test(0),test(2) and test(6) as missing after you have written out the content of arrayintconfig.

      then each time you increment x you need to clear the arrayintconfig, intconfig and matched.

      Does that make any sence?

      1. break apart you output in to a temp array for each indivual interface.
      2. test each of your expected lines against each of the lines from this temp array
      3. mark any you find, in a new array/string
      4. by a process elimination you can now see what lines are missing.

      Reply
  4. Manny

    OK, I was playing with this today. I tried to create a multi-Line Regex using the “|” between them, but it appears that it is an “OR” instead of “AND”. So I have some interfaces (Tunnel XX) that I do not want to include in this check.

    Again, vConfigLines is an array from the output of a show run command on the Cisco device.

    2 Things, the Regex is matching any instance of the regex after the | and I am getting an out of range error. If you can help, that would be great.

    Regards.
    It is failing

    vConfigLines = Split(strResultsINT, vbcr)

    Set re = new RegExp
    re.Global = False
    re.IgnoreCase = True
    re.Multiline = False
    re.Pattern = “interface\s[FG].*”

    Set re2 = new RegExp
    re2.Global = False
    re2.IgnoreCase = True
    re2.Multiline = False
    re2.Pattern = “\sswitchport\saccess\svlan [0-9].+|\sspanning-tree\sportfast|\sswitchport\smode\saccess”

    Do While nCount 0 Then
    MsgBox “Regex 2″ & vConfigLines(nCount)
    else
    exit do
    end if
    nCount = nCount + 1
    loop
    Else
    nCount = nCount + 1
    end IF
    Loop

    Reply
    1. DevilWAH

      Hi,

      Sadly I am not a big user of Regex so can’t really help with that.

      I suggest you go to a site called “Stackoverflow.com”

      You will need to sign up by its a free forum for programmers and has a huge user base. There are not to many people on there using CRT, but for VBscript and any other programming language you can think of you will get an answer.

      I generally find I can get an answer of there in 30minutes or so, and I know from experience there are some good Regex experts there.

      Try there and please let me know what they say, if your interested in scripting and programming its worth having them bookmarked anyway.

      Hope that helps

      DevilWAH

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>