Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-18-2018, 11:24 AM
cynarl cynarl is offline
Registered User
 
Join Date: Sep 2018
Posts: 2
run function in all active sessions

I created a script to send multiple commands in all active sessions, but these commands are in another function, so when I run this script all the commands are send to one session.

Code:
Sub Main
	crt.Session.ConnectInTab("/S session1")
	Function1
	crt.Session.ConnectInTab("/S session2")
	Function1
    For nIndex = 1 to crt.GetTabCount
        Set objCurrentTab = crt.GetTab(nIndex)
        objCurrentTab.Activate
        if objCurrentTab.Session.Connected = True Then
        	Function1
        end if
    Next
End Sub

Sub Function1
	crt.Screen.Send "command 1" & vbcr
        crt.Screen.Send "command 2" & vbcr
End Sub

Last edited by jdev; 09-18-2018 at 11:44 AM. Reason: wrap code in [code] blocks to preserve indentation
Reply With Quote
  #2  
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: 931
Quote:
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:
    Code:
    Sub Main
    	crt.Session.ConnectInTab("/S session1")
    	Function1
    	crt.Session.ConnectInTab("/S session2")
    	Function1
        For nIndex = 1 to crt.GetTabCount
            Set objCurrentTab = crt.GetTab(nIndex)
            objCurrentTab.Activate
            if objCurrentTab.Session.Connected = True Then
            	Function1 objCurrentTab
            end if
        Next
    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.

    Code:
    ' 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
    Next
    
    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
        crt.CommandWindow.Send
    
        ' 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)
            objTab.Activate()
    
            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.
                objTab.Screen.WaitForString("#")
            End If
        Next
    Next
    
    ' 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: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #3  
Old 09-18-2018, 01:20 PM
cynarl cynarl is offline
Registered User
 
Join Date: Sep 2018
Posts: 2
Thank you Jake, now the script is running in all sessions
Reply With Quote
Reply

Tags
function , multiple , session


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:54 PM.