VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Script to run in Tabs (https://forums.vandyke.com/showthread.php?t=11628)

bigkid123 08-09-2014 06:45 PM

Script to run in Tabs
 
All,

I am fairly new to writing scripts and very limited to the knowledge needed to even know where to start. I have modified this one but not quite sure of how to get it to execute in tabs that are already connected to my servers. I am basically saving the screen data to a file but would like all of the sessions to log to the same file. I believe the script that I have below would accomplish this but not sure of the code to use to have it run per open tab. Any help would be appreciated.

I also have an issue of when it writes the SessionName into the .txt file it includes the folders with the Session name and not sure how to only show the Session name.

I have found this script to do a single / but not sure how to change it to multiple folders.

strSession = crt.Session.Path
If Instr(strSession, "\") Then
vSession = Split(strSession, "\")
strSession = vSession(1)
MsgBox strSession
Else
MsgBox strSession
End If

I also found the following code which I believe would accomplish the send commands to multiple tabs but not quite sure how to update the script below to combine everything that I am needing.

Any help/guidance would be appreciated.

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

' Example script showing how to send commands to multiple tabs

Option Explicit

Sub Main()

Dim tab, index, nTabCount

nTabCount = crt.GetTabCount()

for index = 1 to nTabCount
set tab = crt.GetTab(index)
tab.activate
tab.screen.send "ping 192.168.0.131" & vbcr
next

End Sub

The script below is what I use to pull the needed data from the screen.

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

' This script demonstrates how to capture line by line output
' from a command sent to a server. It then saves each line of output
' to a file. This script shows how the 'WaitForStrings' command can be
' use to wait for multiple possible outputs.

' Constants used by OpenTextFile()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub Main

crt.Screen.Synchronous = True

' Create an instance of the scripting filesystem runtime so we can
' manipulate files.
'
Dim fso, file
Dim strSessionName
Set fso = CreateObject("Scripting.FileSystemObject")

' Open a file for writing. The last True parameter causes the file
' to be created if it doesn't exist.
'
Set file = fso.OpenTextFile("c:\scrt.config\%S.txt", ForAppending, True)

' Stuff the data from the SecureCRT screen into a variable
' Using crt.Screen.Get2 instead of crt.Screen.Get allows us to capture
' \r and \n so that data appears in the file the same way it appears in
' the SecureCRT window.
szScreenData = crt.Screen.Get2(1, 1, crt.screen.Rows, crt.Screen.Columns)

'msgbox szScreenData

' Write the line out with an appended '\r\n'

' Extract data displayed on row 2, between columns 1 and 2
nProcesses = crt.Screen.Get(7,1,7,41)

' Extract data displayed on row 5, between columns 36 and 41
nFreeMem = crt.Screen.Get(5,36,5,41)

strSessionName = crt.Session.Path

file.Write strSessionName & vbCrLf
file.Write nProcesses & vbCrLf
' file.Write “Free Memory: “ & nFreeMem & VbCrLf
' file.Write szScreenData & vbCrLf
file.Close

crt.screen.synchronous = false

End Sub

bgagnon 08-11-2014 10:10 AM

Hello bigkid123,

It's a little difficult to tell what your inquiries actually are. :)

Let me address this last problem first.

Quote:

I also have an issue of when it writes the SessionName into the .txt file it includes the folders with the Session name and not sure how to only show the Session name.
Are you referring to these lines of the script?

Code:

Set file = fso.OpenTextFile("c:\scrt.config\%S.txt", ForAppending, True)

...

file.Write strSessionName & vbCrLf


If so, the session name is being set to the full path per this line:

Code:

strSessionName = crt.Session.Path

Using the GetOption method available to the SessionConfiguration object is probably a better way to get just the session name.

SecureCRT's help file topic Scripting / Script Objects Reference contains explanations (and often examples) of all the object, methods and properties available in SecureCRT.

Another good resource (for just VBScript and SecureCRT in general) is the scripting manual (which may be where you already got the information on Get).


Quote:

I have modified this one but not quite sure of how to get it to execute in tabs that are already connected to my servers.
I am not quite sure what "this one" is. What was your starting point? One of our example scripts?

To iterate over sessions already opened in tabs, see the GetTabCount() method available to the Application object. You will probably need several of those other methods as well (GetActiveTab(), GetScriptTab(), etc.)

bigkid123 03-15-2015 02:18 PM

Hi,

I just wanted to post a follow-up to this post as I was able to find a script on here and modify to my needs. This script will take data displayed between one column to another column and output the data to a text file for all opened tabs.

I have the data within the text file with the Tab Name and the data requested. Not sure if this will help others looking for something similar but thought I would post the resulting script.

Code:

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

' Example script showing how to send commands to multiple tabs
Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub Main()

Dim objCurrentTab, nIndex, nTabCount, fso, file, strSessionName, szScreenData, nProcesses, nFreeMem, vSession, bConnected, nRows, nCols

Set fso = CreateObject("Scripting.FileSystemObject")

For nIndex = 1 To crt.GetTabCount
        Set objCurrentTab = crt.GetTab(nIndex)
                bConnected = objCurrentTab.Session.Connected
                if bConnected then
                                nRows = objCurrentTab.Screen.Rows
        nCols = objCurrentTab.Screen.Columns

strSessionName = objCurrentTab.Session.Path

If Instr(strSessionName, "\") Then
vSession = Split(strSessionName, "Ahold\Ahold\")
strSessionName = vSession(1)
End If

' Extract data displayed on row 2, between columns 1 and 2
nProcesses = objCurrentTab.Screen.Get(13,12,13,59)

' Extract data displayed on row 5, between columns 36 and 41
'nFreeMem = objCurrentTab.Screen.Get(5,36,5,41)

Set file = fso.OpenTextFile("c:\scrt.config\ControllerReport.txt", ForAppending, True)
file.Write strSessionName & chr(9) & chr(9)
file.Write nProcesses & vbCrLf
file.Close
        End If
       
next

End Sub



All times are GMT -6. The time now is 07:04 PM.