# $language = "VBScript"
# $interface = "1.0"
' UseIEAsCustomDialog.vbs
'
' Description:
' This sample script shows how the Internet Explorer automation object
' can be used to create custom dialog that can be used within a SecureCRT
' script. In this example, an "Send...Expect" dialog is created and
' displayed to the user for input. Then each "Send" value is sent to
' the remote machine, and the script waits until the corresponding "Expect"
' value is received from the remote.
'
' Although one could actually create a HTML "form" and store it in an
' actual .html file and then navigate to it, in this example, the IE
' object is navigated to "about:blank", and then generates the HTML
' document (aka "Dialog") programmatically.
Dim g_objIE, g_objTab
set g_objTab = crt.GetScriptTab
Dim g_fso, g_shell
Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")
g_objTab.Screen.Synchronous = True
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
if Not g_objTab.Session.Connected then
MsgBox "This script was designed to be launched from a tab " & _
"that is already connected to a remote machine."
exit sub
end if
Dim szExpect1, szExpect2, szExpect3
Dim szSend1, szSend2
Dim bLogOutput, szLogFile, szAppendOrOverwrite
' Set up default values for the dialog. These values are passed to the
' PromptForInput() function as "ByRef" references, so their values will
' be modified therein and will remain changed (or not, depending on the
' user's interaction with the dialog) once the dialog is dismissed.
szSend1 = "whoami"
szExpect1 = "]$"
szSend2 = "pwd"
szExpect2 = szExpect1
szSend3 = "who -w"
szExpect3 = szExpect1
bLogOutput = True
szLogFile = "C:\temp\MyLogFileName.log"
szAppendOrOverwrite = "Append"
' Save the current session's log file name so that we can twiddle it
' in the dialog, write to the new value, and then restore the original
' log file name
szSavedLogFileName = g_objTab.Session.LogFileName
Dim szTempLogFileName
' This loop gives us an opportunity to do some error checking with
' the return values provided once the dilaog's "OK" button is clicked,
' display an Error message, and then display the dialog again with the
' same values the user submitted intact. In this example, we're doing
' two checks on the log file, 1) Check that a log file name was supplied
' if logging option was enabled and 2) Check that the parent folder of
' the specified log file exists.
Do
if Not PromptForInput(szSend1, szExpect1, _
szSend2, szExpect2, _
szSend3, szExpect3, _
bLogOutput, szLogFile, _
szAppendOrOverwrite) then
MsgBox "User canceled."
exit sub
end if
if bLogOutput then
if szLogFile = "" then
MsgBox "Log filename required if Log option is enabled."
else
if Not g_fso.FolderExists(g_fso.GetParentFolderName(szLogFile)) then
MsgBox "Log folder path does not exist: " & vbcrlf & _
vbcrlf & _
vbtab & _
g_fso.GetParentFolderName(szLogFile) & vbcrlf & _
vbcrlf & _
"Please specify a log file name in an existing folder."
else
szTempLogFileName = szLogFile
exit do
end if
end if
else
exit do
end if
Loop
' Now that we have validated data, let's do the work...
' 1) Determine if we should log or not...
if bLogOutput then
' 1.1) Determine which log mode to use...
Dim bAppend
Select Case szAppendOrOverwrite
Case "Append"
bAppend = True
Case "Overwrite"
bAppend = False
Case Else
' We don't ever expect this unless there is some mistake
' in the radio button HTML code in the PromptForInput()
' function below.
MsgBox "Unknown LogMode value: " & szAppendOrOverwrite
exit sub
End Select
' 1.2) Turn off logging on the old file if it's enabled
if g_objTab.Session.Logging then g_objTab.Session.Log False
' 1.3) Set up the current session to log to the temporary log file
' specified in the user dialog
g_objTab.Session.LogFileName = szTempLogFileName
' 1.3) Start logging with the appropriate log mode
g_objTab.Session.Log True, bAppend
end if
' 2) Go through each send/expect... If the ExpectN string is empty,
' don't bother waiting for it.
g_objTab.Screen.Send szSend1 & vbcr
if szExpect1 <> "" then g_objTab.Screen.WaitForString szExpect1
g_objTab.Screen.Send szSend2 & vbcr
if szExpect2 <> "" then g_objTab.Screen.WaitForString szExpect2
g_objTab.Screen.Send szSend3 & vbcr
if szExpect3 <> "" then g_objTab.Screen.WaitForString szExpect3
' 3) Now that we're done, turn off logging.
if bLogOutput then
g_objTab.Session.Log False
' Restore the existing log file name for the current session
g_objTab.Session.LogFileName = szSavedLogFileName
end if
crt.Dialog.Messagebox _
"Operation completed."
if bLogOutput then
' Once we have completed the work, bring up the log file in the default
' file handler.
g_shell.Run chr(34) & szLogFile & chr(34)
end if
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function PromptForInput(byRef szSend1, byRef szExpect1, _
byRef szSend2, byRef szExpect2, _
byRef szSend3, byRef szExpect3, _
byRef bLogOutput, byRef szLogFile, _
byRef szAppendOrOverwrite)
' Sample function that prompts for the following information:
' 1) Send #1: First command to send to the remote
' 2) Expect #1: What to Expect after Send #1 is sent
' 3) Send #2: Second command to send to the remote
' 4) Expect #2: What to Expect after Send #2 is sent
' 5) Send #3: Third command to send to the remote
' 6) Expect #3: What to Expect after Send #3 is sent
' 7) LogOutput? If this checkbox is enabled, a "LogFile" entry field will
' become enabled, allowing the user to specify a log file in which
' the output of the sent commands and their results will be stored.
' 8) LogFilename: Enabled when the LogOuput box is checked. Specifies the
' name of the log file that SecureCRT should use to log the commands
' issued and their results.
' 9) AppendOrOverwrite? Radio button value determines whether the log file
' starts out a-fresh each time (overwrite), or appends to existing log
' information.
' RETURNS: True or False, depending on whether or not the user presses "OK", or
' "Cancel" from within the dialog.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' First step, set up the dialog (InternetExplorer)
Set g_objIE = CreateObject("InternetExplorer.Application")
g_objIE.Offline = True
g_objIE.navigate "about:blank"
' Wait for the navigation to the "blank" web page to complete
Do
crt.Sleep 100
Loop While g_objIE.Busy
g_objIE.Document.body.Style.FontFamily = "Sans-Serif"
' Here's where we "create" the elements of our dialog box. We basically
' throw HTML into the document, and IE loads it in real-time for us.
'
' The hidden "ButtonHandler" input is used to tie IE and
' SecureCRT together such that SecureCRT can know when a
' button is pressed, etc.
g_objIE.Document.Body.innerHTML = _
" Send1:
" & _
"Expect1:" & _
"