Solved

VB Script Activation Problem

Posted on 2013-06-24
45
236 Views
Last Modified: 2013-06-27
Hello Experts,

Can someone please tell me why the script will work for

CaptureJustTheLinesWeCareAbout

but won't work for:

CaptureJustTheLinesWeCareAbout2

# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"


' .
' .
' .

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
    CaptureJustTheLinesWeCareAbout2
  ' 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
  

    crt.Screen.WaitForString "changed state to down"

    ' 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


	LogOutputOfSpecificCommandUseReadString
	
   '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 CaptureJustTheLinesWeCareAbout2()
  MsgBox "Capturing just the lines we care about"
  do
    ' Look for specific output
  

    crt.Screen.WaitForString "changed state to up"

    ' 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


	LogOutputOfSpecificCommandUseReadString2
   '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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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


Cheers

P.S.

If you can think of a more elegant / streamlined way of achieving the same result without so much code that would be great also.

Cheers
0
Comment
Question by:cpatte7372
  • 25
  • 18
  • 2
45 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270908
what is not working?
0
 

Author Comment

by:cpatte7372
ID: 39271380
Hi Sedgwick

Taking a look at line 55 to 61 the Example Uses WaitForString to look for just the text I 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.(I actually don't know what Split() builtin VBScript is.

If the text 'changed state to down' appears then

Sub CaptureJustTheLinesWeCareAbout() is invoked,

However, if the text 'changed state to up' appears

Sub CaptureJustTheLinesWeCareAbout2(), isn't invoked.

Does that make sense?

Cheers

Carlton
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39271424
in the post line 55 to 61 has alot of commented lines besides 2 calls to CaptureJustTheLinesWeCareAbout and CaptureJustTheLinesWeCareAbout2.
are we watching the same script?

btw, split function splits the string to string array based on given delimiter.
for example:
dim words = split("changed state to down"," ")
the 2nd parameter is the delimiter, in this case is a space.
so u ended up having an array with 4 words:
changed
state
to
down
0
 

Author Comment

by:cpatte7372
ID: 39271464
Hi Sedgwick,
Yes we're watching the same script. I talked about lines 55 to 61 to try and make myself understood - failed at that :-)


Anyhow, the problem is 'calls' to 'CaptureJustTheLinesWeCareAbout' work but calls to 'CaptureJustTheLinesWeCareAbout2', don't work
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39271487
found the bug, replace line 323 from
 szCommand = Trim(h_vCommands(nIndex))

Open in new window


to
 szCommand = Trim(g_vCommands(nIndex))

Open in new window

0
 

Author Comment

by:cpatte7372
ID: 39271524
Hi Sedgwick,

Going to try that now, but I'm not sure that is the problem .  uhmmmm
0
 

Author Comment

by:cpatte7372
ID: 39271552
Hi Sedgwick,

Just as I thought.

The change resulted in the text 'change state to down' to call Sub CaptureJustTheLinesWeCareAbout2()

whereas 'change state to down' should call CaptureJustTheLinesWeCareAbout() and 'changed state to up' should call CaptureJustTheLinesWeCareAbout2()
0
 

Author Comment

by:cpatte7372
ID: 39271560
Sedgwick

Is the problem that I have to Dims'

Dim g_vCommands(100)
g_vCommands(0) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39271575
i fixed it:
# $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

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
    CaptureJustTheLinesWeCareAbout2
  ' 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
  

    crt.Screen.WaitForString "changed state to down"

    ' 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


	LogOutputOfSpecificCommandUseReadString
	
   '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 CaptureJustTheLinesWeCareAbout2()
  MsgBox "Capturing just the lines we care about"
  do
    ' Look for specific output
  

    crt.Screen.WaitForString "changed state to up"

    ' 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


	LogOutputOfSpecificCommandUseReadString2
   '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 LogOutputOfSpecificCommandUseReadString()

	Dim g_vCommands(100)
	g_vCommands(0) = "show ip int brief"
	g_vCommands(1) = "show cdp neighbor"


    ' 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 LogOutputOfSpecificCommandUseReadString2()


	Dim h_vCommands(100)
	h_vCommands(0) = "term len 0"
	h_vCommands(1) = "show run"


    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 

Author Comment

by:cpatte7372
ID: 39271654
Hi Sedgwick,

I wish you had, but the text 'changed state to down' still calls CaptureJustTheLinesWeCareAbout2(), whereas the nothing happens with the text 'changed state to up'
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39271798
the script is posted simply calls the 2 Subs one after the other.
i can't see any usage of the split() function which determines which sub is called based on which command.
0
 

Author Comment

by:cpatte7372
ID: 39271816
Hi Sedgwick

Thats a shame, because if you can't figure out I know I wont be able to. ;-(
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39271847
don't give up.
after lines 87 and 135 add debug line:
wscript.echo vElements

Open in new window

what do u get?
0
 

Author Comment

by:cpatte7372
ID: 39271855
Hi Sedgwick,

Going add it now.

I won't give up if you don't
0
 

Author Comment

by:cpatte7372
ID: 39271868
Sedgwick,

I get the attached:
scripterror11.png
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39272738
Try changing the suggested line:

wscript.echo vElements

to:

MsgBox vElements

~bp
0
 

Author Comment

by:cpatte7372
ID: 39274244
Hi billprew,

Thanks for responding.

After trying your suggestion I get the following error shown in the attachment

Cheers
scripterror10.png
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39274414
Okay, makes sense, it's an array, I wasn't looking at the code other than that single line before.  Try this:

MsgBox Join(vElements, "|")

~bp
0
 

Author Comment

by:cpatte7372
ID: 39274456
Hi billprew,

That is working, however we appear to be at the same place we have been.

Let me try to explain'

When the following text appears 'changed state to down' the script calls CaptureJustTheLinesWeCareAbout().

However, when the text 'changed state to up' appears the script won't call CaptureJustTheLinesWeCareAbout2()


The Expert that has been currently working on this has tried very hard, but it appears to be a problem to get the script to call CaptureJustTheLinesWeCareAbout2()

Cheers
0
 

Author Comment

by:cpatte7372
ID: 39274478
Billprew,

The following Msgbox appears when CaptureJustTheLinesWeCareAbout() is called, see attachment..
scripterror101.png
0
 

Author Comment

by:cpatte7372
ID: 39275990
Sedgwick

Have you given up?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39276044
nop, i've been crazy busy at work had no time to even review the comments.
try something else, instead of using separate crt.Screen.WaitForString for "changed state to up" and "changed state to down", try use
Dim result = crt.Screen.WaitForStrings("changed state to up","changed state to down"):
# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"


' .
' .
' .

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 = crt.Screen.WaitForStrings("changed state to up","changed state to down")
	

    ' 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
		LogOutputOfSpecificCommandUseReadString
	case 2
		LogOutputOfSpecificCommandUseReadString2
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:cpatte7372
ID: 39276278
Hi Sedgwick,

I'm pleased you're sticking with me on this.

Anyway, I getting the follow error, see attached
scripterror12.png
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277017
CAn u post the script?
Line 91 in my post refer to a comment and not the discussed function.
0
 

Author Comment

by:cpatte7372
ID: 39277320
Hi Sedgwick,

I'm using the following script:

# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"


' .
' .
' .

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 = crt.Screen.WaitForStrings("changed state to up","changed state to down")
	

    ' 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
		LogOutputOfSpecificCommandUseReadString
	case 2
		LogOutputOfSpecificCommandUseReadString2
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277341
i change the function call to accept array, try now:

# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"


' .
' .
' .

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 vWaitFors 
	vWaitFors = Array("changed state to up","changed state to down")

    dim result = crt.Screen.WaitForStrings(vWaitFors, 10)
	

    ' 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
		LogOutputOfSpecificCommandUseReadString
	case 2
		LogOutputOfSpecificCommandUseReadString2
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 

Author Comment

by:cpatte7372
ID: 39277349
Hi Sedgwick,

Thanks again for responding. I'm not getting the following error, see attached.
scripterror12.png
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39277421
i can't believe i fall for that, stupid mistake:
# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"


' .
' .
' .

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")
	

    ' 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
		LogOutputOfSpecificCommandUseReadString
	case 2
		LogOutputOfSpecificCommandUseReadString2
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 

Author Comment

by:cpatte7372
ID: 39277514
Sedgwick,

It appears to be working. I don't want to get to excited just yet. I want to carry out a few tests, but at the moment a sitting here with a big smile on my face :-)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277648
\w/
0
 

Author Comment

by:cpatte7372
ID: 39277814
This is beautiful. And just to think I was about to cancel my membership because I thought I could get help from other sites - nothing to do with you - I just thought I wasn't getting value for my money.

You're a star.

So, if I want to add another case would I do the following when the text "%LINK-5-CHANGED" appears:

# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"
h_vCommands(2) = "show ip bgp"


' .
' .
' .

Dim I_vCommands(100)
I_vCommands(0) = "term len 0"
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","%LINK-5-CHANGED")
	

    ' 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
		LogOutputOfSpecificCommandUseReadString
	case 2
		LogOutputOfSpecificCommandUseReadString2
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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 LogOutputOfSpecificCommandUseReadString3()
    ' 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
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277828
yes, but check line 130.
u need to update the select case if u add more expressions for WaitForStrings() function:

select case result
	case 1 
'handle "changed state to up"
		LogOutputOfSpecificCommandUseReadString
	case 2 
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadString2
case 3 
'handle "%LINK-5-CHANGED" 
	end select

Open in new window

0
 

Author Comment

by:cpatte7372
ID: 39277890
Sedgwick,

Don't I need to also add

LogOutputOfSpecificCommandUseReadString3
0
 

Author Comment

by:cpatte7372
ID: 39277976
Hi Sedgwick,

I amended the script as you suggested, however it didn't work:

# $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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"
h_vCommands(2) = "show ip bgp"


' .
' .
' .

Dim I_vCommands(100)
I_vCommands(0) = "term len 0"
I_vCommands(1) = "show ip traffic"
I_vCommands(2) = "show ip pim"


' .
' .
' .

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","%LINK-5-CHANGED","Duplicate address")
	

    ' 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"
		LogOutputOfSpecificCommandUseReadString
	case 2 
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadString2
	case 3 
'handle "%LINK-5-CHANGED"
		LogOutputOfSpecificCommandUseReadString3
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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 LogOutputOfSpecificCommandUseReadString3()
    ' 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 GetMyDocumentsFolder()
    Dim myShell
    Set myShell = CreateObject("WScript.Shell")

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

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

















'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277978
yes of course.
it would be better to name the function a meanningful name:

for changed state to up:
LogOutputOfSpecificCommandChangedStateToUp

and so on.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277986
can u be more specific, what didn't work?
0
 

Author Comment

by:cpatte7372
ID: 39278039
Hi Sedgwick,

I changed the following lines:

select case result
	case 1 
'handle "changed state to up"
		LogOutputOfSpecificCommandUseReadString
	case 2 
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadString2
	case 3 
'handle "Duplicate address"
		LogOutputOfSpecificCommandUseReadString3
	end select

Open in new window




and I created the following:

Sub LogOutputOfSpecificCommandUseReadString3()
    ' 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

Open in new window

0
 

Author Comment

by:cpatte7372
ID: 39278069
So Sedgwick, when the text Duplicate address appears,

the following should be run:


Dim I_vCommands(100)
I_vCommands(0) = "term len 0"
I_vCommands(1) = "show ip traffic"
I_vCommands(2) = "show ip pim"

Open in new window


The full code 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) = "show ip int brief"
g_vCommands(1) = "show cdp neighbor"


' .
' .
' .

Dim h_vCommands(100)
h_vCommands(0) = "term len 0"
h_vCommands(1) = "show run"
h_vCommands(2) = "show ip bgp"


' .
' .
' .

Dim I_vCommands(100)
I_vCommands(0) = "term len 0"
I_vCommands(1) = "show ip traffic"
I_vCommands(2) = "show ip pim"


' .
' .
' .

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","%LINK-5-CHANGED","Duplicate address")
	

    ' 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"
		LogOutputOfSpecificCommandUseReadString
	case 2 
'handle "changed state to down"
		LogOutputOfSpecificCommandUseReadString2
	case 3 
'handle "Duplicate address"
		LogOutputOfSpecificCommandUseReadString3
	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 LogOutputOfSpecificCommandUseReadString()
    ' 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 LogOutputOfSpecificCommandUseReadString2()
    ' 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


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub LogOutputOfSpecificCommandUseReadString3()
    ' 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 GetMyDocumentsFolder()
    Dim myShell
    Set myShell = CreateObject("WScript.Shell")

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

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

















'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
 

Author Comment

by:cpatte7372
ID: 39278124
Hi Sedgwick,

I think I can take care of it from here.....

You've been brilliant.

What country are you in?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39278142
Israel ;-)
0
 

Author Comment

by:cpatte7372
ID: 39278425
Wow!

If ever you come to London, or I go to Israel, I'll buy you a drink :-)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39278525
U got it.
Cheers mate
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39279313
Dont forget to close the question, good luck mate.
0
 

Author Closing Comment

by:cpatte7372
ID: 39280535
I would like to take a moment to express my eternal gratitude to this Expert. Not just for his impeccable skills but for his dogged determination to see the problem through to the end.

It is Experts like Sedgwick that will make customers remain customers.

I wish I was in a position to award Sedgwick more than 500 points as he deserves every point awarded to him.

As long as there are experts like Sedgwick working for EE I will always pay my subscription fee.

Thank you very much Sedgwick

Cheers

Carlton
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39280557
thanks again for your kind words.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now