#$language = "Python" #$interface = "1.0" # AutomateConnectSFTPForOpenTabs.py # Last Modified: 01 Nov, 2016 # - Initial revision # # DESCRIPTION: # Demonstrates opening SFTP tabs for each of the SSH2 # terminal connections currently open in the SecureCRT # window. Prior to running this script, you will connect # to each host in the normal way to get a shell # connection. Then, run this script. # NOTE: This script will close all of the active tabs that # were open prior to SFTP tabs being opened. If you # want original tabs to remain open, set the value # of the g_bCloseExistingShells variable to False. # # Once SFTP tabs have been opened and the original Shell # connections have been closed, this script opens the # Command Window and sets the 'Send Commands to All # Sessions" property to True, so that you can drive file # transfers for all SFTP connections in parallel. # Set up a dictionary to house existing tabs g_cExistingTabs = {} # Set up a dictionary to house new SFTP tabs we create g_cSFTPTabs = {} g_bCloseExistingShells = True g_bOpenCommandWindow = True g_bSendToAllCmdWinds = True #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Main(): global g_cExistingTabs global g_cSFTPTabs # Collect references for all the existing tabs that are # currently open (with shell connections active) for i in range(0, crt.GetTabCount()): objTab = crt.GetTab(i+1) strKey = "{0} - {1}".format(i+1, objTab.Caption) g_cExistingTabs[strKey] = crt.GetTab(i+1) # Connect SFTP tabs for each of the open Shell tabs for strKey in g_cExistingTabs.keys(): objTab = g_cExistingTabs[strKey] objSFTPTab = objTab.ConnectSftp() strKey = "{0} - {1}".format( objSFTPTab.Index, objSFTPTab.Caption) g_cSFTPTabs[strKey] = objSFTPTab # Wait for each of the SFTP tabs to be "ready", as # indicated by the presence of "sftp>" prompt. for strKey in g_cSFTPTabs.keys(): objSFTPTab = g_cSFTPTabs[strKey] objSFTPTab.Screen.Synchronous = False while True: strScreenText = objSFTPTab.Screen.Get2( 1, 1, objSFTPTab.Screen.Rows, objSFTPTab.Screen.Columns) # If we spot the "sftp> " prompt on the screen, # then we're pretty sure it's safe to start # interacting with the SFTP tab. Exit the loop: if "sftp> " in strScreenText: break # Otherwise, the absence of the "sftp> " prompt # on the screen indicates that the tab isn't # quite ready to go... sleep for a few # milliseconds and then check again... crt.Sleep(10) global g_bCloseExistingShells if g_bCloseExistingShells: # Now, go ahead and disconnect each of the original # shell tabs. objScriptTab = crt.GetScriptTab() for strKey in g_cExistingTabs.keys(): objTab = g_cExistingTabs[strKey] try: objTab.Close() except: objTab.Session.SetStatusText("Cannot close script tab") objTab.Session.Disconnect() if g_bOpenCommandWindow: crt.CommandWindow.Visible = True if g_bSendToAllCmdWinds: crt.CommandWindow.SendToAllSessions = True # Send a few innocuous commands to get things # started... crt.CommandWindow.Text = "lpwd" crt.CommandWindow.Send() crt.CommandWindow.Text = "pwd" crt.CommandWindow.Send() # Turn off the command window crt.CommandWindow.Visible = False if g_bCloseExistingShells: # Now, close the Script tab (has to be done last # because otherwise if we close the script tab, none # of the above commands would have been performed -- # because the script tab itself would have been # closed, ending the script prematurely. objScriptTab = crt.GetScriptTab() objScriptTab.Activate() objScriptTab.Screen.SendSpecial("MENU_TAB_CLOSE") Main()