# $language = "VBScript" # $interface = "1.0" ' Command(Chat)WindowScriptingExample(7.2+).vbs ' Last Modified: ' 15 Jun, 2015 ' - Added RestoreCommandWindowState helper sub and related ' global variables ' - Added "Part 1" and "Part 2" comments ' ' 22 Aug, 2013 ' - Initial version ' ' DESCRIPTION: ' Demonstrates how to automate the Command (formerly known ' as 'chat') window in SecureCRT 7.2 and newer. ' ' CommandWindow API is currently: ' Methods ' ======================================================== ' Send() Simulates pressing the Enter ' key within the command (chat) ' window; sends contents of the ' command window to the remote ' device. ' ' Properties ' ========================================================= ' SendToAllSessions Boolean Read/Write value that ' governs whether the command(s) ' in the Command window are sent ' just to the active script tab ' or session (False) or to all ' connected sessions/tabs within ' the current SecureCRT window ' (True). ' ' -------------------------------------------------------- ' Text String Read/Write value that ' determines the ASCII characters ' that are currently displayed in ' (read) or to be populated into ' (write) the command window. ' ' -------------------------------------------------------- ' Visible Boolean Read/Write value which ' determines if the command window ' is visible or not. Dim g_bCommandWindowWasVisible, g_nScriptTabIndex, g_bSendToAllSessions Sub Main() Set objScriptTab = crt.GetScriptTab g_nScriptTabIndex = objScriptTab.Index bCommandWindowWasVisible = crt.CommandWindow.Visible ' Make the command (chat) window visible crt.CommandWindow.Visible = True ' Store the current "SendToAll" setting so that we can ' restore it after our example script is done bOrigSendToAll = crt.CommandWindow.SendToAllSessions ' -------------------------------------------------------------------- ' Part 1: Demonstrate sending command through the command window ' to *ONLY* the script tab (the active tab at the time ' this script was originally launched): ' -------------------------------------------------------------------- ' Make sure the command (chat) window is *not* sending ' chat/commands to all sessions... crt.CommandWindow.SendToAllSessions = False strCommand = "ls -l" ' Populate the command window with text... crt.CommandWindow.Text = strCommand ' Send the contents of the command window to the remote crt.CommandWindow.Send crt.Sleep 300 ' Iterate through each tab, activating it for a brief ' moment so the user can see that the text was only ' sent to the tab that was active when the script was ' first started: For nTab = 1 To crt.GetTabCount crt.GetTab(nTab).Activate If nTab = g_nScriptTabIndex Then crt.Sleep 1500 Else crt.Sleep 500 End If Next ' Activate the original tab that was active when the ' script first started: objScriptTab.Activate ' -------------------------------------------------------------------- ' Part 2: Demonstrate sending command through the command window ' to all connected sessions: ' -------------------------------------------------------------------- ' Turn on the "Send commands to all Sessions" option so that ' text in the command window is sent to all connected ' sessions in the Same SecureCRT Window. crt.CommandWindow.SendToAllSessions = True strCommand = "pwd" ' Populate the command window with another command... crt.CommandWindow.Text = strCommand ' Send the contents of the command window to all ' connected sessions (sent to all because the ' crt.CommandWindow.SendToAllSessions property has ' been set to True): crt.CommandWindow.Send crt.Sleep 300 ' Iterate through each tab, activating it for a brief ' moment so the user can see that the text was sent ' sent to all connected tabs/sessions, not just the ' one that was active when the script was first started: For nTab = 1 To crt.GetTabCount crt.GetTab(nTab).Activate If nTab = g_nScriptTabIndex Then crt.Sleep 1500 Else crt.Sleep 500 End If Next RestoreCommandWindowState End Sub ' ----------------------------------------------------------------------------- Sub RestoreCommandWindowState() ' Now, restore what the command windows "send to all" ' option as it was before the script was started crt.CommandWindow.SendToAllSessions = g_bOrigSendToAll ' Now, restore visibility of the command window to what ' it was prior to running the script (after all, the ' command window may not have been visible prior): crt.CommandWindow.Visible = g_bCommandWindowWasVisible ' Activate the original tab that was active when the ' script first started: Set objTab = crt.GetTab(g_nScriptTabIndex) objTab.Activate End Sub