# $language = "VBScript" # $interface = "1.0" ' SFTPTabAutomation.vbs ' Last Modified: ' 23 Jan, 2023 ' - Demonstrate how to retrieve local and remote working directories ' - Demonstrate how to determine local "Documents" folder ' - Demonstrate one approach of how to detect 'cd' failures ' - Demonstrate how to display text to SecureCRT's terminal screen ' without sending any data to the connected host/device ' - With success, display a countdown leading up to closing the SFTP ' tab. ' - With failure, display error message in red and leave SFTP tab open ' ' 26 Mar, 2020 ' - Bug Fix: Close is a method; use Close() ' - Added Disconnect() call prior to Close() ' - No longer use a Dialog to display success/fail. Instead ' we send text to the screen. ' ' 04 Apr, 2012 ' - Initial version ' ' DESCRIPTION: ' Demonstrates opening an SFTP tab & automating a file download. ' Prior to running this script, the currently active tab must already be ' connected with the SSH2 protocol to a remote server that is capable of ' providing SFTP functionality; otherwise an error will occur and script ' execution will be halted. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub Main() ' Open up an SFTP tab based on the current (script tab) connection Set objSFTPTab = crt.GetScriptTab.ConnectSFTP ' When using Get2, Screen.Synchronous should be False, or else nothing ' will appear on the screen (since we're not using any WaitForString() ' or ReadString() methods). objSFTPTab.Screen.Synchronous = False ' Wait for the 'sftp> ' prompt to appear on the screen as an indication that ' interaction with the SFTP tab can begin. Do strScreenText = _ objSFTPTab.Screen.Get2(1, 1,crt.Screen.Rows,crt.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 do..loop: If Instr(strScreenText, "sftp> ") Then Exit Do ' 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 Loop ' Now we can work with the SFTP connection. ' NOTE: We turn on Synch = True now so that we don't miss any data. ' This means that for EVERY Screen.Send(), we must do an accompanying ' Screen.WaitForString() so that we don't get out of sync. objSFTPTab.Screen.Synchronous = True ' Determine our current remote directory objSFTPTab.Screen.Send("pwd" & vbcr) WaitForSequence Array("pwd" & vbcrlf), objSFTPTab strCurPWD = objSFTPTab.Screen.ReadString(vbcrlf & "sftp> ") objSFTPTab.Session.SetStatusText "Cur: " & strCurPWD ' Get a long file listing with the "ls -l" command objSFTPTab.Screen.Send "ls -l" & vbcr WaitForSequence Array("ls -l", vbcrlf), objSFTPTab ' Use ReadString to wait for the "sftp> " prompt to arrive, capturing all ' the data we receive in the mean time. strFileListing = objSFTPTab.Screen.ReadString("sftp> ") If Len(strFileListing) > 1024 Then strDataToDisplay = Left(strFileListing, 1024) & vbcrlf & "[truncated]" Else strDataToDisplay = strFileListing End If 'crt.Dialog.MessageBox("File Listing" & vbcrlf & strDataToDisplay) ' Change local directory (local to the SecureCRT Windows machine) to the ' folder in which the downloaded file should be saved: strLocDocuments = (CreateObject("WScript.Shell")).SpecialFolders("MyDocuments") objSFTPTab.Screen.Send "lcd """ & strLocDocuments & """" & vbcr objSFTPTab.Screen.WaitForString "sftp> " objSFTPTab.Screen.Send "lpwd" & vbcr WaitForSequence Array("lpwd" & vbcrlf), objSFTPTab strCurLocDir = objSFTPTab.Screen.ReadString(vbcrlf & "sftp> ") 'crt.Dialog.MessageBox("Current local dir: " & strCurLocDir) ' Change remote directory to the path where the file to be downloaded ' is expected to exist: objSFTPTab.Screen.Send "cd tmp" & vbcr WaitForSequence Array("cd tmp", vbcrlf), objSFTPTab strCDResults = objSFTPTab.Screen.ReadString("sftp> ") If Len(strCDResults) > 0 Then DisplayToScreen vbcrlf & vbcrlf & _ chr(27) & "[31mFailed to cd to directory!" & vbcrlf & vbcrlf & _ vbtab & chr(27) & "[7m" & strCDResults & chr(27) & "[0m", _ objSFTPTab DisplayToScreen vbcrlf & vbcrlf & _ "Error detected; leaving tab open.", objSFTPTab Exit Sub End If ' Get (download) a file named MyFile.txt objSFTPTab.Screen.Send "get MyFile.txt" & vbcr WaitForSequence Array("get MyFile.txt", vbcrlf), objSFTPTab strResults = objSFTPTab.Screen.ReadString("sftp> ") ' Check for success/failure If Instr(strResults, "100%") = 0 Then DisplayToScreen vbcrlf & vbcrlf & _ chr(27) & "[31mFailed to get file!" & vbcrlf & vbcrlf & vbtab & _ chr(27) & "[7m" & strResults & chr(27) & "[0m", _ objSFTPTab DisplayToScreen vbcrlf & vbcrlf & _ "Error detected; leaving tab open.", objSFTPTab Exit Sub End If ' Put the information we received from the screen into an array where ' each line of the output gathered is in a unique element of the array vDataLines = Split(strResults, vbcrlf) ' Display success along with the 2nd-to-last line of the information; ' that's the line that contains stats about the successful transfer. DisplayToScreen vbcrlf & vbcrlf & _ chr(27) & "[7;32mFile transfer successful" & chr(27) & "[0m" & _ vbcrlf & vDataLines(UBound(vDataLines) - 1) & _ vbcrlf & vbcrlf, _ objSFTPTab ' Display a countdown of 5 seconds until the tab will get closed. nCountdownSeconds = 5 Do While nCountdownSeconds > 0 If nCountdownSeconds > 1 Then strSeconds = "seconds" If nCountdownSeconds = 1 Then strSeconds = "second " DisplayToScreen vbcr & "Closing SFTP tab in " & _ nCountdownSeconds & " " & strSeconds & "...", objSFTPTab crt.Sleep(1000) nCountdownSeconds = nCountdownSeconds - 1 Loop ' Close the SFTP tab once we're done. In case the Global Option "Confirm ' on disconnect" is enabled, we'll first disconnect this SFTP tab prior ' to attempting to close it. objSFTPTab.Session.Disconnect objSFTPTab.Close End Sub ' --------------------------------------------------------------------- Sub WaitForSequence(vSequenceOfStrings, objTab) For Each strEntry In vSequenceOfStrings objTab.Screen.WaitForString(strEntry) Next End Sub ' --------------------------------------------------------------------- Sub DisplayToScreen(strData, objTab) bOrigSync = objTab.Screen.Synchronous If bOrigSync Then objTab.Screen.Synchronous = False objTab.Screen.Send strData, True crt.Sleep 1 If bOrigSync Then objTab.Screen.Synchronous = True End Sub