Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-15-2013, 12:54 PM
gunslingor gunslingor is offline
Registered User
 
Join Date: May 2013
Posts: 27
SecureCRT problem with instr special characters

My code is below but the problem can probably be explained quicker:

Using instr while searching for special characters seems not to work, I've tried the following variations:
instrrev(1, szResult, Cstr(chr(34)))
instrrev(1, szResult, chr(34))
instrrev(1, szResult, """)
instrrev(1, szResult, '"')
instrrev(1, szResult, \")
and many others, nothing works. Even other special characters seem to fail... the other one I've tried is the "/" symbol. Always fails with the same error:

Microsoft VBScript runtime error
Error: Type mismatch: '[string:"""]'

Is there a 'special' SecureCRT escape character or something?

Code:
Language="VBScript"
Interface="1.0"

Sub Main()

	Dim objTab
	Dim szStartString, szEndingString, szResult
	Dim sversion, simage_file
	crt.Screen.Synchronous = True
	szStartString = "#"
	szEndingString = "#"
	
	Set objTab = crt.GetScriptTab
	
	If Not objTab.session.connected then
        MsgBox "Not Connected.  Please connect before running this script."
        exit sub
    end if
	
	crt.Screen.Send "show version | include Cisco IOS Software|flash:/"
	crt.Screen.Send chr(13)
	szResult = objTab.Screen.ReadString(szEndingString)
	
	Dim n_string_start, n_string_end
	n_string_start = instr(1,szResult, "Version")+8
	n_string_end = instr(n_string_start, szResult, ", ")
	sverion = mid(szResult, n_string_start, n_string_end - n_string_start)
	
	n_string_start = instrrev(1, szResult, Cstr(chr(34)))+1
	msgbox n_string_start
	'n_string_end = instrrev(1, szResult, )
	simage_file = mid(szResult, n_string_start, n_string_end - n_string_start)	
	
	msgbox simage_file
	
	'crt.Session.Disconnect
End sub
Reply With Quote
  #2  
Old 05-15-2013, 01:48 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi gunslingor,

The question you are asking is really a VBScript question. I would suggest taking a look at Microsoft's documentation on VBScript. Here is the MSDN location as of the date of this post (Microsoft could change the location in the future):
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
To answer your question, the issue is likely one of incorrect VBScript syntax. Your use of InStrRev() appears to be searching the number one (1) for a string with the begin point of a quote ("). Here is the syntax from MSDN:
InStrRev(string1, string2[, start[, compare]])
My guess is that you need to modify the order to be the following:
InStrRev(szResult, chr(34), 1)
This code will only search the first character of the string, so I am not sure what your goal is.

Perhaps if you can post an example of the data you want to parse, and what you want to capture, we can point you in a better direction.

Additionally, if you want to perform complex parsing of data, you may want to turn to regular expressions.

Chapter 4.3 of the scripting manual covers various strategies for parsing data.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 05-15-2013 at 01:53 PM.
Reply With Quote
  #3  
Old 05-16-2013, 05:48 AM
gunslingor gunslingor is offline
Registered User
 
Join Date: May 2013
Posts: 27
Quote:
Originally Posted by rtb View Post
Hi gunslingor,
The question you are asking is really a VBScript question. I would suggest taking a look at Microsoft's documentation on VBScript. Here is the MSDN location as of the date of this post (Microsoft could change the location in the future):
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
To answer your question, the issue is likely one of incorrect VBScript syntax. Your use of InStrRev() appears to be searching the number one (1) for a string with the begin point of a quote ("). Here is the syntax from MSDN:
InStrRev(string1, string2[, start[, compare]])
My guess is that you need to modify the order to be the following:
InStrRev(szResult, chr(34), 1)
This code will only search the first character of the string, so I am not sure what your goal is.

Perhaps if you can post an example of the data you want to parse, and what you want to capture, we can point you in a better direction.

Additionally, if you want to perform complex parsing of data, you may want to turn to regular expressions.

Chapter 4.3 of the scripting manual covers various strategies for parsing data.
Your right, it was a VBscript question... I feel like an idiot, 2 hours wasted because I assumed instr and instrrev had the same arguments in the same order. I'm pretty sure I've made this mistake before as well, lol, oh boy. Thanks.

The code you suggested, i believe, would actually search the entire string since the "1" just defines the starting position starting from the end... according to documentation, the end position is actually -1 and this is the default, so I actually just took it out completely.

Regarding the use of regular expressions, yeah I think I will end up using it for IP/MAC addresses and potentially others, but interfaces names, OS, and other field data might be to variable for this. Thanks for the chapter reference, I'll read it... the manual is quite good I must say.

I noticed this before on VB reference sites, and you referenced it again in your last post. "InStrRev(string1, string2[, start[, compare]])".... Waits with all the odd brackets, bad HTML interpretation (e.g. http://www.w3schools.com/vbscript/func_instr.asp)?

For thread completeness sake, here's the wrap up-This code runs a one command, pulls out the OS version and the image file name from the output, and displays the results in a textbox. Once I get all these tidbits of data assigned to variables, I'll output them to a text file.

Code:
Language="VBScript"
Interface="1.0"

Sub Main()

	Dim objTab
	Dim szStartString, szEndingString, szResult
	Dim sversion, simage_file
	crt.Screen.Synchronous = True
	szStartString = "#"
	szEndingString = "#"
	
	Set objTab = crt.GetScriptTab
	
	If Not objTab.session.connected then
        MsgBox "Not Connected.  Please connect before running this script."
        exit sub
    end if
	
	crt.Screen.Send "show version | include Cisco IOS Software|flash:/"
	crt.Screen.Send chr(13)
	szResult = objTab.Screen.ReadString(szEndingString)
	
	'pull out the OS version
	Dim n_string_start, n_string_end
	n_string_start = instr(1,szResult, "Version")+8
	n_string_end = instr(n_string_start, szResult, ", ")
	sverion = mid(szResult, n_string_start, n_string_end - n_string_start)
	
	'pull out the image file name
	n_string_start = instrrev(szResult, chr(47))+1
	n_string_end = instrrev(szResult, chr(34))
	simage_file = mid(szResult, n_string_start, n_string_end - n_string_start)	
	
	'test display for results
	msgbox (sverion & chr(13) & simage_file)
	
	'crt.Session.Disconnect
End sub
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:49 PM.