This little script helps to locate a (cisco) switch which has multiple connections on one port.
The result is a txtfile with the name, ip and port of the switch.
Software used:
- cut.exe
- sort.exe
- uniqe.exe
- tst10.exe aka Telnet Scripting Tool
The first 3 files can found in the CoreUtils archive.
The Telnet Scripting Tool can be found on Jerry Mannel’s blog
Preparations:
- Create a folder and place the files
- Create a file, eg switches.txt
- Save the source code, which can be found at the bottom of this article, as something.vbs
- Switches.txt should be in the following format:
ip-address;serietype;fullname (in most cases serietype=2950)
10.10.10.1;2950;switch-01
10.10.10.2;2950;switch-02
To run: open a commandprompt and type “wscript something.vbs”
The Code:
We begin by setting up our environment.
set wshshell = createobject("wscript.shell")
set fso= createobject("scripting.filesystemobject")
set switchlist = fso.opentextfile("switches.txt")
if fso.fileExists("result.txt") then
fso.deleteFile "result.txt", true
end if
set result = fso.openTextFile("result.txt",8,true)We loop switches.txt until all items are processed and make sure that previous created files are removed.
do until switchlist.atendofstream
'--- Remove all previous files ---
if fso.fileExists("output.txt") then
fso.deleteFile "output.txt", true
end if
if fso.fileExists("temp.txt") then
fso.deleteFile "temp.txt", true
end if
if fso.fileExists("double.txt") then
fso.deleteFile "double.txt", true
end if
switch = switchList.readLine
matrix = split(switch, ";")
ip = matrix(0)
typeswitch = matrix(1)
switchname = matrix(2)
if fso.filEexists("script.txt") then
fso.deleteFile "script.txt", true
end ifSince not every IOS uses the same commands, we split our code up.
if typeswitch = "2950" then
set script = fso.openTextFile("script.txt",8,true)
script.writeline ip & " 23"
script.writeline "send " & """<passwd>\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """term len 0\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """show mac-address-table | include Fa0/\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """quit\m"""
script.close
else 'type=2948
set script = fso.openTextFile("script.txt",8,true)
script.writeline ip & " 23"
script.writeline "send " & """<passwd>\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """set len 0\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """show cam dyn | exclude 2/49\m"""
script.writeline "wait " & """>"""
script.writeline "send " & """quit\m"""
script.close
end ifThe code above is necessary for using the Telnet Scripting Tool. More information about its use can be found on this blog.
commandoTelnet = "TST10 /r:script.txt /o:output.txt"
wshshell.run commandoTelnet, ,True
wscript.sleep 3000
if typeswitch = "2950" then
commandoGrep = "cmd /c " & """grep -e " & """DYNAMIC""" & " -e " & """STATIC""" & " output.txt > temp.txt"""
wshshell.run commandoGrep, , True
commandoCut = "cmd /c " & """cut -c 39- temp.txt > test.txt"""
wshshell.run commandoCut, , True
else '=2948
commandoGrep = "cmd /c " & """grep -e " & """[ALL]""" & " output.txt > temp.txt"""
wshshell.run commandoGrep, , True
commandoCut = "cmd /c " & """cut -c 39- temp.txt > test.txt"""
wshshell.run commandoCut, , True
end if
commandoFindDuplicates = "cmd /c" & """sort test.txt |uniq -d > double.txt"""
doubles = wshshell.run(commandoFindDuplicates, ,True)
if fso.fileExists("double.txt") then
set readDoubleTxt = fso.openTextFile("double.txt")
do until readDoubleTxt.atendofstream
routerOpPort = readDoubleTxt.readline
result.writeline switchname & "(" & ip & ")" & "->" & routerOpPort
loop
readDoubleTxt.close
end if
loop
result.close
Wscript.Echo "Script finished !"The resulting output is:
switch-02(10.10.10.1)->Fa0/45
switch-02(10.10.10.2)->Fa0/46
switch-03(10.10.10.3)->Fa0/2
switch-06(10.10.10.4)->Fa0/33
switch-07(10.10.10.5)->Fa0/41
switch-08(10.10.10.6)->Fa0/24
Although this script can be further optimized, it does the job for me
The fullcode :









Follow me