Solved

VB Script Modification Request

Posted on 2013-06-19
37
241 Views
Last Modified: 2014-01-27
Hello Experts,

I very clever Expert called Sedgwick help compile the script below.

At the moment if the following occurs changed state to down on a router the the script will run the following commands:

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

I would like to add another trigger like 'ospf neighbor down' to trigger another set of commands on the router like:

Dim g_vCommands(200)
g_vCommands(0) = "show ip eigrp"
g_vCommands(1) = "show ip dns"
g_vCommands(2) = "show ip dhcp server"
g_vCommands(3) = "show ip etc"
g_vCommands(4) = "show ip etc"
g_vCommands(5) = "show ip etc"

Basically, I would like different triggers to trigger a different set of commands.

As always, you're help is very much appreciated.


# $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."
&#9;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
  • 19
  • 13
  • 2
37 Comments
 

Author Comment

by:cpatte7372
Comment Utility
Can I get some help please?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
If you only have two sets of commands, I would implement it this way.  If you have multiple sets of commands, you should read a file of such commands and populate the dictionary from the file contents.

The If and For statements would replace your Do loop at line 161

Note: the Dim statement in the snippet below includes two of your script variables.
    Dim dicCommands, szPrompt, szCommand
    Set dicCommands = CreateObject("scripting.dictionary")
    dicCommands.Add "changed state to down", "show ip int brief^show cdp neighbor^show run^show ip ospf^show ip bgp^show ip ospf neighbor^show int fas 0/0^show ldp neighbor^show run int tunn 0^show run int fas 0/1"
    dicCommands.Add "ospf neighbor down", "show ip eigrp^show ip dns^show ip dhcp server^show ip etc^show ip etc^show ip etc"
    
    '...
    If dicCommands.exists(szPrompt) Then
        For Each szCommand In Split(dicCommands(szPrompt), "^")
            'do what you normally do with the command
            
        Next
    End If

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark,

Thanks for responding. Unfortunately, I have limited scripting experience. I wouldn't have a clue where to include the amendments you made into the script.

Can you guide me please?

Cheers
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
The For...Next structure replaces your Do...Loop structure at line 161

The other statements would normally appear somewhere before them, either in the general declarations area above Sub Main() or, for initialization statements, at the top of the Sub Main() routine.
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark,

going to give it a go now .... will let you know.

I hope it works
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark,

I totally don't know where to include the modifications into the script ... as I mentioned please understand my scripting abilities are seriously limited.

Cheer
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
Comment Utility
It would be something like this.
BEWARE: This code issues network commands and has been posted without testing.  Great caution should be taken when running this in a production environment.

# $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 dicCommands
    Set dicCommands = CreateObject("scripting.dictionary")
    dicCommands.Add "changed state to down", "show ip int brief^show cdp neighbor^show run^show ip ospf^show ip bgp^show ip ospf neighbor^show int fas 0/0^show ldp neighbor^show run int tunn 0^show run int fas 0/1"
    dicCommands.Add "ospf neighbor down", "show ip eigrp^show ip dns^show ip dhcp server^show ip etc^show ip etc^show ip etc"

' .
' .
' .

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

    If dicCommands.exists(szPrompt) Then
        For Each szCommand In Split(dicCommands(szPrompt), "^")
            

        ' 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

        Next
    End If
    
    ' 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
Comment Utility
aikimark,

Thanks again for responding, I'm now getting the following error shown in the attachment
scripterror9.png
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
that should be an Exit For statement instead of an Exit Do statement
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark, it didn't work.

None of the commands were run on the router.

All that happened was explorer opened up....
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
were the commands issued in your original script?
0
 

Author Comment

by:cpatte7372
Comment Utility
The original looked like this:


# $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) = "ls -lR --color /tmp"
'g_vCommands(2) = "ps -eaf | grep vshelld"
'g_vCommands(3) = "tail -100 /var/log/messages"
'g_vCommands(4) = "sh run"
'g_vCommands(5) = ""
'g_vCommands(6) = ""
'g_vCommands(7) = ""
'g_vCommands(8) = ""
'g_vCommands(9) = ""
' .
' .
' .

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


Thanks for your continued support
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
I asked if the commands were issued in the original script.  Did the original script actually work?
0
 

Author Comment

by:cpatte7372
Comment Utility
No. The command weren't in the original script.

Why do you ask?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
because I thought you were asking how to execute a different set of commands in addition to the set that you already had.
0
 

Author Comment

by:cpatte7372
Comment Utility
To clarify the following commands weren't in the original script:

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

Author Comment

by:cpatte7372
Comment Utility
Sorry for the confusion.

I wish to execute and different set of commands  - not in addition to the set that is already there
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:cpatte7372
Comment Utility
The original might be confusing because both sets of commands look similar. However, I would like event changed state to down to activate the following commands

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

I would like to add another trigger like 'ospf neighbor down' to execute a different set of commands like:

Dim g_vCommands(200)
g_vCommands(0) = "show ip eigrp"
g_vCommands(1) = "show ip dns"
g_vCommands(2) = "show ip dhcp server"
g_vCommands(3) = "show ip etc"
g_vCommands(4) = "show ip etc"
g_vCommands(5) = "show ip etc"
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Hopefully, someone will be able to test this on their system
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi  aikimark,

Aren't you able to assist further?

I appreciate your help anyway, but if you could continue looking at it I would be very grateful.

Cheers
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
I've done as much as I can.  I do not have anything that looks like your system in order to test things.
0
 

Author Comment

by:cpatte7372
Comment Utility
OK, thanks. Lets see if another Expert can pick it up...

Cheers mate
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
You were already given the answer by  aikimark, http://www.experts-exchange.com/Hardware/Networking_Hardware/Routers/Q_28162093.html#a39262110  

You can read up on looping http://www.w3schools.com/vbscript/vbscript_looping.asp

Your answer here http://www.experts-exchange.com/Hardware/Networking_Hardware/Routers/Q_28162093.html#a39262126 should be more detailed then "it didn't work".    It probably did but now you just have another error in another place.  

Keep trouble shooting.  Look at your error messages and follow them.  If you are not sure what it is, look it up on http://www.w3schools.com/vbscript/default.asp or google it.  A little frustration means your learning.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@cpatte7372

A Google search (vbscript router commands) shows many hits.  However, I can not advocate for one approach/solution over another, since I do not have a router with which I can test.  Moreover, you lack the experience that would allow me to make a suggestion and then have you implement and test it in your environment.  You need real hand-holding from someone that has sent commands to a router via VBScript.  You probably need someone that can remote into your desktop to see what you are doing or make a site visit.

You lack the programming experience to even understand your script in its current state, much less make changes to it.

Please understand that routers are very important things to a business and that any codes you send to a router, either manually or through a script, can lock up, break, or open up a router -- all of which would be very bad for your business.

===============
If I had properly understood the true nature of your problem, I would not have commented.  I only posted the best approach to supplying sequences of router commands, based on some string input (used as the name of the command sequence). I am still monitoring this question thread, but I'm ethically prevented from posting a suggestion that could hurt your company.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
I said virtually the same thing thing http://www.experts-exchange.com/Hardware/Networking_Hardware/Routers/Q_28163641.html#a39266565

I can only look at the code and only help with the logic. I don't know enough about routers to go beyond that.  

Two of us has suggested the same thing for you.
0
 

Author Comment

by:cpatte7372
Comment Utility
With due respect, both you guys are wrong in your approach to this problem.

All I ask for the script to be modified so that one event triggers a separate set of commands.

The script does exactly what I would like it to do, but only for one event.

So, at the moment if an event called 'changed state to down' were to occur on a device a set of commands would be executed.

I just need another event to execute another set of commands.

The fact that this occurs on a router is immaterial.

If you can't help further I understand but I fear your comments have discouraged in any other Experts from helping.

Regards
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@cpatte7372

If your current script does not issue any commands, which I think you've told me in an earlier comment, I am unable to advise you on how to fix that problem.  I have supplied code that DOES address multiple sets of commands, but I can not test your script in my environment.

If this script were issuing Net Use or IpConfig commands, I would be able to help you, since I can issue such commands in my environment.  You want to send commands to a router, which is a very special part of the network.
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark

What does 'Neglected' question mean?
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark,

Just so you're aware, the script is designed to respond to certain events. Following an event commands should be executed....

Cheers
0
 

Author Comment

by:cpatte7372
Comment Utility
aikimark,

You're script doesn't send any of the commands to the screen/router


dicCommands.Add "changed state to down", "show ip int brief^show cdp neighbor^show run^show ip ospf^show ip bgp^show ip ospf neighbor^show int fas 0/0^show ldp neighbor^show run int tunn 0^show run int fas 0/1"
    dicCommands.Add "ospf neighbor down", "show ip eigrp^show ip dns^show ip dhcp server^show ip etc^show ip etc^show ip etc"
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@cpatte7372

I know that my version of the script does not send any commands.  My version of the script shows you how to package sets of commands that can be issued, based on some name you seem to be scraping from a terminal screen.

Neglected Question is a mechanism that sends email alerts to experts in the associated zones for a question.
0
 

Author Comment

by:cpatte7372
Comment Utility
Aikimark
Thanks for responding

I honestly don't see how your script shows me how to package sets of  commands that can be issued?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Each .Add method of the dictionary object variable is a delimited string of commands.  The first parameter of the method is the name of the command.  I tried to use the string/name that you would gather from your earlier parsing.

A dictionary object is more flexible than the array object your code is currently using.
0
 

Author Closing Comment

by:cpatte7372
Comment Utility
Cheers
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

12 Experts available now in Live!

Get 1:1 Help Now