Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBS Script Help Part III

Posted on 2013-06-27
2
Medium Priority
?
380 Views
Last Modified: 2013-06-27
Hello Experts,

Can someone please tell me why the text 'duplex' in case 3, doesn't call the following section of the script:

Dim I_vCommands(100)
I_vCommands(0) = "READSTRING_DUP"
'I_vCommands(1) = "show run"
'I_vCommands(2) = "show ip bgp"

Open in new window


The full script is as follows:

# $language = "VBScript"
# $interface = "1.0"

' ASSUMPTION: file being 'tailed' contains information
'             similar to:
'   Jan 5 18:26:13 device01 9927: Jan 5 18:26:12: %LINK-3-UPDOWN: Interface POS5/2, changed state to down
'   Jan 5 18:26:47 device02 6332: Jan 5 18:26:46.243: %LINK-3-UPDOWN: Interface GigabitEthernet3/1, changed state to down
'   Jan 5 18:28:40 device01 9937: Jan 5 18:28:39: %LINK-3-UPDOWN: Interface POS5/2, changed state to down
Dim g_fso, g_shell
Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")

Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

Dim g_szLogFile, g_szFirstLogFilePath, objTab

g_szLogFile = GetMyDocumentsFolder & "\Command#__NUM___Results.txt"

Set objTab = crt.GetScriptTab

Dim g_vCommands(100)
g_vCommands(0) = "READSTRING_UP"
'g_vCommands(1) = "readstringONE"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "READSTRING_DOWN"
'h_vCommands(1) = "show run"
'h_vCommands(2) = "show ip bgp"


' .
' .
' .

Dim I_vCommands(100)
I_vCommands(0) = "READSTRING_DUP"
'I_vCommands(1) = "show run"
'I_vCommands(2) = "show ip bgp"


' .
' .
' .

Sub Main()

	
    ' Make sure that we are running in Synchronous mode so
    ' that when WaitForStrings() finds something, we can
    ' capture the current line without risk of it already
    ' having flown by on the screen.  This slows things down
    ' a bit, but provides insurance against errors due to
    ' SecureCRT and the script running asynchronously.
    crt.Screen.Synchronous = true

    ' Start tailing a file...
    crt.Screen.Send "tail tailfile.txt" & vbcr

  ' Example #1: Uses WaitForString to look for just the text
  '             we care about.  Once the text is found, the
  '             line on which the text was located is captured
  '             and a piece of information is parsed using the
  '             Split() builtin VBScript method.
    CaptureJustTheLinesWeCareAbout
	
  ' Example #2: Uses WaitForString to detect when every new line
  '             arrives from the remote.  Each line of text is
  '             captured, and the Instr() builtin VBScript
  '             function is used to detect the presence of the
  '             specific text we care about.  Iff it is present,
  '             the Split() builtin VBScript method is used to
  '             parse out the relevant piece of information.
  '
  '             The benefit of this method over
  '             CaptureJustTheLinesWeCareAbout is that we
  '             can retrieve information from the line if
  '             the text we care about comes prior to the
  '             relevant piece of information that we would
  '             like to display as part of our notification.
  '
  '             To enable this example, comment out the statement
  '             on line 29 and uncomment the line below:
  '  CaptureAllLinesAndSearchForTextWeCareAbout

End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub CaptureJustTheLinesWeCareAbout()
  MsgBox "Capturing just the lines we care about"
  do
    ' Look for specific output
	
    dim result 
	result = crt.Screen.WaitForStrings("changed state to up","changed state to down","Duplicate address","duplex")
	

    ' Once specific output is found, capture the line
    ' of text on which the "changed to state down" string
    ' was located.
    szCurrentLine = crt.Screen.Get(crt.Screen.CurrentRow, _
                                   0, _
                                   crt.Screen.CurrentRow, _
                                   crt.Screen.Columns)

    ' Trim off any leading or training spaces
    szCurrentLine = Trim(szCurrentLine)

    ' Use the Split() method (builtin VBScript method)
    ' to get at the component we care about (deviceID)
    vElements = Split(szCurrentLine, " ")

	for each x in vElements 
		'document.write(x & "<br />")
	next

		'MsgBox "Device """ & vElements(3) & """ just went down."

	g_szLogFile = GetMyDocumentsFolder & "\Command#__NUM___Results.txt"

	Set objTab = crt.GetScriptTab

	select case result
	case 1
'handle "changed state to up"
		LogOutputOfSpecificCommandUseReadStringUP
	case 2
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadStringDOWN
	case 3
'handle "Duplicate address","duplex"
		LogOutputOfSpecificCommandUseReadStringDUP
	end select

   'MsgBox "Device """ & vElements(0) & vElements(1) & vElements(2) & vElements(3) & """ just went down."

'MsgBox "Device """ & vElements(0) & vElements(1) & vElements(2) & vElements(3) & "is " vElements(6)"
  Loop

End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub LogOutputOfSpecificCommandUseReadStringUP()
    ' Instruct WaitForString and ReadString to ignore escape sequences when
    ' detecting and capturing data received from the remote (this doesn't
    ' affect the way the data is displayed to the screen, only how it is handled
    ' by the WaitForString, WaitForStrings, and ReadString methods associated
    ' with the Screen object.
    objTab.Screen.IgnoreEscape = True
    objTab.Screen.Synchronous = True
   
    If Not objTab.Session.Connected then
        crt.Dialog.MessageBox _
            "Not Connected.  Please connect before running this script."
        exit sub
    end if

    Dim szCommand, szPrompt, nRow, szLogFileName, nIndex

    ' If this script is run as a login script, there will likely be data
    ' arriving from the remote system.  This is one way of detecting when it's
    ' safe to start sending data. If this script isn't being run as a login
    ' script, then the worst it will do is seemingly pause for one second
    ' before determining what the prompt is.
    ' If you plan on supplying login information by waiting for username and
    ' password prompts within this script, do so right before this do..loop.
    Do
        bCursorMoved = objTab.Screen.WaitForCursor(1)
    Loop until bCursorMoved = False
    ' Once the cursor has stopped moving for about a second, we'll
    ' assume it's safe to start interacting with the remote system.
    
    ' Get the shell prompt so that we can know what to look for when
    ' determining if the command is completed. Won't work if the prompt
    ' is dynamic (e.g. changes according to current working folder, etc)
    nRow = objTab.Screen.CurrentRow
    szPrompt = objTab.screen.Get(nRow, _
                                 0, _
                                 nRow, _
                                 objTab.Screen.CurrentColumn - 1)
    szPrompt = Trim(szPrompt)

    Dim szLogFile
    nIndex = 0
    Do
        szCommand = Trim(g_vCommands(nIndex))


        ' If the command is empty, then we should be done issuing commands
        ' (there's nothing else in our command array g_vCommands)
        if szCommand = "" then Exit Do

        ' Set up the log file for this specific command
        szLogFile = Replace(g_szLogFile, "__NUM__", NN(nIndex + 1, 2))
        
        ' Store the path for our first log file for later use (see end of this
        ' function...
        if g_szFirstLogFilePath = "" then g_szFirstLogFilePath = szLogFile

        ' Send the command text to the remote
        objTab.Screen.Send szCommand & vbcr

        ' Wait for the command to be echo'd back to us.
        objTab.Screen.WaitForString vbcr, 1
        objTab.Screen.WaitForString vblf, 1        

        Dim szResult
        ' 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.
        szResult = objTab.Screen.ReadString(szPrompt)
        
        Dim objFile
        Set objFile = g_fso.OpenTextFile(szLogFile, ForWriting, True)

        ' If you don't want the command logged along with the results, comment
        ' out the very next line
        objFile.WriteLine "Results of command: " & szCommand

        ' Write out the results of the command to our log file
        objFile.WriteLine szResult
        
        ' Close the log file
        objFile.Close

        ' Move on to the next command in our command array g_vCommands
        nIndex = nIndex + 1
    Loop
    
    ' Once we're complete, let's bring up Windows Explorer with the first of
    ' the log files selected.
    g_shell.Run "explorer /e,/select,""" & g_szFirstLogFilePath & """"

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub LogOutputOfSpecificCommandUseReadStringDOWN()
    ' Instruct WaitForString and ReadString to ignore escape sequences when
    ' detecting and capturing data received from the remote (this doesn't
    ' affect the way the data is displayed to the screen, only how it is handled
    ' by the WaitForString, WaitForStrings, and ReadString methods associated
    ' with the Screen object.
    objTab.Screen.IgnoreEscape = True
    objTab.Screen.Synchronous = True
   
    If Not objTab.Session.Connected then
        crt.Dialog.MessageBox _
            "Not Connected.  Please connect before running this script."
        exit sub
    end if

    Dim szCommand, szPrompt, nRow, szLogFileName, nIndex

    ' If this script is run as a login script, there will likely be data
    ' arriving from the remote system.  This is one way of detecting when it's
    ' safe to start sending data. If this script isn't being run as a login
    ' script, then the worst it will do is seemingly pause for one second
    ' before determining what the prompt is.
    ' If you plan on supplying login information by waiting for username and
    ' password prompts within this script, do so right before this do..loop.
    Do
        bCursorMoved = objTab.Screen.WaitForCursor(1)
    Loop until bCursorMoved = False
    ' Once the cursor has stopped moving for about a second, we'll
    ' assume it's safe to start interacting with the remote system.
    
    ' Get the shell prompt so that we can know what to look for when
    ' determining if the command is completed. Won't work if the prompt
    ' is dynamic (e.g. changes according to current working folder, etc)
    nRow = objTab.Screen.CurrentRow
    szPrompt = objTab.screen.Get(nRow, _
                                 0, _
                                 nRow, _
                                 objTab.Screen.CurrentColumn - 1)
    szPrompt = Trim(szPrompt)

    Dim szLogFile
    nIndex = 0
    Do
        szCommand = Trim(h_vCommands(nIndex))


        ' If the command is empty, then we should be done issuing commands
        ' (there's nothing else in our command array h_vCommands)
        if szCommand = "" then Exit Do

        ' Set up the log file for this specific command
        szLogFile = Replace(g_szLogFile, "__NUM__", NN(nIndex + 1, 2))
        
        ' Store the path for our first log file for later use (see end of this
        ' function...
        if g_szFirstLogFilePath = "" then g_szFirstLogFilePath = szLogFile

        ' Send the command text to the remote
        objTab.Screen.Send szCommand & vbcr

        ' Wait for the command to be echo'd back to us.
        objTab.Screen.WaitForString vbcr, 1
        objTab.Screen.WaitForString vblf, 1        

        Dim szResult
        ' 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.
        szResult = objTab.Screen.ReadString(szPrompt)
        
        Dim objFile
        Set objFile = g_fso.OpenTextFile(szLogFile, ForWriting, True)

        ' If you don't want the command logged along with the results, comment
        ' out the very next line
        objFile.WriteLine "Results of command: " & szCommand

        ' Write out the results of the command to our log file
        objFile.WriteLine szResult
        
        ' Close the log file
        objFile.Close

        ' Move on to the next command in our command array h_vCommands
        nIndex = nIndex + 1
    Loop
    
    ' Once we're complete, let's bring up Windows Explorer with the first of
    ' the log files selected.
    g_shell.Run "explorer /e,/select,""" & g_szFirstLogFilePath & """"

End Sub



Function GetMyDocumentsFolder()
    Dim myShell
    Set myShell = CreateObject("WScript.Shell")

    GetMyDocumentsFolder = myShell.SpecialFolders("MyDocuments")
End Function



'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub LogOutputOfSpecificCommandUseReadStringDUP()
    ' Instruct WaitForString and ReadString to ignore escape sequences when
    ' detecting and capturing data received from the remote (this doesn't
    ' affect the way the data is displayed to the screen, only how it is handled
    ' by the WaitForString, WaitForStrings, and ReadString methods associated
    ' with the Screen object.
    objTab.Screen.IgnoreEscape = True
    objTab.Screen.Synchronous = True
   
    If Not objTab.Session.Connected then
        crt.Dialog.MessageBox _
            "Not Connected.  Please connect before running this script."
        exit sub
    end if

    Dim szCommand, szPrompt, nRow, szLogFileName, nIndex

    ' If this script is run as a login script, there will likely be data
    ' arriving from the remote system.  This is one way of detecting when it's
    ' safe to start sending data. If this script isn't being run as a login
    ' script, then the worst it will do is seemingly pause for one second
    ' before determining what the prompt is.
    ' If you plan on supplying login information by waiting for username and
    ' password prompts within this script, do so right before this do..loop.
    Do
        bCursorMoved = objTab.Screen.WaitForCursor(1)
    Loop until bCursorMoved = False
    ' Once the cursor has stopped moving for about a second, we'll
    ' assume it's safe to start interacting with the remote system.
    
    ' Get the shell prompt so that we can know what to look for when
    ' determining if the command is completed. Won't work if the prompt
    ' is dynamic (e.g. changes according to current working folder, etc)
    nRow = objTab.Screen.CurrentRow
    szPrompt = objTab.screen.Get(nRow, _
                                 0, _
                                 nRow, _
                                 objTab.Screen.CurrentColumn - 1)
    szPrompt = Trim(szPrompt)

    Dim szLogFile
    nIndex = 0
    Do
        szCommand = Trim(I_vCommands(nIndex))


        ' If the command is empty, then we should be done issuing commands
        ' (there's nothing else in our command array I_vCommands)
        if szCommand = "" then Exit Do

        ' Set up the log file for this specific command
        szLogFile = Replace(g_szLogFile, "__NUM__", NN(nIndex + 1, 2))
        
        ' Store the path for our first log file for later use (see end of this
        ' function...
        if g_szFirstLogFilePath = "" then g_szFirstLogFilePath = szLogFile

        ' Send the command text to the remote
        objTab.Screen.Send szCommand & vbcr

        ' Wait for the command to be echo'd back to us.
        objTab.Screen.WaitForString vbcr, 1
        objTab.Screen.WaitForString vblf, 1        

        Dim szResult
        ' 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.
        szResult = objTab.Screen.ReadString(szPrompt)
        
        Dim objFile
        Set objFile = g_fso.OpenTextFile(szLogFile, ForWriting, True)

        ' If you don't want the command logged along with the results, comment
        ' out the very next line
        objFile.WriteLine "Results of command: " & szCommand

        ' Write out the results of the command to our log file
        objFile.WriteLine szResult
        
        ' Close the log file
        objFile.Close

        ' Move on to the next command in our command array I_vCommands
        nIndex = nIndex + 1
    Loop
    
    ' Once we're complete, let's bring up Windows Explorer with the first of
    ' the log files selected.
    g_shell.Run "explorer /e,/select,""" & g_szFirstLogFilePath & """"

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function NN(nNumber, nDesiredDigits)
' Normalizes a single digit number to have nDesiredDigits 0s in front of it
    Dim nIndex, nOffbyDigits, szResult
    nOffbyDigits = nDesiredDigits - len(nNumber)

    szResult = nNumber

    For nIndex = 1 to nOffByDigits
        szResult = "0" & szResult
    Next
    NN = szResult
End Function

Open in new window

0
Comment
Question by:cpatte7372
2 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 39281370
cause Duplex is the 4th string passed to WaitForStrings not the 3rd one, so u should add case 4:
select case result
	case 1
'handle "changed state to up"
		LogOutputOfSpecificCommandUseReadStringUP
	case 2
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadStringDOWN
	case 3
'handle "Duplicate address"
		LogOutputOfSpecificCommandUseReadStringDUP
case 4
'handle "duplex"
LogOutputOfSpecificCommandUseReadStringDUP
	end select

Open in new window

0
 

Author Closing Comment

by:cpatte7372
ID: 39281418
Thanks again mate....
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question