cpatte7372
asked on
Combine vb scripts
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
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
where in number1.vbs, you would like to call the 2nd script?
ASKER
Hi sedgwick,
Right after'
MsgBox "Device """ & vElements(3) & """ just went down."
Cheers mate
Right after'
MsgBox "Device """ & vElements(3) & """ just went down."
Cheers mate
ASKER
Hi Sedgwick,
You still around?
Cheers
You still around?
Cheers
yes, checking your script
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
ASKER
Hi Sedgwick,
I'm getting the following error message:
runtime error
Error: Object required: 'Wscript'
Line 84
I'm getting the following error message:
runtime error
Error: Object required: 'Wscript'
Line 84
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
ASKER
Hi Sedgwick
I'm not getting the error messsage
Scripting error
Error: Unknown error
Line 86
I'm not getting the error messsage
Scripting error
Error: Unknown error
Line 86
make sure u have the full path to the LogOutputOfSpecificCommand -UseRe.vbs
ASKER
hi sedgwick,
How can I tell the path to LogOutputOfSpecificCommand -UseRe.vbs ?
Cheers
How can I tell the path to LogOutputOfSpecificCommand
Cheers
u should know where u put the script in your file system.
ASKER
well the script is definitely in my file system..
ASKER
I think I've found the problem.
The file name is 'LogOutputOfSpecificComman d-UseReadS tring.vbs'
I will replace LogOutputOfSpecificCommand -UseRe.vbs with 'LogOutputOfSpecificComman d-UseReadS tring.vbs' and see if that works ..
The file name is 'LogOutputOfSpecificComman
I will replace LogOutputOfSpecificCommand
the file name was wrong u say?
ASKER
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.
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.
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
ASKER
Hi Sedgwick
I changed the folder to the following:
GetMyDocumentsFolder = myShell.SpecialFolders("Sc ripts")
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'
I changed the folder to the following:
GetMyDocumentsFolder = myShell.SpecialFolders("Sc
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'
ASKER
Hi Sedgwick,
I made the changes you suggested and the system32 folder still appears....
I made the changes you suggested and the system32 folder still appears....
ASKER
Hi Sedgwick,
Here is the script with amendments you suggested:
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
ASKER
Is it because I'm running Windows Server 2008?
ASKER
ahhhh, Sedgwick,
If I run the script 'LogOutputOfSpecificComman d-UseReadS tring.vbs' by itself (without calling it) it works fine.
It just doesn't seem to work when its called from number1 script
If I run the script 'LogOutputOfSpecificComman
It just doesn't seem to work when its called from number1 script
ASKER
Hi Sedgwick
Just so you know, running the LogOutputOfSpecificCommand -UseReadSt ring.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.
Just so you know, running the LogOutputOfSpecificCommand
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.
ASKER
Hi Sedgwick
Are you still around to help me out, mate?
Are you still around to help me out, mate?
ASKER
Is there any other experts that can help me out while sedgwick is away?
Cheers
Cheers
ASKER
Hello Experts,
I tried recompiling the script but now I'm getting an error on line 86
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
ASKER
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
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
ASKER
Now the error has gone back to
Unknown error on line 86
objShell.Run "LogOutputOfSpecificComman d-UseReadS tring.vbs" on number 1 script
Unknown error on line 86
objShell.Run "LogOutputOfSpecificComman
put the full path of the script not just the file name.
for example:
objShell.Run "c:\scripts\vbs\LogOutputO fSpecificC ommand-Use ReadString .vbs"
for example:
objShell.Run "c:\scripts\vbs\LogOutputO
ASKER
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:
Your help is greatly appreciated.
Cheers
Carlton
scripterror.png
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
Your help is greatly appreciated.
Cheers
Carlton
scripterror.png
ASKER
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 -UseReadSt ring.vbs
Below is the script that is generating the error:
Thanks mate.
scripterror2.png
I'm no longer getting the error message at line 86.
Please see attached for error message that I'm getting on LogOutputOfSpecificCommand
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
Thanks mate.
scripterror2.png
comment the first and second line and try again.
'#$language = "VBScript"
'#$interface = "1.0"
ASKER
do u want to have multiple tabs, each tab running a script?
ASKER
Hi Sedgwick,
not at this stage
Shall I comment it out?
not at this stage
Shall I comment it out?
ASKER
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
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
nop.
u need to run the second script like this:
where strSession is the same session u used to connect.
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"
where strSession is the same session u used to connect.
ASKER
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.Shel l")
objShell.Run " /Script C:\LogOutputOfSpecificComm and-UseRea dString.vb s"
Cheers
BTW, really appreciate your continued assistance.
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.Shel
objShell.Run " /Script C:\LogOutputOfSpecificComm
Cheers
BTW, really appreciate your continued assistance.
ASKER
Hi Sedgwick,
I definitely don't want multiple tabs.
Can you help me?
Cheers
I definitely don't want multiple tabs.
Can you help me?
Cheers
ASKER
Hi Sedgwick,
I think if the amendment doesn't multiple tabs then it might be perfect..... I've tried but no luck
I think if the amendment doesn't multiple tabs then it might be perfect..... I've tried but no luck
Ill get to the office and will check
ASKER
Thanks Sedgwick
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"
ASKER
you are not getting the error message?
ASKER
Hi Sedgwick,
I don't understand.
When I run the script I get the error message I showed you in attachment 'scripterror4'
I don't understand.
When I run the script I get the error message I showed you in attachment 'scripterror4'
u posted: "I'm not getting the error message as shown in the attached"
ASKER
Oops!
I meant to say I 'am' getting the error message.....
I meant to say I 'am' getting the error message.....
did u updated the strSession variable?
ASKER
Hi Sedgwick,
I'm not sure what you mean.
I applied the following:
Dim objShell
Set objShell = CreateObject("WScript.Shel l")
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
" /Script C:\LogOutputOfSpecificComm and-UseRea dString.vb s"
I'm not sure what you mean.
I applied the following:
Dim objShell
Set objShell = CreateObject("WScript.Shel
objShell.Run "SecureCRT /S " & chr(34) & strSession & chr(34) & _
" /Script C:\LogOutputOfSpecificComm
ASKER
Hi Sedgwick,
The script either opens another session or opens another tab.
All I want is for the first script to call LogOutputOfSpecificCommand -UseReadSt ring.vbs and apply the commands to the existing connection/session.
Regards
The script either opens another session or opens another tab.
All I want is for the first script to call LogOutputOfSpecificCommand
Regards
strSession is the currernt session used in the 1st vb script, so use the same session name.
ASKER
Hi Sedgwick,
First, I wanna say thanks for you continued patience in helping me resolved this issue.
I have these lines in the script:
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
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"
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
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"
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "SecureCRT /S /T " & chr(34) & strSession & chr(34) & _
" /Script C:\LogOutputOfSpecificCommand-UseReadString.vbs"
ASKER
Hi Sedgwick,
I will try both cases when I get to office in 30 mins.
Cheers
I will try both cases when I get to office in 30 mins.
Cheers
ASKER
Hi Sedgwick,
I hope you're still around.
Anyway, with both scripts I get the error shown in the attached.
scripterror5.png
I hope you're still around.
Anyway, with both scripts I get the error shown in the attached.
scripterror5.png
i really don't know why u keep getting this error.
can u try run it as a single script, meaning copy LogOutputOfSpecificCommand -UseReadSt ring.vbs content to the location in the 1st vbs.
can u try run it as a single script, meaning copy LogOutputOfSpecificCommand
ASKER
Hi Sedgwick
do you mean like this?
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
yes, instead of calling the 2nd script, embed the vbs content inside the 1st one.
ASKER
ASKER
ASKER
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
scripterror7.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Sedgwick,
I have fixed all the errors with the comments:
The combined script now looks like this:
However, I'm getting the following error message:
scripterror8.png
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
However, I'm getting the following error message:
scripterror8.png
ASKER
Hi Sedgwick,
You still with me mate?
You still with me mate?
ASKER
I fixed the error in scripterror8, I'm now getting a real error saying syntax error at line 170
Sub Main()
Sub Main()
ASKER
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!
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!
awesome, glad its working for you.
good luck ;-)
good luck ;-)