VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Connect to multiple hosts and execute a script on each - asynchronously (https://forums.vandyke.com/showthread.php?t=12451)

fredless 08-23-2016 01:23 PM

Connect to multiple hosts and execute a script on each - asynchronously
 
Scenario is this: have a script which reads a list of hosts into an array; then walks the array, connecting to each host (adhoc) & executing a configuration script.

My issue is that this operation occurs serially\synchronously and the script requires several minutes per host to complete. When our host array hit 50-200 hosts, the wait time for the whole process to execute is quite long obviously.

Is there way to programmatically spawn new tabs and have each tab execute a script independently? Obviously this is possible at some level, as you can do this when connecting to multiple saved sessions interactively. You can also use the /script standard argument to do this from the Win command line (but that isn't a scalable solution when you are talking about this many simultaneous connections, I've tried). But I haven't figured out a straightforward way to do this with adhoc sessions in script.

Working with v8.0.2

jdev 08-23-2016 05:18 PM

First, it's been my experience that SecureCRT's performance can handle 30-40 simultaneous connections open in Tabs within the same SecureCRT window before it becomes too bogged down, so I'd limit each "batch" to ~30 sessions at a time, to begin with.

Second, depending on how you want to accomplish what you're doing, you might want to move through blocks of 30 at a time and use the ability of the SecureCRT button bar to shift-click a button and have that action take place in parallel in all connected tabs in the same SecureCRT window. Here's an example script for your learning and benefit to demonstrate this approach in brevity:
Code:

' TabSafeScriptingExample(Shift-Click-ButtonBar).vbs
'
' Exemplifies tab-safe scripting so that the same script can
' run in parallel in each connected Tab in the self-same
' SecureCRT window by shift-clicking a button on the Button
' Bar that is set to run this script.

Set objTab = crt.GetScriptTab
objTab.Screen.Synchronous = True

' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
    nTabIndex = objTab.Index
    ' Get the text to the left of the cursor as the shell prompt
    strPrompt = objTab.Screen.Get(_
        objTab.Screen.CurrentRow, 1, _
        objTab.Screen.CurrentRow, objTab.Screen.CurrentColumn - 1)

    '======
    ' Send a command 'term len 0':
    strCmd = "term len 0"
    objTab.Screen.Send strCmd & vbcr
    ' Wait for the command to be echoe'd back:
    objTab.Screen.WaitForString strCmd & vbcr
    ' Wait for evidence that the command has completed (shell prompt appears):
    objTab.Screen.WaitForString strPrompt

    ' ======
    ' Send a command 'sh run':
    strCmd = "sh run"
    objTab.Screen.Send strCmd & vbcr
    ' Wait for the command to be echoe'd back:
    objTab.Screen.WaitForString strCmd & vbcr
    ' Wait for evidence that the command has completed (shell prompt appears):
    objTab.Screen.WaitForString strPrompt

    ' ======
    ' Send a command 'sh startup':
    strCmd = "sh startup"
    objTab.Screen.Send strCmd & vbcr
    ' Wait for the command to be echoe'd back:
    objTab.Screen.WaitForString strCmd & vbcr
    ' Wait for evidence that the command has completed (shell prompt appears):
    objTab.Screen.WaitForString strPrompt

    ' ---------------------------------------------------------------------
    ' Display time of completion (w/ms) in the status bar for this tab/session:
    crt.Session.SetStatusText "Tab #" & nTabIndex & " done: " & _
        GetCurTimeWithMSeconds
End Sub


' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function GetCurTimeWithMSeconds()
    ' Use WMI to get at the current time values
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objItem In colItems
        strLocalDateTime = objItem.LocalDateTime
    Next
    ' strLocalDateTime has the following pattern:
    ' 20111013093717.418000-360  [ That is,  YYYYMMDDHHMMSS.MILLIS(zone) ]
    strYear = Left(strLocalDateTime, 4)
    strMonth = Mid(strLocalDateTime, 5, 2)
    strDay = Mid(strLocalDateTime, 7, 2)
    strHour = Mid(strLocalDateTime, 9, 2)
    strMinute = Mid(strLocalDateTime, 11, 2)
    strSecond = Mid(strLocalDateTime, 13, 2)
    ' skip the "." and start with offset char #16
    strMillis = Mid(strLocalDateTime, 16, 3)  ' 6 digits, but last 3 are always zero
    GetCurTimeWithMSeconds = strYear & strMonth & strDay & "_" & _
        strHour & strMinute & strSecond & "." & strMillis
End Function

Third, if you really want to go down the semi-parallel route through scripting w/o the use of the button bar shift-click method, you'd want to engineer your script to issue one command at a time in each of your tabs (in series) within your script. Instead of waiting for the entire series of commands to complete in one tab and then moving on to the next, you'd wait for the current command to complete in each and every tab, before moving on with the next command and so on. This way, only the slowest tab of all would hold up the process. This concept is demonstrated in the "RunCommandsInParallelTabs(Pseudo).vbs.txt" file attached to this existing forum post by bgagnon.

--Jake


All times are GMT -6. The time now is 11:11 AM.