Solved

VB Script Type Mismatch Error

Posted on 2013-06-23
20
451 Views
Last Modified: 2013-08-06
Hello Experts,

Can someone please tell me how to fix the Type Mismatch error at line 168?

# $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_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

  ' 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."

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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .
	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 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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Comment
Question by:cpatte7372
  • 11
  • 9
20 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39269028
what's the value of nIndex when the error occur?
0
 

Author Comment

by:cpatte7372
ID: 39269038
Hi Sedgwick,

I'm not entirely sure I know what you mean?

Actually, the script is the one you helped me create. I'm trying to tweak it so that I can run a different of commands when I different trigger occurs.

If you remember, the trigger 'changed state to down' would execute a number of commands. I'm trying to tweak the script so that a different set of commands would be triggered if ,say a statement appeared on a device that I was connected to said 'elvis is alive'.

Would you like to see the original script as a recap?

Cheers
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39269123
each index in g_vCommands reflect a command.
so when the error occur, what is the value of nIndex?
does it happen in the first entry to the loop when nIndex = 0?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cpatte7372
ID: 39269144
Hi Sedgwick,

I'm not entirely sure what you mean - the full error is shown in the attachment
scripterror6.png
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39269154
replace this line:
 szCommand = Trim(g_vCommands(nIndex))

Open in new window

with this line:
dim nextCmd 
nextCmd = g_vCommands(nIndex)
wscrip.echo nIndex & " " &nextCmd
 szCommand = Trim(nextCmd)

Open in new window

what is the output to the screen?
0
 

Author Comment

by:cpatte7372
ID: 39269863
Hello Sedgwick,

Sorry for the delayed response - I had to run into office.

Anyway, I'm getting the error shown in the attachment.

# $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_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

  ' 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."

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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .
	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 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
        	dim nextCmd 
		nextCmd = g_vCommands(nIndex)
		wscrip.echo nIndex & " " &nextCmd
 		szCommand = Trim(nextCmd)

        ' 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

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

scripterror7.png
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270425
now i understand, you declared the g_vCommands inside other sub, so it is not accessible to other subs
u need to declare it globally.
if u look at the script i posted, i moved the g_vCommands  declaration outside any sub (above Sub Main).
now it can be read by any sub in the 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_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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .

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
  

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

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


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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: 39270545
Hi Sedgwick,

Thanks for responding, I'm now getting the following error message, see attachment.
scripterror8.png
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270551
here:
# $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_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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .

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

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
  

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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: 39270554
Hi Sedgwick,

I'm not sure how the script will run a different set of commands if say instead of 'changed state to down' was echo'd back 'elvis is alive' echo'd back?

Cheers
0
 

Author Comment

by:cpatte7372
ID: 39270556
Hi Sedgwick,

Thanks for such a speedy response.

I'm now getting the following error, please see attachment
scripterror9.png
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270570
here:
# $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_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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .

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
  

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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: 39270587
Hello mate,

Now getting the following error...
scripterror10.png
scripterror10.png
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39270594
# $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"
g_vCommands(2) = "show run"
g_vCommands(3) = "show ip ospf"
g_vCommands(4) = "show ip bgp"
g_vCommands(5) = "show ip ospf neighbor"
g_vCommands(6) = "show int fas 0/0"
g_vCommands(7) = "show ldp neighbor"
g_vCommands(8) = "show run int tunn 0"
g_vCommands(9) = "show run int fas 0/1"
' .
' .
' .

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
  

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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: 39270648
OK, Sedgwick, this is looking good.

So, what if I wanted to change the array/variable/parameter to send the commands if the message 'elvis is alive' (of course I'm being silly with the elvis thing, just want to emphasise to point).

I would like another set of commands to be executed.

If you just give me a guide on how it can be accomplished I'll try and do it myself - I just mindful that you've helped me sooooooo much and I appreciate it mate.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270910
i'd use external file which lists all commands and have the vb script read them and loop them, so you won't need to change the script in case new/different commands are required.
0
 

Author Comment

by:cpatte7372
ID: 39272254
Hi Sedgwick,

Can you show me how to that?

Cheers
0
 

Author Comment

by:cpatte7372
ID: 39324743
I've requested that this question be closed as follows:

Accepted answer: 0 points for cpatte7372's comment #a39269038

for the following reason:

Cheers
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39324744
@cpatte7372

sorry for missing your post, i'd really like to help you finish the script if it's still relevant.
if u can re-open the question i'll post you the the answer as required.

cheers
0
 

Author Closing Comment

by:cpatte7372
ID: 39384944
Brilliant.

Cheers mate.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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