vbscript读取Cisco交换机接口
尝试创建一个将发送“sh run |”的脚本b 接口' 到 Cisco 交换机。将输出写入数组。使用 vbcr 拆分该数组,以便配置的每一行都位于数组的 sep 元素中。
我尝试过很多方法给猫剥皮,但仍然很挣扎。
英语逻辑: 向 Cisco 设备发送命令 将输出捕获到数组 定义预期行'这是交换机每个“接口”下所需的行 匹配“接口”名称和相应的编号并将其写入文件。 检查该界面下的预期特定行 如果找到它,请写下该行 & “, 是的” 如果没有找到,请写入行 & “, 不” 继续执行此操作,直到找不到更多 '^interface\s[FG][az].+'
输出应如下所示: 接口千兆以太网0/2 spanning-tree portfast,是
这是失败的示例代码:
'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
Trying to create a script that will send a 'sh run | b interface' to a Cisco switch. Write the output to an array. Split that array with a vbcr so each line of the config is in a sep elemant of the array.
I have tried to skin the cat many ways and still I am struggling.
Logic in English:
Send command to Cisco device
Capture the output to an array
define expected lines 'This are lines that are required under each 'interface' of the switch
Match the 'interface' name and corresponding number and write it to a file.
Check under that interface for the specific lines in the expected
If it finds it, write the line & ", YES"
If it does not find it, write the line & ", NO"
Keep doing this until you do not find any more '^interface\s[FG][a-z].+'
Output should look like this:
Interface GigabitEthernet 0/2
spanning-tree portfast, YES
This is the sample code that is failing:
'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
This is a sample of the vConfigLines output:
There could be 48+ port per switch.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当面临困难且复杂的任务时,只需遵循以下规则:
要处理的数据:
那么让我们开始:
您必须处理一个文本文件(行)。因此,不要
在 .ReadAll 上使用 Split 来执行超过 90% 的代码,这只是多余的。是的,它是
Do Until tsIn.AtEndOfStream
,而不是Do While tsIn.AtEndOfStream = False
。否设置 tsIn = Nothing
,请。
数据按块组织(项目 n ...!),因此请确保
识别零件并知道找到它们后该怎么做:
输出:
对于每个项目,输出应该是:
最简单的方法是
加入节拍串联,特别是如果您想设置/操作
部分独立和/或在开始时预先设置其中一些。
输出
显示,通过设置合理的默认值(NO),此版本的脚本
正确处理没有任何有趣属性的项目。
因此,让我们解决中间/Case Else 部分:
现在输出:
但是令人讨厌的是:
如果一个属性名称是前缀,则简单的 Instr() 将失败
其他。证明先从简单开始,然后再增加复杂性
是个好策略:
输出:
When facing a difficult and complex task, just follow these rules:
Data to play with:
So let's start:
You have to deal with a text file (lines). So don't do more than
90 % of the code using Split on .ReadAll is just fat. Yes, it's
Do Until tsIn.AtEndOfStream
and notDo While tsIn.AtEndOfStream = False
. NoSet tsIn = Nothing
,please.
The data is organized in blocks (Item n ... !), so make sure you
recognize the parts and know what to do when finding them:
output:
For each item the output should be:
The easiest way to do that is
Joining beats concatenation, especially if you want to set/manipulate the
parts independently and/or to pre-set some of them in the beginning.
The output
shows that by setting sensible defaults (NO), this version of the script
deals correctly with items having none of the interesting properties.
So lets tackle the middle/Case Else part:
output now:
But what about Nasty:
The simple Instr() will fail, if one property name is a prefix of
another. To prove that starting simple and add complexity later
is good strategy:
output: