View Single Post
Old 10-14-2014, 04:12 PM
rtb rtb is offline
VanDyke Technical Support
Join Date: Aug 2008
Posts: 4,306
Hi kgaudineer,

There are other dependencies. I am not sure how your script may be different, but in the unmodified script, it is lines 91 to 99 that read in the data from the commands file. The ReadDataFromFile() function definition begins on line 326. You can see that vCommands is an array that is passed by reference when the function is called to read the data from the commands file. After vCommands is populated with the commands to run, it is the foundation of the For Each...Next loop that starts on line 198.

I think that rather than try and get rid of the commands file, it may be easier to add in a little code.

If you were to use the concept that I suggested initially, you could modify the script to handle many different types of commands. The For Each...Next loop would look something like the following:
For Each strCommand In vCommands
    If strCommand = "" Then Exit For
    ' Handle interactive commands.  This could have multiple ElseIf statements.
    ' It would even be possible to include the IP Address in the commands file
    ' and parse it out in the code below.  Commented lines 10-14 and 21 are
    ' related to this possibility.
    If Instr(strCommand, "copy") > 0 Then
        ' Take special action
        ' If the command was of the following format:
        '     copy startup-config tftp,
        ' vData = Split(strCommand, ",")
        ' Send the command text to the remote
        ' g_objNewTab.Screen.Send vData[0] & vbcr
        g_objNewTab.Screen.Send strCommand & vbcr

        ' Wait "Address or name of remote host []" step
        g_objNewTab.Screen.WaitForStrings "#", "?"
        ' Send the IP address
        ' g_objNewTab.Screen.Send vData[1] & vbcr
        g_objNewTab.Screen.Send "" & vbcr
        ' Wait for "Destination filename [Cisco-2960-confg]" step
        g_objNewTab.Screen.WaitForStrings "#", "?"
        ' Press Enter to execute the command
        g_objNewTab.Screen.Send vbcr
        ' Proceed normally with a portion of the code
        ' Send the command text to the remote
        g_objNewTab.Screen.Send strCommand & vbcr

        ' Wait for the command to be echo'd back to us.
        g_objNewTab.Screen.WaitForString strCommand
    End If
    ' Since we don't know if we're connecting to a cisco switch or a
    ' linux box or whatever, let's look for either a Carriage Return
    ' (CR) or a Line Feed (LF) character in any order.
    vWaitFors = Array(vbcr, vblf)
    bFoundEOLMarker = False
        ' Call WaitForStrings, passing in the array of possible
        ' matches.
        g_objNewTab.Screen.WaitForStrings vWaitFors, 1
        ' Determine what to do based on what was found)
        Select Case g_objNewTab.Screen.MatchIndex
            Case 0 ' Timed out
                Exit Do
            Case 1,2 ' found either CR or LF
                ' Check to see if we've already seen the other
                ' EOL Marker
                If bFoundEOLMarker Then Exit Do
                ' If this is the first time we've been through
                ' here, indicate as much, and then loop back up
                ' to the  top and try to find the other EOL
                ' marker.
                bFoundEOLMarker = True
        End Select

    ' Now that we know the command has been sent to the remote
    ' system, we'll begin the process of capturing the output of
    ' the command.
    Dim strResult
    ' Use the ReadString() method to get the text displayed
    ' while the command was runnning.  Note that the ReadString
    ' usage shown below is not documented properly in SecureCRT
    ' help files included in SecureCRT versions prior to 6.0
    ' Official.  Note also that the ReadString() method captures
    ' escape sequences sent from the remote machine as well as
    ' displayed text.  As mentioned earlier in comments above,
    ' if you want to suppress escape sequences from being
    ' captured, set the Screen.IgnoreEscape property = True.
    strResult = g_objNewTab.Screen.ReadString(strPrompt)
    Dim objFile, strLogFile
    ' Set the log file name based on the remote host's IP
    ' address and the command we're currently running.  We also
    ' add a  date/timestamp to help make each filename unique
    ' over time.
    strLogFile = Replace( _
        g_strLogFileTemplate, _
        "IPADDRESS", _

    ' Replace any illegal characters that might have been
    ' introduced by the command we're running (e.g. if the
    ' command had a path or a pipe in it)
    strCleanCmd = Replace(strCommand, "/", "[SLASH]")
    strCleanCmd = Replace(strCleanCmd, "\", "[BKSLASH]")
    strCleanCmd = Replace(strCleanCmd, ":", "[COLON]")
    strCleanCmd = Replace(strCleanCmd, "*", "[STAR]")
    strCleanCmd = Replace(strCleanCmd, "?", "[QUESTION]")
    strCleanCmd = Replace(strCleanCmd, """", "[QUOTE]")
    strCleanCmd = Replace(strCleanCmd, "<", "[LT]")
    strCleanCmd = Replace(strCleanCmd, ">", "[GT]")
    strCleanCmd = Replace(strCleanCmd, "|", "[PIPE]")
    strLogFile = Replace(strLogFile, "COMMAND", strCleanCmd)
    ' Add Time stats to the log file name based on the Template
    ' defined by the script author.
    strLogFile = Replace(strLogFile, "YYYY-", Year(Date) & "-")
    strLogFile = Replace(strLogFile, "-MM-", "-" & NN(Month(Date)) & "-")
    strLogFile = Replace(strLogFile, "-DD-", "-" & NN(Day(Date)) & "-")
    strLogFile = Replace(strLogFile, "-hh'", "-" & NN(Hour(Time)) & "'")
    strLogFile = Replace(strLogFile, "'mm'", "'" & NN(Minute(Time)) & "'")
    strLogFile = Replace(strLogFile, "'ss", "'" & NN(Second(Time)))
    Set objFile = g_fso.OpenTextFile(strLogFile, ForAppending, True)

    ' If you want the command logged along with the results,
    ' uncomment the next two lines
    ' objFile.WriteLine "Results of command """ & strCommand & _
    '    """ sent to host """ & g_strHost & """: "

    ' Write out the results of the command and a separator
    objFile.WriteLine strResult
    ' Close the log file
There may be additional changes needed, but this is an example of how it would look.

Does this help you move forward in creating your script?

VanDyke Software
Technical Support
Reply With Quote