cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
730
Views
0
Helpful
3
Replies

Help using Vbscript to capture serial numbers and name/create files

Siren
Level 1
Level 1

Need help to create simple Vbscript to capture the serial number from Cisco machines and use the serial number to create and name a file on the local PC. The machines are local so no need to authenticate and of course I am using SecureCRT.

I found a nifty Cisco command show snmp chassis that writes the serial number alone on the next line. I thought I could just suck it up into a variable with the Vbs command strResult = crt.Screen.ReadString (Router#).  I was able to trim it with the Vbs Trim commands. The problem is that the variable doesn't work when I try and create a file or logfile on the local windows machine and gives me the error that the path variable or file name is incorrect. When I replace the serial number variable with a static variable number like 12345678 it work fine and creates the file when I switch back no good.

I tried to use the variable with the hostname command to rename the router and the same problem appears the variable created 12345678 within Vbs works fine but the serial number wont work. I have used MsgBox to display the serial number and it is in there. I am working a old IOS router. I created a bunch of variables to experiment with hoping the variable was the problem and I could clean it up.

If any can suggest an entirely new method of get the serial numbers into a working variable I am open to that. I am just hoping to find a solution that works on my Cisco IOS machines.

# $language = "VBScript"
# $interface = "1.0"

Sub Main

crt.Screen.Synchronous = True
Dim strResult, strResult1, strResult2, strResult3, strResult4


strResult5 = 1234567
crt.Screen.Send "conf t" & vbCr
crt.Screen.WaitForString("#")
crt.Screen.Send vbCr
crt.Screen.Send "no logging console" & vbCr
crt.Screen.WaitForString("#")
crt.Screen.Send "snmp-server community public RO" & vbCr
crt.Screen.WaitForString("#")
crt.Screen.Send chr(26)
crt.Screen.WaitForString("#")
crt.Sleep 1000
crt.Screen.WaitForString("#")
crt.Screen.Send "sh snmp chassis" & vbCr
strResult = crt.Screen.ReadString ("Router#")
strResult1 = Mid(strResult, 16)
strResult2 = Trim(strResult1)
strResult3 = RTrim(StrResult2)
strResult4 = LTrim(StrResult3)
strResult6 = ("hostname " & strResult4)
strResult7 = Trim(strResult6)

MsgBox strResult6

crt.Session.LogFileName = ("C:\Running_Capture\"& strResult2 & ".txt")

'The filename, directory name, or volume label syntax is incorrect.

'Unable to open logfile:
' xxx
'strResult4 = crt.Screen.ReadString ("TermG#")


crt.session.log True, True
crt.Screen.WaitForString "#"
crt.Screen.Send "show inv" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "!" & VbCr
crt.Screen.WaitForString "#"

crt.Screen.Synchronous = False
crt.session.log False
End Sub

3 Replies 3

pieterh
VIP
VIP

you start with this as a first variable: strResult = crt.Screen.ReadString ("Router#")
then try to dissect this line to determine the hostname
but most of the times on the device a prompt will be set equal to the hostname
so the string "Router#" may not be matched as this will be "<hostname>#" (being the hostname of the router)
and so all subsequent variables derived from this will not give the information as expected

you can confirm this with a
   strResult = crt.Screen.ReadString ("Router#")
   MsgBox strResult 

Thanks for the reply. In my case "Router#" will always be the the host name of the router since "Router#" is the default name of the machine when no configuration is present. Every machine will be factory reset via booting with confreg-register 0x2142 before this variable is created.

I have displayed the variable with MsgBox and the serial number appears fine but when I try and use the variable to name anything then throws an error message. My working theory is that the ReadString is capturing some other invisible garbage which is not displayed when MsgBox displays the serial number. When I create a variable within the script like strResult#1 = 12345678 then it works fine and creates/names the file. When I switch back to the variable created via crt.Screen.ReadString then no good. I tried a bunch to trim and Mid() the variable but still no success. 

Is there a better method to see everything contained in the variable or to wash it so only the number remain. 

 

 

I do not see the script exit configuration mode in a normal way

instead of using
   crt.Screen.Send chr(26)
   crt.Screen.WaitForString("#")
   crt.Sleep 1000
   crt.Screen.WaitForString("#")
I suggest to change that to 
   crt.Screen.Send "end" & vbCr
   crt.Screen.WaitForString("#")

I did not test the whole script, but the "show snmp chassis" command does not produce additional characters (except normal end-of-line)