【问题标题】:vbscript reading Cisco switch interfacesvbscript 读取 Cisco 交换机接口
【发布时间】:2012-01-11 17:50:04
【问题描述】:

尝试创建一个将发送 'sh run | 的脚本b 到 Cisco 交换机的接口。将输出写入数组。使用 vbcr 拆分该数组,以便配置的每一行都在数组的 sep elemant 中。

我尝试了很多方法给猫剥皮,但我仍然在苦苦挣扎。

英文逻辑: 向 Cisco 设备发送命令 将输出捕获到数组 定义预期行'这是交换机的每个'接口'下所需的行 匹配“接口”名称和相应的编号并将其写入文件。 在该界面下检查预期中的特定行 如果找到它,写下 & ", YES" 如果没有找到,写下 &", NO" 继续这样做,直到找不到更多 '^interface\s[FG][a-z].+'

输出应如下所示: 接口 GigabitEthernet 0/2 生成树端口快,是的

这是失败的示例代码:

'These are the expected line (not being compared in the script below but is my intention to have it compare the matched elements)
Dim vExpectedINT(4)
vExpectedINT(0)  = "spanning-tree portfast"
vExpectedINT(1)  = "switchport access vlan 17"
vExpectedINT(2)  = "switchport mode access"
vExpectedINT(3)  = "ip mtu 1400"    


'objStream.Write "######################################################### " & vbcrlf
'objStream.Write "#                  I N T E R F A C E                    # " & vbcrlf
'objStream.Write "######################################################### " & vbcrlf


nCount = 0
vConfigLines = Split(strResultsINT, vbcr)

Set re = new RegExp
re.Global = False
re.IgnoreCase = True
re.Multiline = False
re.Pattern = "^interface [FG]"

' Regex Ex Definition
Set re2 = new RegExp
re2.Global = False
re2.IgnoreCase = True
re2.Multiline = False
re2.Pattern = "\sspanning-tree\sportfast"

' Regex Ex Definition
Set re3 = new RegExp
re3.Global = False
re3.IgnoreCase = True
re3.Multiline = False
re3.Pattern = "ip\smtu\s1400"

Set re4 = new RegExp
re4.Global = False
re4.IgnoreCase = True
re4.Multiline = False
re4.Pattern = "!"

' Compares the information
x = 1
Do While x <= Ubound(vConfigLines) - 1 do 
    MsgBox chr(34) & strLine & chr(34)
    If re.Test(vConfigLines(x)) Then
        ' Write data to not expected section
        x=x+1
        do
            If ! re4.Test(vConfigLines(x)) Then
                MsgBox vConfigLines(x)
                'objStream.Write vConfigLines(x) & vbcr
                elseif re2.Test(vConfigLines(x)) Then
                MsgBox vConfigLines(x)
                elseif re3.Test(vConfigLines(x)) Then
                MsgBox vConfigLines(x)
            else
                exit do
            end if
            x=x+1
        loop
        end IF
   End If
Loop    

这是 vConfigLines 输出的示例:

每个交换机可能有 48 个以上的端口。

interface FastEthernet1/0/1
 switchport access vlan 127
 switchport mode access
 switchport voice vlan 210
 srr-queue bandwidth share 10 10 60 20
 srr-queue bandwidth shape 0 3 0 0
 priority-queue out 
 mls qos trust cos
 auto qos voip trust 
 spanning-tree portfast
!
interface FastEthernet1/0/2
 switchport access vlan 127
 switchport mode access
 switchport voice vlan 210
 srr-queue bandwidth share 10 10 60 20
 srr-queue bandwidth shape 0 3 0 0
 priority-queue out 
 mls qos trust cos
 auto qos voip trust 
 spanning-tree portfast
!
interface FastEthernet1/0/3
 switchport access vlan 127
 switchport mode access
 switchport voice vlan 210
 srr-queue bandwidth share 10 10 60 20
 srr-queue bandwidth shape 0 3 0 0
 priority-queue out 
 mls qos trust cos
 auto qos voip trust 
 spanning-tree portfast

【问题讨论】:

    标签: regex vbscript cisco


    【解决方案1】:

    当面临艰巨复杂的任务时,请遵循以下规则:

    Divide the task in independently solvable subproblems
      getting the info from Cisco
      processing the resulting file
        gather interesting info
        output
    
    Concentrate on the difficult subtask(s)
      processing the resulting file
    
    Solve a simplified but generalized version of (each) subtask using handmade data
    for easy testing
      You have items and are interested in whether they (don't) have given properties
    

    要玩的数据:

    Item 0 (both props)
     prop_a
     prop_b
    !
    Item 1 (just b)
     prop_b
    !
    Item 2 (a only)
     prop_a
    !
    Item 3 (none)
    !
    Item 4 (irrelevant prop)
     prop_c
    !
    Item 5 (Richy)
     prop_c
     prop_b
     prop_a
    !
    Item 6 (Junky)
     junk
    
     prop_b
     whatever
    
    !
    #Item 7 (Nasty)
    # prop_a_like_but_not_prop_a
    # prop_b
    #!
    
    Keep it simple
      don't do more than absolutely necessary
      don't use variables/components you can do without
    

    让我们开始吧:

    你必须处理一个文本文件(行)。所以不要做超过

      Dim tsIn : Set tsIn = goFS.OpenTextFile("..\data\TheProblem.txt")
      Dim sLine
      Do Until tsIn.AtEndOfStream
          sLine = Trim(tsIn.ReadLine())
          If "" <> sLine Then
          End If
      Loop
      tsIn.Close
    

    在 .ReadAll 上使用 Split 的 90 % 的代码太胖了。是的,它是Do Until tsIn.AtEndOfStream 而不是Do While tsIn.AtEndOfStream = False。没有Set tsIn = Nothing, 请。

    数据以块的形式组织(第 n 项 ...!),因此请确保您 识别零件并知道在找到它们时该怎么做:

      Dim tsIn : Set tsIn = goFS.OpenTextFile("..\data\TheProblem.txt")
      Dim sItem  : sItem    = "Item"
      Dim sEnd   : sEnd     = "!"
      Dim sLine
      Do Until tsIn.AtEndOfStream
          sLine = Trim(tsIn.ReadLine())
          If "" <> sLine Then
             Select Case True
               Case 1 = Instr(sLine, sItem)
                 WScript.Echo "Begin, note item (name)"
               Case 1 = Instr(sLine, sEnd)
                 WScript.Echo "End, output info"
                 WScript.Echo "----------"
               Case Else
                 WScript.Echo "Middle, gather info"
             End Select
          End If
      Loop
      tsIn.Close
    

    输出:

    Begin, note item (name)
    Middle, gather info
    Middle, gather info
    End, output info
    ----------
    Begin, note item (name)
    Middle, gather info
    End, output info
    ----------
    ...
    

    对于每个项目,输出应该是:

    name, property, yes|no
    

    最简单的方法是

    WScript.Echo Join(aData, ", ")
    

    加入节拍串联,特别是如果您想设置/操作 部分独立和/或在开始时预先设置其中一些。

      Dim aData  : aData    = Array( _
          Array( "Item?", "prop_a", "NO") _
        , Array( "Item?", "prop_b", "NO") _
      )
      Dim sLine, aTmp, nIdx
      Do Until tsIn.AtEndOfStream
          sLine = Trim(tsIn.ReadLine())
          If "" <> sLine Then
             Select Case True
               Case 1 = Instr(sLine, sItem)
                 aTmp = aData
                 For nIdx = 0 To UBound(aTmp)
                     aTmp(nIdx)(0) = sLine
                 Next
               Case 1 = Instr(sLine, sEnd)
                 For nIdx = 0 To UBound(aTmp)
                     WScript.Echo Join(aTmp(nIdx), ", ")
                 Next
                 WScript.Echo "----------"
               Case Else
                 WScript.Echo "Middle, gather info"
             End Select
          End If
      Loop
      tsIn.Close
    

    输出

    ...
    Item 3 (none), prop_a, NO
    Item 3 (none), prop_b, NO
    ...
    

    表明,通过设置合理的默认值 (NO),此版本的脚本 正确处理没有有趣属性的项目。

    所以让我们处理中间/Case Else 部分:

       Case Else
         For nIdx = 0 To UBound(aTmp)
             If 1 = Instr(sLine, aTmp(nIdx)(1)) Then
                aTmp(nIdx)(2) = "YES"
                Exit For
             End If
         Next
    

    现在输出:

    Item 0 (both props), prop_a, YES
    Item 0 (both props), prop_b, YES
    ----------
    Item 1 (just b), prop_a, NO
    Item 1 (just b), prop_b, YES
    ----------
    Item 2 (a only), prop_a, YES
    Item 2 (a only), prop_b, NO
    ----------
    Item 3 (none), prop_a, NO
    Item 3 (none), prop_b, NO
    ----------
    Item 4 (irrelevant prop), prop_a, NO
    Item 4 (irrelevant prop), prop_b, NO
    ----------
    Item 5 (Richy), prop_a, YES
    Item 5 (Richy), prop_b, YES
    ----------
    Item 6 (Junky), prop_a, NO
    Item 6 (Junky), prop_b, YES
    ----------
    

    但是讨厌的呢:

    #Item 7 (Nasty)
    # prop_a_like_but_not_prop_a
    # prop_b
    #!
    

    简单的 Instr() 将失败,如果一个属性名称是前缀 其他。证明从简单开始,然后增加复杂性 是个好策略:

      Dim sFSpec : sFSpec   = "..\data\TheProblem.txt"
      WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll
      Dim tsIn   : Set tsIn = goFS.OpenTextFile(sFSpec)
      Dim sItem  : sItem    = "Item"
      Dim sEnd   : sEnd     = "!"
      Dim aData  : aData    = Array( _
          Array( "Item?", "prop_a", "NO") _
        , Array( "Item?", "prop_b", "NO") _
      )
      Dim aRe    : aRe      = Array(New RegExp, New RegExp)
      Dim nIdx
      For nIdx = 0 To UBound(aRe)
          aRe(nIdx).Pattern = "^" & aData(nIdx)(1) & "$"
      Next
      Dim sLine, aTmp
      Do Until tsIn.AtEndOfStream
          sLine = Trim(tsIn.ReadLine())
          If "" <> sLine Then
             Select Case True
               Case 1 = Instr(sLine, sItem)
                 aTmp = aData
                 For nIdx = 0 To UBound(aTmp)
                     aTmp(nIdx)(0) = sLine
                 Next
               Case 1 = Instr(sLine, sEnd)
                 For nIdx = 0 To UBound(aTmp)
                     WScript.Echo Join(aTmp(nIdx), ", ")
                 Next
                 WScript.Echo "----------"
               Case Else
                 For nIdx = 0 To UBound(aTmp)
                     If aRe(nIdx).Test(sLine) Then
                        aTmp(nIdx)(2) = "YES"
                        Exit For
                     End If
                 Next
             End Select
          End If
      Loop
      tsIn.Close
    

    输出:

    Item 0 (both props)
     prop_a
     prop_b
    !
    Item 1 (just b)
     prop_b
    !
    Item 2 (a only)
     prop_a
    !
    Item 3 (none)
    !
    Item 4 (irrelevant prop)
     prop_c
    !
    Item 5 (Richy)
     prop_c
     prop_b
     prop_a
    !
    Item 6 (Junky)
     junk
    
     prop_b
     whatever
    
    !
    Item 7 (Nasty)
     prop_a_like_but_not_prop_a
     prop_b
    !
    
    Item 0 (both props), prop_a, YES
    Item 0 (both props), prop_b, YES
    ----------
    Item 1 (just b), prop_a, NO
    Item 1 (just b), prop_b, YES
    ----------
    Item 2 (a only), prop_a, YES
    Item 2 (a only), prop_b, NO
    ----------
    Item 3 (none), prop_a, NO
    Item 3 (none), prop_b, NO
    ----------
    Item 4 (irrelevant prop), prop_a, NO
    Item 4 (irrelevant prop), prop_b, NO
    ----------
    Item 5 (Richy), prop_a, YES
    Item 5 (Richy), prop_b, YES
    ----------
    Item 6 (Junky), prop_a, NO
    Item 6 (Junky), prop_b, YES
    ----------
    Item 7 (Nasty), prop_a, NO
    Item 7 (Nasty), prop_b, YES
    ----------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多