Well again its been a while since I posted, and once again I have been sidelined from CISCO studies.
A few weeks back I was asked to audit 1500 ports for a client, so thre was no way I was doing that by hand, I am a manger after all you know ;). But it was a great excuse to great a bit of scripting experience in scripting / programming.
Secure CRT has great scripting support and after a few hours playing around I had a script that can read from an excel spread sheet and carry out a list of checks and record the results.
1500 ports spread over 260 devices = 15 min, compared to the 20hr + I worked out it could have taken by hand including preparing the report.
But this lead me a bit further, what would be nice is a program that you could input any amount of code, where varibles where highlighted like.
conf t
int ***port***
description ***des***
speed ****speed****
along with a spread sheet that contained columns containing the variables, and the program could loop through the rows implementing each line as it goes.
In my job there is the potential of having to implement the same config on multiply devices with different values (ie assign ports to vlans)
So for the last few weeks, VBA, VB script and C# have been taking up my life, its been great fun and while maybe not my CCNP still good skills to have as a network engineer to be able to automate things and increase throughput.
hope to be back soon with more updates…
On and other news…. Lilith was one at the weekend, happy birthday my beautiful little girl, you are and angel and very much loved by me and your mum 🙂 XX
DevilWAH
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
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
testing
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.
Sorry 🙂 One should be int g0/23 and the other g0/24
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.
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
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