Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-26-2017, 06:50 PM
ngrison ngrison is offline
Registered User
 
Join Date: Aug 2017
Posts: 22
ReadString() limitations

Hi,

I have written a few python scripts to automatically collect data from various devices. The script relies on ReadString() to capture the output of a command.

This works fine for commands returning a few hundred lines. However, for commands returning a few thousand lines or more the function breaks. It slows down and eventually the script crashes, probably because of a buffer filling up.

I have modified my script to read the output line by line however this has a major impact on performance. This is maybe 100 times slower and so for long outputs is just no useable. Unfortunately the data being random it is not possible to process it in larger chunks, LF is pretty much the only regularly recurring data.

I was wondering if there is a way to increase the buffer for ReadString() to deal with more data, or if anyone else has an alternative suggestion that wouldn't impact performance as much. The code I have come up with is shown below. Thanks!


Normal command

Code:
		cmdresult += SCRIPT_TAB.Screen.ReadString(prompt,120)
Line by line alternative:

Code:
		while 1:
			cmdresult += SCRIPT_TAB.Screen.ReadString(['\r\n', prompt],20)
			index = SCRIPT_TAB.Screen.MatchIndex
			
			if (index == 0):
   				crt.Dialog.MessageBox("Timed out!")
   				return
			elif (index == 1):
				cmdresult += '\r\n'
			elif (index == 2):
				cmdresult += 'prompt'
   				break
Reply With Quote
  #2  
Old 08-28-2017, 10:54 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi ngrison,

I'm sorry you're having trouble.

I'm hoping you can give me a little more context. How are you using cmdresult? Are you manipulating this data or are you simply capturing and logging the data you receive? Would it be an option to log the data to a file instead of capturing it as a variable?

If you do need to manipulate the data as a variable there may still be performance increases by logging the data to a file and reading back into a variable from the file.

I've created a bug report and asked that our QA/DEV team review the issues you're seeing with the ReadString method. Should a version of SecureCRT that address this issue become available I'll be sure to let you know by responding to this thread.
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 08-28-2017, 03:21 PM
ngrison ngrison is offline
Registered User
 
Join Date: Aug 2017
Posts: 22
Hi,

Thanks for your response. I do parse the output data with a regex search/replace but outputting to file is an option. I have rewritten my function quickly and it is indeed a workaround, thanks a lot for the suggestion.

Unfortunately I've hit another problem. All my sessions are setup with a default log file string:

Code:
/Users/ngrison/Documents/SecureCRT Logs/%Y-%M-%D/%S(%H)--%h-%m.log
To log the output to another file I need to temporarily change it. I have tried:
Code:
# Save the current setup
currLog = SCRIPT_TAB.Session.Logging
currLogFile = SCRIPT_TAB.Session.LogFileName

SCRIPT_TAB.Session.Log(False)
SCRIPT_TAB.Session.LogFileName = logNewFileName
SCRIPT_TAB.Session.Log(True, True)

<process itself>

# Restore the previous setup
SCRIPT_TAB.Session.Log(False)
SCRIPT_TAB.Session.LogFileName = currLogFile
But unfortunately it restore the already processed log file string, not the original one with the substitutions.

I have also tried to specify the string manually with this:

Code:
LOG_FILE = os.path.join(os.path.expanduser('~'), "Documents", "SecureCRT Logs", '%Y-%M-%D', '%S(%H)--%h-%m.log')
SCRIPT_TAB.Session.LogFileName = LOG_FILE
But I have the same problem. I also tried SCRIPT_TAB.Session.LogUsingSessionOptions() but that doesn't seem to work either.

Do you know if there is any way to reset the log file string to what it was originally?
Reply With Quote
  #4  
Old 08-28-2017, 05:03 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi ngrison,

When using log filenames that contain variables it gets a little more complex. You have to capture both the logging template containing your variables and the true filename that is being logged to. When ready to restore the original log you must restore both the actual filename and the templated version containing the variables.

The below code should help demonstrate this.

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

logNewFileName = "c:/temp/CommandResults.log"

Set SCRIPT_TAB = crt.GetScriptTab()
SCRIPT_TAB.Screen.Synchronous = True

Set objConfig = SCRIPT_TAB.Session.Config

' Store the original (template substitutions may be present)
' log file name\path into a variable (so we can restore logging)
currLogFile = objConfig.GetOption("Log Filename V2")

' Get a reference to the *Actual* log file that is being written
' to right now:
strActualLogFile = SCRIPT_TAB.Session.LogFileName

' Turn off logging (to the template-resolved location)
SCRIPT_TAB.Session.Log False

' Establish new log file for use with subsequent commands
SCRIPT_TAB.Session.LogFileName = logNewFileName
' Turn on logging
SCRIPT_TAB.Session.Log True

' Issue subsequent commands
<process itself>
' Wait for the output of said commands to finish being received:
SCRIPT_TAB.Screen.WaitForString prompt

' Turn off logging
SCRIPT_TAB.Session.Log False

' Restore *actual* log file that we were writing to before we
' interrupted things earlier:
SCRIPT_TAB.Session.LogFileName = strActualLogFile
' Begin logging again (forcing an append so that prior log contents
' do not get wiped out)
SCRIPT_TAB.Session.Log True, True

' Restore the original template-based 
objConfig.SetOption "Log Filename V2", currLogFile
Does this help?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 08-29-2017, 04:26 AM
ngrison ngrison is offline
Registered User
 
Join Date: Aug 2017
Posts: 22
Thanks a lot Eric. I did convert your code to python but was still having some problems.

After looking into it, I found out that while "SCRIPT_TAB.Session.LogFileName" seems to impact both the current session and the shortcut that launched it, "SCRIPT_TAB.Session.SetOption("Log Filename V2", xxx)" would only impact the setup of the current established session, not the original shortcut. Hence by the time the script is finished the shortcut would still be configured with the incorrect string.

Based on this I found out how to manually save and restore the config for the original shortcut, as well as for the current session and it now seems to run ok. Here is the code I am now using:

Code:
	# Save shortcut logging setup
	#

	configPath = SCRIPT_TAB.Session.Path
	shortcutConfig = crt.OpenSessionConfiguration(configPath)
	shortcutStrLogFile = shortcutConfig.GetOption("Log Filename V2")

	# Save session logging setup and configure logging to new file
	#
	
	sessionConfig = SCRIPT_TAB.Session.Config
	
	savedSessionLoggingStatus = SCRIPT_TAB.Session.Logging
	savedSessionLogFile = SCRIPT_TAB.Session.LogFileName
	savedSessionStrLogFile = sessionConfig.GetOption("Log Filename V2")
	
	SCRIPT_TAB.Session.Log(False)
	SCRIPT_TAB.Session.LogFileName = logFileName
	SCRIPT_TAB.Session.Log(True, True)

	### Core of the script runs ###

	# Restore session logging setup
	#
	
	SCRIPT_TAB.Session.Log(False)
	SCRIPT_TAB.Session.LogFileName = savedSessionLogFile
	SCRIPT_TAB.Session.Log(savedSessionLoggingStatus, True)

	sessionConfig.SetOption("Log Filename V2", savedSessionStrLogFile)
	sessionConfig.Save()
	
	
	# Restore shortcut logging setup
	#
	
	shortcutStrLogFile = shortcutConfig.SetOption("Log Filename V2",shortcutStrLogFile)
	shortcutConfig.GetOption("Start Log Upon Connect", shortcutStrLoggingStatus)
	shortcutConfig.Save()
Thank you very much for your help, I'll let you know if I find any more problems with this.
Reply With Quote
  #6  
Old 08-29-2017, 09:54 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi ngrison,

I'm glad you found a solution. Please don't hesitate to reach out if you have any other questions.

Have a great rest of the day.
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 01:23 PM.