# $language = "VBScript" # $interface = "1.0" ' Command(Chat)WindowScriptingTutorial(7.2+).vbs ' Last Modified: ' 15 Jun, 2015 ' - Added RestoreCommandWindowState helper sub and related ' global variables ' - Added "Part 1" and "Part 2" comments ' - Added tutorial-like check-points throughout the script ' to present more of a "walk-through" progression with ' opportunities for bailing out at each step if desired. ' ' 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 nResult = crt.Dialog.MessageBox(_ "This example script contains two separate demonstrations:" & _ vbcrlf & vbcrlf & _ "1) Drives command window to send data only to the active tab/session." & _ vbcrlf & vbcrlf & _ "2) Drives command window to send data to all connected tabs/sessions." & _ vbcrlf & vbcrlf & _ "Press OK to continue", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then MsgBox nResult RestoreCommandWindowState Exit Sub End If ' -------------------------------------------------------------------- ' 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 nResult = crt.Dialog.MessageBox(_ "The script has made sure the command window is visible, turned " & _ "OFF the ""Send to All Sessions"" option, and populated the " & _ "command window with the following command:" & vbcrlf & _ vbtab & strCommand & _ vbcrlf & vbcrlf & _ "Press OK to continue (command will only be sent to the active tab)", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then MsgBox nResult RestoreCommandWindowState Exit Sub End If ' Send the contents of the command window to the remote crt.CommandWindow.Send crt.Sleep 300 nResult = crt.Dialog.MessageBox(_ "Now the script will activate each tab in turn, so you can see " & _ "that the command (" & strCommand & ") was sent only to the " & _ "active tab/session." & _ vbcrlf & vbcrlf & _ "Press OK to continue", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then RestoreCommandWindowState Exit Sub End If ' 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 nResult = crt.Dialog.MessageBox(_ "Part 1 of the demo is now complete." & vbcrlf & vbcrlf & _ "Now the script will enable the ""Send to All Sessions"" option." & _ vbcrlf & _ "(A visual indicator will appear above the Command Window)" & _ vbcrlf & vbcrlf & _ "Press OK to continue", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then RestoreCommandWindowState Exit Sub End If ' -------------------------------------------------------------------- ' 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 nResult = crt.Dialog.MessageBox(_ "The script has now populated the command window with the " & _ "following command:" & vbcrlf & _ vbtab & strCommand & _ vbcrlf & vbcrlf & _ "Press OK to continue" & vbcrlf & _ "(command will be sent to all connected tabs/sessions)", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then RestoreCommandWindowState Exit Sub End If ' 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 nResult = crt.Dialog.MessageBox(_ "Now the script will activate each tab in turn, so you can see " & _ "that the command (" & strCommand & ") was sent to all the " & _ "connected tabs/sessions." & _ vbcrlf & vbcrlf & _ "Press OK to continue", _ "Command Window Script Example", _ vbOKCancel) If nResult <> vbOK Then RestoreCommandWindowState Exit Sub End If ' 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 crt.Dialog.MessageBox "The script example is now completed." & _ vbcrlf & vbcrlf & _ "The ""Send to All Sessions"" option and the state of the " & _ "Command Window has been restored to what it was prior to " & _ "this script being run." 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