Welcome to the VanDyke Software Forums

Join the discussion today!

Go Back   VanDyke Software Forums > Scripting

Thread Tools Rate Thread Display Modes
Old 01-19-2012, 03:07 PM
JM3100 JM3100 is offline
Registered User
Join Date: Jan 2012
Posts: 4
Combining data from two consecutive screens

Hey all, Kind of new to scripting, and after reading some of these posts, determined you guys are the best source for help. I am trying to combine data from multiple consecutive screens into a csv file using the get2() command. I am needing to capture (x)rows of data, Hit the Enter key and capture the same rows on the following screen then output both sets of data stacked over each other to the CSV file. I have one page working, and I am getting nowhere trying to figure out the next page. Any help would be greatly appreciated. Here is my code below.

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

' SaveSelectedTextToCSVFile.vbs
' Description:
' If non-whitespace text is selected within the terminal screen, the user
' will be prompted for a location + filename in which to store the selected
' text as a CSV file. Multiple space characters within the data will be
' converted to a single comma character.
' Demonstrates:
' - How to use the Screen.Selection property new to SecureCRT 6.1 to access
' text selected within the terminal window.
' - How to use the Scripting.FileSystemObject to write data to a
' text file.
' - How to use RegExp's Replace() method to convert sequential space
' characters into a single comma character.
' - One way of determining if the script is running on Windows XP or not.
' - One way of displaying a file browse/open dialog in Windows XP
' g_nMode is used only if the user specifies a file that already exists, in
' which case the user will be prompted to overwrite the existing file, append
' to the existing file, or cancel the operation.

Dim g_nMode
Const ForWriting = 2
Const ForAppending = 8

' Be "tab safe" by getting a reference to the tab for which this script
' has been launched:

Set objTab = crt.GetScriptTab
Set g_shell = CreateObject("WScript.Shell")
Set g_fso = CreateObject("Scripting.FileSystemObject")


Sub SaveSelectedTextToCSVFile()

' Capture the selected text into a variable. The 'Selection' property
' is available in SecureCRT 6.1 and later. This line of code will cause
' an error if launched in a version of SecureCRT earlier than 6.1.

'++strSelection = objTab.Screen.Selection

readline = crt.Screen.Get2(4,1, 21,crt.Screen.Columns)
'items = Split( readline, ":", -1 )
'readline = Split(screenrow, vbcrlf)
'For nIndex = 1 To UBound(strSelection)
'MsgBox "Line #" & nIndex & ": " & strSelection(nIndex - 1)

' Check to see if the selection really has any text to save... we don't
' usually want to write out nothing to a file.

'++If Trim(strSelection) = "" Then
'++crt.Dialog.MessageBox "Nothing to save!"
'++Exit Sub
'++End If
strFilename = g_shell.SpecialFolders("MyDocuments") & _
"\" & crt.Session.RemoteAddress & "-SelectedText.csv"
strFilename = BrowseForFile(strFilename)
If strFilename = "" Then Exit Sub

' Do some sanity checking if the file specified by the user already
' exists...

If g_fso.FileExists(strFilename) Then
nResult = crt.Dialog.MessageBox( _
"Do you want to replace the contents of """ & _
strFilename & _
""" with the selected text?" & vbcrlf & vbcrlf & _
vbtab & "Yes = overwrite" & vbcrlf & vbcrlf & _
vbtab & "No = append" & vbcrlf & vbcrlf & _
vbtab & "Cancel = end script" & vbcrlf, _
"Replace Existing File?", _
Select Case (nResult)
Case vbYes
g_nMode = ForWriting
Exit Do
Case vbNo
g_nMode = ForAppending
Exit Do
Case Else
Exit Sub
End Select
g_nMode = ForWriting
Exit Do
End If

' Automatically append a .csv if the filename supplied doesn't include
' any extension.

If g_fso.GetExtensionName(strFilename) = "" Then
strFilename = strFilename & ".csv"
End If

' Replace instances of one or more space characters with a comma. Use
' the VBScript built-in RegExp object's Replace method to perform this
' task

Set re = New RegExp

' The pattern below means "one or more sequential space characters"

re.Pattern = "[ ]+"
re.Global = True
re.MultiLine = True
strCSVData = re.Replace(readline, ",")
On Error Resume Next
Set objFile = g_fso.OpenTextFile(strFilename, g_nMode, True)
nError = Err.Number
strErr = Err.Description
On Error Goto 0
If nError = 0 Then Exit Do

' Display a message indicating there were problems opening
' the file.

nResponse = crt.Dialog.MessageBox( _
"Failed to open """ & strFilename & """ (" & nError & "): " & _
vbcrlf & vbtab & strErr & vbcrlf & vbcrlf & _
"Check to see if the file is already open in another " & _
"application and make sure you have permissions to " & _
vbcrlf & "edit the file and create new files within the " & _
"destination folder.", _
"Save Operation Failed", _
If nResponse <> vbRetry Then Exit Sub
objFile.Write strCSVData & vbcrlf
g_strMode = "Wrote"
If g_nMode = ForAppending Then g_strMode = "Appended"
crt.Dialog.MessageBox _
g_strMode & " " & Len(readline) & " bytes to file:" & vbcrlf & _
vbcrlf & strFilename
' Now open the CSV file in the default .csv file application handler...
'g_shell.Run chr(34) & strFilename & chr(34)
End sub


Function BrowseForFile(strDefault)

' Determine if we're running on Windows XP or not...

Dim strOSName
Set objWMIService = GetObject("winmgmts:" & _
Set colSettings = _
objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOS In colSettings

' Windows XP "Name" might look like this:
' "Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\"...
' Vista might appear as follows:
' "Microsoft® Windows Vista™ Business |C:\Windows|\Device\Harddisk0\"...

strOsName = Split(objOS.Name, "|")(0)
Exit For
If Instr(strOsName, "XP") > 0 Then

' Based on information obtained from
' http://blogs.msdn.com/gstemp/archive.../17/74868.aspx
' NOTE: Will only work with Windows XP or newer since other OS's
' don't have a UserAccounts.CommonDialog ActiveX
' object registered.

Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.FileName = strDefault
objDialog.Filter = "CSV Files|*.csv|Text Files|*.txt|All Files|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = g_shell.SpecialFolders("MyDocuments")
nResult = objDialog.ShowOpen
If nResult <> 0 Then
BrowseForFile = objDialog.FileName
End If

' On Windows other than XP, we'll just pop up an InputBox

BrowseForFile = crt.Dialog.Prompt(_
"Save selected text to file:", _
"SecureCRT - Save Selected Text To File", _
End If
End Function
Reply With Quote
Old 01-19-2012, 04:27 PM
bgagnon bgagnon is offline
VanDyke Technical Support
Join Date: Oct 2008
Posts: 4,525
Hi JM3100,

Fortunately, since you want to capture the exact same rows/columns, if there is a certain prompt that is displayed when you access the second page, you can likely just insert a WaitForString, then call the SaveSelectedTextToCSVFile() function again.

For example:
crt.Screen.WaitForString "#"
The If/Then/Else and Select Case construct in that function will result in a prompt as to whether the new information should overwrite the existing file, append to existing file, or cancel execution of the script.

Are you manually just executing the script via the Script menu when the first screen (where you wish to capture data) is displayed?

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote
Old 01-19-2012, 04:57 PM
JM3100 JM3100 is offline
Registered User
Join Date: Jan 2012
Posts: 4
Yes, from the first page with the data, The amount of data will vary, Sometimes 2 pages other times 5 pages. But do not want to bite off too much scripting at one time. My idea is this.

1) work out getting the data off multiple pages
2) look for the last line of data that will start with the word "Total"
3) Get the script to run by itself every five minutes.

Eventually I will get there. But would just be glad to get the data at this point. Will try your comment tonight
Reply With Quote
Old 01-19-2012, 05:09 PM
bgagnon bgagnon is offline
VanDyke Technical Support
Join Date: Oct 2008
Posts: 4,525
Hi JM3100,

If the data is variable, the prior idea will probably not work. The best way we can assist in your scripting efforts is to know what data the remote sends. This would be captured in a raw log.

To be clear on what you need to do to resolve this issue, as well as to help you with the future endeavors, can you collect a raw log of a "typical session" per the instructions below?

To create a raw log:
  • Before connecting with the session, select Raw Log Session from SecureCRT's File menu.

  • In the Select Log File dialog, choose a folder and filename in which you would like the log text to be stored and press the Save button. At this point, if you open the File menu, you will notice that the Raw Log Session menu item now has a checkmark next to it; this indicates that raw logging is activated.

  • Now connect to the remote machine and access all the screens that include the information you wish to collect.

  • Once you have completed the manual tasks, disconnect the session and select Raw Log Session from the File menu to turn off the raw logging feature.

Since a raw log can contain sensitive information, feel free to send it as an attachment via e-mail to support@vandyke.com. Please reference "Attn Brenda - Forum Thread #10259" in the subject line.

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote
Old 01-19-2012, 05:37 PM
JM3100 JM3100 is offline
Registered User
Join Date: Jan 2012
Posts: 4
Will do. Will probably be a couple hours before I can kick that out
Reply With Quote
Old 01-20-2012, 12:36 PM
JM3100 JM3100 is offline
Registered User
Join Date: Jan 2012
Posts: 4
Log sent to support@vandyke.com
Reply With Quote

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 11:46 PM.