Solved

Combine vb scripts

Posted on 2013-06-16
67
301 Views
Last Modified: 2013-06-20
Hello Experts

I'm currently working with an expert to help resolve a scripting issue, however I have another issue I would like to start a discussion with.

I would like someone to show me how to combine two scripts.

In the first called number1 a message box appears when the statement 'changed state to down' appears on my router.

The second script logs to an output file of specific commands.

I would like to either combine the second or have the second script activated when the first script is activated due to the statement 'changed state to down' appearing on my router.

Is that possible?

Cheers
number1.vbs
LogOutputOfSpecificCommand-UseRe.vbs
0
Comment
Question by:cpatte7372
  • 44
  • 23
67 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
where in number1.vbs, you would like to call the 2nd script?
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi sedgwick,

Right after'


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

Cheers mate
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

You still around?

Cheers
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
yes, checking your script
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
here the updated number1.vbs:
# $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


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 objShell
Set objShell = Wscript.CreateObject("WScript.Shell")

objShell.Run "LogOutputOfSpecificCommand-UseRe.vbs" 

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I'm getting the following error message:

runtime error

Error: Object required: 'Wscript'

Line 84
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
try 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


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 objShell
Set objShell = CreateObject("WScript.Shell")

objShell.Run "LogOutputOfSpecificCommand-UseRe.vbs" 

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick

I'm not getting the error messsage

Scripting error

Error: Unknown error

Line 86
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
make sure u have the full path to the LogOutputOfSpecificCommand-UseRe.vbs
0
 

Author Comment

by:cpatte7372
Comment Utility
hi sedgwick,

How can I tell the path to LogOutputOfSpecificCommand-UseRe.vbs ?

Cheers
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
u should know where u put the script in your file system.
0
 

Author Comment

by:cpatte7372
Comment Utility
well the script is definitely in my file system..
0
 

Author Comment

by:cpatte7372
Comment Utility
I think I've found the problem.

The file name is 'LogOutputOfSpecificCommand-UseReadString.vbs'

I will replace LogOutputOfSpecificCommand-UseRe.vbs with  'LogOutputOfSpecificCommand-UseReadString.vbs' and see if that works ..
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
the file name was wrong u say?
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

Nearly there.

The problem is the log file. The system32 folder appears, as opposed to MyDocuments folder.

I should mention I'm running Windows Server 2008, and there isn't a MyDocuments folder. I tried changing it to just Documents but the log file still didn't appear.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
change the function to:
Function GetMyDocumentsFolder()
dim WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

End Function

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick

I changed the folder to the following:

GetMyDocumentsFolder = myShell.SpecialFolders("Scripts")

But when the script activates the command "show ip int brief", doesn't appear to be sent to the device neither are the commands sent to the log folder 'Scripts'
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I made the changes you suggested and the system32 folder still appears....
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

Here is the script with amendments you suggested:

#$language = "VBScript"
#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to the remote
'   machine.  The results of each command are captured into a variable, and then
'   written to an individual log file (one log file for each command).  Once all
'   the commands have been run, Windows explorer is launched, with the first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific commands
'   to separate files (one file for each command) without having to manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write data to a
'   file.  Instead, we use the ReadString() method to capture the result of each
'   command and write it manually to a file of our choosing (g_szLogFile serves
'   as a template for the file name which changes with each command that is
'   issued to the remote.  For example, the results of the first command will be
'   written to a file named Command#01_Results.txt, the results of the 2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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
Is it because I'm running Windows Server 2008?
0
 

Author Comment

by:cpatte7372
Comment Utility
ahhhh, Sedgwick,

If I run the script 'LogOutputOfSpecificCommand-UseReadString.vbs' by itself (without calling it) it works fine.

It just doesn't seem to work when its called from number1 script
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick

Just so you know, running the LogOutputOfSpecificCommand-UseReadString.vbs directly creates file called Command#01_Results.txt and within the file the following is displayed:


Results of command: show ip int brief
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            unassigned      YES unset  administratively down down    
FastEthernet0/1            unassigned      YES unset  administratively down down    
FastEthernet1/0            unassigned      YES unset  administratively down down    

Which is exactly what I would like to see when called from number1

Cheers mate.
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick

Are you still around to help me out, mate?
0
 

Author Comment

by:cpatte7372
Comment Utility
Is there any other experts that can help me out while sedgwick is away?

Cheers
0
 

Author Comment

by:cpatte7372
Comment Utility
Hello Experts,

I tried recompiling the script but now I'm getting an error on line 86

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


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 objShell
Set objShell = CreateObject("WScript.Shell")

objShell.Run "LogOutputOfSpecificCommand-UseReadString.vbs" 

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
The script that is called is giving me the following error message from 'Window Script Host'

Line: 1
Char: 1
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript Compilation error



#$language = "VBScript"
#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to the remote
'   machine.  The results of each command are captured into a variable, and then
'   written to an individual log file (one log file for each command).  Once all
'   the commands have been run, Windows explorer is launched, with the first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific commands
'   to separate files (one file for each command) without having to manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write data to a
'   file.  Instead, we use the ReadString() method to capture the result of each
'   command and write it manually to a file of our choosing (g_szLogFile serves
'   as a template for the file name which changes with each command that is
'   issued to the remote.  For example, the results of the first command will be
'   written to a file named Command#01_Results.txt, the results of the 2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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
Now the error has gone back to

Unknown error on line 86

objShell.Run "LogOutputOfSpecificCommand-UseReadString.vbs" on number 1 script
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
put the full path of the script not just the file name.
for example:

objShell.Run "c:\scripts\vbs\LogOutputOfSpecificCommand-UseReadString.vbs"
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I have put both scripts in c:\ and I'm still getting the error message, please see attached.

Below is the failing script:

#$language = "VBScript"
#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to the remote
'   machine.  The results of each command are captured into a variable, and then
'   written to an individual log file (one log file for each command).  Once all
'   the commands have been run, Windows explorer is launched, with the first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific commands
'   to separate files (one file for each command) without having to manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write data to a
'   file.  Instead, we use the ReadString() method to capture the result of each
'   command and write it manually to a file of our choosing (g_szLogFile serves
'   as a template for the file name which changes with each command that is
'   issued to the remote.  For example, the results of the first command will be
'   written to a file named Command#01_Results.txt, the results of the 2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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


Your help is greatly appreciated.

Cheers

Carlton
scripterror.png
0
 

Author Comment

by:cpatte7372
Comment Utility
OK Sedgwick,

I'm no longer getting the error message at line 86.

Please see attached for error message that I'm getting on LogOutputOfSpecificCommand-UseReadString.vbs

Below is the script that is generating the error:

#$language = "VBScript"
#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to the remote
'   machine.  The results of each command are captured into a variable, and then
'   written to an individual log file (one log file for each command).  Once all
'   the commands have been run, Windows explorer is launched, with the first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific commands
'   to separate files (one file for each command) without having to manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write data to a
'   file.  Instead, we use the ReadString() method to capture the result of each
'   command and write it manually to a file of our choosing (g_szLogFile serves
'   as a template for the file name which changes with each command that is
'   issued to the remote.  For example, the results of the first command will be
'   written to a file named Command#01_Results.txt, the results of the 2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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 mate.
scripterror2.png
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
comment the first and second line and try again.
'#$language = "VBScript"
'#$interface = "1.0"

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
sedgwick

made the change, but I'm now getting the following error, see attached.
scripterror3.png
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
do u want to have multiple tabs, each tab running a script?
0
IT, Stop Being Called Into Every Meeting

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

 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

not at this stage

Shall I comment it out?
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

When I say 'not at this stage', I mean to say I don't need multiple tabs, each tab running a script at this stage'

I just want to see the script working...

Cheers
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
nop.

u need to run the second script like this:
	Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /T /S " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Open in new window


where strSession is the same session u used to connect.
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I inserted the amendment, however its better if I don't have it automatically run on every tab.

So would I need to do the following instead so that it only runs on the tab that I'm actively in?

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Cheers

BTW, really appreciate your continued assistance.
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I definitely don't want multiple tabs.

Can you help me?

Cheers
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I think if the amendment doesn't multiple tabs then it might be perfect..... I've tried but no luck
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
Ill get to the office and will check
0
 

Author Comment

by:cpatte7372
Comment Utility
Thanks Sedgwick
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
i dropped the /t for suppress new tab:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I'm not getting the error message as shown in the attached
scripterror4.png
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
you are not getting the error message?
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I don't understand.

When I run the script I get the error message I showed you in attachment 'scripterror4'
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
u posted: "I'm not getting the error message as shown in the attached"
0
 

Author Comment

by:cpatte7372
Comment Utility
Oops!

I meant to say I 'am'  getting the error message.....
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
did u updated the strSession variable?
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I'm not sure what you mean.

I applied the following:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

The script either opens another session or opens another tab.

All I want is for the first script to call LogOutputOfSpecificCommand-UseReadString.vbs and apply the commands to the existing connection/session.

Regards
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
strSession is the currernt session used in the 1st vb script, so use the same session name.
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

First, I wanna say thanks for you continued patience in helping me resolved this issue.

I have these lines in the script:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Open in new window


However, when I run the script number a completely new tab opens up, please see attached. However, I need the script to run on the existing tab.
19-06-2013-08-13-51.png
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
does it open new tab in both cases?
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Open in new window

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S /T " & chr(34) & strSession & chr(34) & _
                 " /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"

Open in new window

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I will try both cases when I get to office in 30 mins.

Cheers
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I hope you're still around.

Anyway, with both scripts I get the error shown in the attached.
scripterror5.png
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
i really don't know why u keep getting this error.
can u try run it as a single script, meaning copy LogOutputOfSpecificCommand-UseReadString.vbs content to the location in the 1st vbs.
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick

do you mean 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


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

	'#$language = "VBScript"
'#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to 

the remote
'   machine.  The results of each command are captured into a variable, 

and then
'   written to an individual log file (one log file for each command).  

Once all
'   the commands have been run, Windows explorer is launched, with the 

first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific 

commands
'   to separate files (one file for each command) without having to 

manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write 

data to a
'   file.  Instead, we use the ReadString() method to capture the result 

of each
'   command and write it manually to a file of our choosing (g_szLogFile 

serves
'   as a template for the file name which changes with each command that 

is
'   issued to the remote.  For example, the results of the first command 

will be
'   written to a file named Command#01_Results.txt, the results of the 

2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft

\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
yes, instead of calling the 2nd script, embed the vbs content inside the 1st one.
0
 

Author Comment

by:cpatte7372
Comment Utility
Sedgwick,

I'm not getting error message shown
scripterror6.png
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

The error is now changed to the shown
scripterror7.png
0
 

Author Comment

by:cpatte7372
Comment Utility
The script is as follows:

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

' ASSUMPTION: file being 'tailed' contains information
'             similar to:
'   Jan 5 18:26:13 device01 9927: Jan 5 18:26:12: %LINK-3-UPDOWN: 

Interface POS5/2, changed state to down
'   Jan 5 18:26:47 device02 6332: Jan 5 18:26:46.243: %LINK-3-UPDOWN: 

Interface GigabitEthernet3/1, changed state to down
'   Jan 5 18:28:40 device01 9937: Jan 5 18:28:39: %LINK-3-UPDOWN: 

Interface POS5/2, changed state to down


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

	'#$language = "VBScript"
'#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to 

the remote
'   machine.  The results of each command are captured into a variable, 

and then
'   written to an individual log file (one log file for each command).  

Once all
'   the commands have been run, Windows explorer is launched, with the 

first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific 

commands
'   to separate files (one file for each command) without having to 

manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write 

data to a
'   file.  Instead, we use the ReadString() method to capture the result 

of each
'   command and write it manually to a file of our choosing (g_szLogFile 

serves
'   as a template for the file name which changes with each command that 

is
'   issued to the remote.  For example, the results of the first command 

will be
'   written to a file named Command#01_Results.txt, the results of the 

2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft

\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window

scripterror7.png
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
here i combine them for you:
# $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) = "pwd"
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

0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

I have fixed all the errors with the comments:

The combined script now looks 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


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

	'#$language = "VBScript"
'#$interface = "1.0"
' LogOutputOfSpecificCommand-UseReadString.vbs
'
' Description:
'   Sends commands one by one as listed in the g_vCommands() array to 

the remote
'   machine.  The results of each command are captured into a variable, and then
'   written to an individual log file (one log file for each command).  

Once all
'   the commands have been run, Windows explorer is launched, with the first
'   command output file selected within the explorer window.
'
' Demonstrates:
'   This example script demonstrates how to log the output of specific commands
'   to separate files (one file for each command) without having to manually
'   turn logging on before and off after running each command.  
' 
'   This specific example doesn't use the logging script API to write data to a
'   file.  Instead, we use the ReadString() method to capture the result of each
'   command and write it manually to a file of our choosing (g_szLogFile serves
'   as a template for the file name which changes with each command that is
'   issued to the remote.  For example, the results of the first command will be
'   written to a file named Command#01_Results.txt, the results of the 2nd
'   command to Command#02_Results.txt, etc...).
'
'   Specifically, this example automates the logging process by:
'    - Using an array of commands that are to be sent to the
'      remote system (g_vCommands)
'    - Using objTab.Screen.Send() to issue each command.
'    - Using the objTab.Screen.ReadString() method to not only
'      wait for an indication that the command sent has been
'      completed, but also capture all of the text received
'      while the command was running.
'
' Note: This script assumes that SecureCRT is currently connected to
'       the remote device prior to running this script.  Otherwise, the
'       script will exit with an error message.
'
'       If you want this script to perform the connection
'       sequence as well, simply replace the "If Not
'       objTab.Session.Connected" block within the Main() sub
'       below with a line similar to the following:
'
'           objTab.Session.Connect "/S MySessionName"

' Option Explicit

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()
    ' 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
    crt.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 WshShell
 Set WshShell = CreateObject("Wscript.Shell")
GetMyDocumentsFolder  = WshShell.RegRead("HKCU\Software\Microsoft

\Windows\CurrentVersion\Explorer\Shell Folders\Personal")

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

' Using Set is mandatory
Set objShell = Nothing

   '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

Open in new window


However, I'm getting the following error message:
scripterror8.png
0
 

Author Comment

by:cpatte7372
Comment Utility
Hi Sedgwick,

You still with me mate?
0
 

Author Comment

by:cpatte7372
Comment Utility
I fixed the error in scripterror8, I'm now getting a real error saying syntax error at line 170

Sub Main()
0
 

Author Closing Comment

by:cpatte7372
Comment Utility
Do you deserve more that 500 points. I wish I could buy you a drink.

This working like a dream. .

I might need to tweak it sometime in the future, but right now its working beautfully.

I want to thank you for you patience and perseverance.

You've been great!
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
awesome, glad its working for you.
good luck ;-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Problem Description:   Couple of months ago we upgraded the ADSL line at our branch office from Home to Business line. The purpose of transforming the service to have static public IP’s. We were in need for public IP’s to publish our web resour…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now