View Single Post
Old 09-18-2018, 12:17 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 990
so when I run this script all the commands are send to one session.
This is expected.
  • crt.Screen *always* refers to the tab that was active when the script was launched, so using crt.Screen will always refer to the screen associated with the tab that was active when the script was launched.
  • crt.Session *always* refers to the tab that was active when the script was launched, so using crt.Session will always refer to the session in the tab that was active when the script was launched.

If you desire to send commands to all of your sessions from within a script, you should take one of these approaches:
  • Iterate over all the open tabs, get a reference to each tab, then use that Tab object's Screen to send commands to that remote device via that tab's Screen object. You're kind-of already doing this inside of your Main() sub. The fix for you would be to change your Function1 definition to take a parameter (that being the Tab object that should be used when performing the "Send"), and change how you're calling Function1 so that you pass in the tab object. For example:
    Sub Main
    	crt.Session.ConnectInTab("/S session1")
    	crt.Session.ConnectInTab("/S session2")
        For nIndex = 1 to crt.GetTabCount
            Set objCurrentTab = crt.GetTab(nIndex)
            if objCurrentTab.Session.Connected = True Then
            	Function1 objCurrentTab
            end if
    End Sub
    Sub Function1(objTab)
    	objTab.Screen.Send "command 1" & vbcr
            objTab.Screen.Send "command 2" & vbcr
    End Sub
    I can't pass up the opportunity to warn about calling consecutive Send() operations one right after the other without first waiting for some sort of confirmation from the remote device that it's ready to receive a command. Otherwise, you'll risk attempting to send a command to the remote that the remote may very well drop without warning because it's not ready to receive any commands. Take a look at the Scripting Guide, specifically chapter 4, more specifically, section 4.2.

  • Use SecureCRT's CommandWindow API to send commands to all of your connected tabs. This technique is only available in SecureCRT versions 7.2 and later. It's demonstrated in the Example: Command (Chat) Window Automation posted back in 2015.

    If you used this approach, you wouldn't need to iterate over existing tabs so much when sending commands as compared with verifying that each command was received and the remote system is ready again for another command.

    ' Turn on Synchronous so we don't miss out on the
    ' ability to wait for data arriving from the remote
    ' before it gets displayed to the screen:
    For nIndex = 1 To crt.GetTabCount()
        Set objTab = crt.GetTab(nIndex)
        objTab.Screen.Synchronous = True
    bOrigVisible = crt.CommandWindow.Visible
    If Not bOrigVisible Then
        crt.CommandWindow.Visible = True
    End If
    bOrigSendToAll = crt.CommandWindow.SendToAllSessions
    If Not bOrigSendToAll Then
        crt.CommandWindow.SendToAllSessions = True
    End If
    vCommands = Array("command 1", "command 2", "command 3")
    For Each strCommand In vCommands
        crt.CommandWindow.Text = strCommand
        ' Iterate through all existing tabs to make
        ' sure each tab received and echo'd the command,
        ' and that the command completed...
        For nIndex = 1 To crt.GetTabCount()
            ' Wait for the command to be echo'd from the remote
            Set objTab = crt.GetTab(nIndex)
            If objTab.Session.Connected Then
                objTab.Screen.WaitForString(strCommand) & vbcr
                ' Wait for evidence that the command has completed
                ' Perhaps this is the last character or last few
                ' characters of the remote shell prompt. Change this
                ' to match what is common to all of your tabs. If you
                ' don't have commonality among your tabs, then you'll
                ' have to do some extra work to set up an array of
                ' prompts specific to each tab, so that you can get
                ' at the prompt specific to tab #1, tab #2, and so on.
            End If
    ' Restore the state of SendToAll, and Visibility of the
    ' Command Window
    crt.CommandWindow.SendToAllSessions = bOrigSendToAll
    crt.CommandWindow.Visible = bOrigVisible
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel:
Reply With Quote