Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-09-2008, 02:59 AM
hifox1 hifox1 is offline
Registered User
 
Join Date: May 2008
Posts: 3
Vbscript to launch/open output text file

Hi Support,

May I know is there any script available to allow an user to click a dialog box button to launch/open the output text file? For example, an user run the script to capture some screen output and successfully stored the output contents into a text file. Then, there is a dialog box asking whether to view the file.

Thanks.

Regards,
Ken
Reply With Quote
  #2  
Old 06-09-2008, 07:43 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi hifox1,

Such an action as prompting the user for whether they would like to open the text file or not can probably be accomplished.

Could you post the script you are currently using so I can have an idea on how this would be integrated into your script?
As these are public forums, it may be a good idea to make sure any Usernames/Passwords/IP Addresses stored in the script are replaced with non-harmful information.
Reply With Quote
  #3  
Old 06-09-2008, 08:39 AM
hifox1 hifox1 is offline
Registered User
 
Join Date: May 2008
Posts: 3
Below is the sample script. At the end of the script, there is a prompt to alert the user that the testing is completed. How do I give the user an option to view the result (output text file)?

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

' This script demonstrates how to capture line by line output
' from a command sent to a server. It then saves each line of output
' to a file. This script shows how the 'WaitForStrings' command can be
' use to wait for multiple possible outputs.

' Constants used by OpenTextFile()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub Main

crt.Screen.Synchronous = True

' Create an instance of the scripting filesystem runtime so we can
' manipulate files.
'
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")

' Open a file for writing. The last True parameter causes the file
' to be created if it doesn't exist.
'

filename = crt.Dialog.Prompt("Filename:", "Filename", "", False)
filepath = "c:\" & filename & ".txt"
Set file = fso.OpenTextFile(filepath, ForWriting, True)

' Send the initial command then throw out the first linefeed that we
' see by waiting for it.
'
crt.Screen.Send "a.out" & Chr(10)
crt.Screen.WaitForString Chr(10)

' Create an array of strings to wait for.
'
Dim waitStrs
waitStrs = Array( Chr(10), "#" )

Dim row, screenrow, readline, items
row = 1

Do
While True

' Wait for the linefeed at the end of each line, or the shell prompt
' that indicates we're done.
'
result = crt.Screen.WaitForStrings( waitStrs )

' If we saw the prompt, we're done.
If result = 2 Then
Exit Do
End If

' The result was 1 (we got a linefeed, indicating that we received
' another line of of output). Fetch current row number of the
' cursor and read the first 20 characters from the screen on that row.
'
' This shows how the 'Get' function can be used to read line-oriented
' output from a command, Subtract 1 from the currentRow to since the
' linefeed moved currentRow down by one.
'
screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 20 )

' NOTE: We read 20 characters from the screen 'readline' may contain
' trailing whitespace if the data was less than 20 characters wide.

' Write the line out with an appended '\r\n'
file.Write readline & vbCrLf
Wend
Loop

crt.screen.synchronous = false
crt.Dialog.MessageBox "Testing is completed!"

End Sub
Reply With Quote
  #4  
Old 06-09-2008, 09:31 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi hifox1,

The following lines of code will need to be added to the beginning of your script for the second block of example code I will provide to work:

Code:
Dim objShell

Set objShell = CreateObject("WScript.Shell")

Next, you should be able to replace your 'crt.Dialog.Messagebox "Testing is completed!"' line with the following:

Code:
If MsgBox("Would you like to open the text file?", vbYesNo) = vbYes then
	objShell.Run("notepad " & filepath)
End If

Using this information, are you able to develop a solution to accomplish what it is you are looking to do?
Reply With Quote
  #5  
Old 06-09-2008, 09:22 PM
hifox1 hifox1 is offline
Registered User
 
Join Date: May 2008
Posts: 3
Hi Kevin,

Yes.....it works pretty well. Thanks so much for your help.

Regards,
Ken
Reply With Quote
  #6  
Old 06-10-2008, 08:01 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi Ken,

Thanks for the update.

I am glad the information I provided was helpful.
Reply With Quote
  #7  
Old 10-11-2016, 03:56 PM
corp-mule corp-mule is offline
Registered User
 
Join Date: Mar 2006
Posts: 21

This script is interesting. I'm thinking I might be able to use it. So, I modified it the way the thread says to do. I'm hoping I got it right.

But it seems to prompt me if I want to read the file, before I've entered any commands to produce some output for the file.

Am I missing something?

Reply With Quote
  #8  
Old 10-12-2016, 08:19 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi corp-mule,

Are you being prompted to read it?

Or to create the file?

Because the first crt.Dialog.Prompt() in the script involves creating the file:

Quote:
filename = crt.Dialog.Prompt("Filename:", "Filename", "", False)
filepath = "c:\" & filename & ".txt"
Set file = fso.OpenTextFile(filepath, ForWriting, True)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #9  
Old 10-12-2016, 03:02 PM
corp-mule corp-mule is offline
Registered User
 
Join Date: Mar 2006
Posts: 21

First it asks me what I want to name the file.

Then it does this ...

router#a.out
Translating "a.out"

Translating "a.out"
% Unknown command or computer name, or unable to find computer address
router#

And then it asks me if I want to open the file.


Reply With Quote
  #10  
Old 10-12-2016, 03:09 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi corp-mule,

So it sounds like the script is executing exactly as designed.

This line is where the command is being sent:

Quote:
crt.Screen.Send "a.out" & Chr(10)
What are you trying to accomplish?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #11  
Old 10-12-2016, 04:17 PM
corp-mule corp-mule is offline
Registered User
 
Join Date: Mar 2006
Posts: 21


I'm trying to clear the screen and the buffer, then run some commands and capture the output into a file.

Maybe this script won't do what I think it will do. I'm just guessing at this stuff. Trial and error.

...

So, If I replace that "send" command with my own, it should run the command before asking me if I want to open the file?

I tried that and it did not work that way.

See below ...

I've put "my stuff" where you see it below, and I've also put it after the "while true." I get the same results. It captures the "t" of the second "send" command and that's all the file contains.


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

' Can't get this script to work right.
' Waiting for a reply on the thread,
' in the forum.

'Vbscript to launch/open output text file
'https://forums.vandyke.com/showthread.php?t=2979

'Below is the sample script.
'
'At the end of the script, there
'is a prompt to alert the user
'that the testing is completed.
'
'How do I give the user an option
'to view the result (output text file)?
'

' This script demonstrates how to
' capture line by line output from a
' command sent to a server. It then
' saves each line of output to a file.
' This script shows how the
' 'WaitForStrings' command can be use to
' wait for multiple possible outputs.

' The following lines of code will need
' to be added to the beginning of your
' script for the second block of example
' code I will provide to work:

' Code:
'
' Dim objShell
' Set objShell = CreateObject("WScript.Shell")

' Next, you should be able to replace
' your 'crt.Dialog.Messagebox "Testing
' is completed!"' line with the
' following:

' Code:
'
' If MsgBox("Would you like to open the text file?", vbYesNo) = vbYes then
' objShell.Run("notepad " & filepath)
' End If


' Constants used by OpenTextFile()
'
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Sub Main

crt.Screen.Synchronous = True

Dim objShell
Set objShell = CreateObject("WScript.Shell")

' Create an instance of the
' scripting filesystem runtime
' so we can manipulate files.
'
'
' Original code:
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")

' Open a file for writing. The last True
' parameter causes the file to be
' created if it doesn't exist.
'
filename = crt.Dialog.Prompt("Filename:", "Filename", "", False)
filepath = "c:\" & filename & ".txt"
Set file = fso.OpenTextFile(filepath, ForWriting, True)

' Send the initial command then throw
' out the first linefeed that we see by
' waiting for it.
'
'crt.Screen.Send "a.out" & Chr(10)
'crt.Screen.WaitForString Chr(10)


' Adding my stuff here

CRT.Screen.SendSpecial "MENU_CLEAR_SCREEN_AND_SCROLLBACK"
'CRT.Screen.SendSpecial "MENU_CLEAR_SCROLLBACK"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "term len 0" & chr(13)
crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "sh clock" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "sh run" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "sh clock" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "term len 24" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)


crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "! You must copy and paste" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "! this output into a" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "! text file if" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "! you want to save it." & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

crt.Screen.WaitForStrings strPrompt, "#"
crt.Screen.Send "!" & chr(13)

' end of my stuff

' Create an array of
' strings to wait for.
'
Dim waitStrs
waitStrs = Array( Chr(10), "#" )

Dim row, screenrow, readline, items
row = 1

Do
While True
'


' Wait for the linefeed at the end of
' each line, or the shell prompt that
' indicates we're done.
'
result = crt.Screen.WaitForStrings( waitStrs )

' If we saw the prompt, we're done.
'
If result = 2 Then
Exit Do
End If

' The result was 1 (we got a linefeed,
' indicating that we received another
' line of of output). Fetch current row
' number of the cursor and read the
' first 20 characters from the screen on
' that row.
'
' This shows how the 'Get' function can
' be used to read line-oriented output
' from a command, Subtract 1 from the
' currentRow to since the linefeed moved
' currentRow down by one.
'
screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 20 )

' NOTE:
' We read 20 characters from the screen
' 'readline' may contain trailing
' whitespace if the data was less than
' 20 characters wide.

' Write the line out with an appended '\r\n'
'
file.Write readline & vbCrLf
Wend
Loop


' Original code:
'crt.screen.synchronous = false
'crt.Dialog.MessageBox "Testing is completed!"


If MsgBox("Would you like to open the text file?", vbYesNo) = vbYes then
objShell.Run("notepad " & filepath)
End If

End Sub



Reply With Quote
  #12  
Old 10-13-2016, 10:14 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi corp-mule,

I think it's safe to say if you take an example script and immediately comment out all the prior code and add your own that maybe that is not the example script for you.

I do not see where you have ever defined strPrompt.

As long as it is not sensitive data, please post the explicit steps you would follow to perform this task manually. It would also be helpful to have examples of the resulting output from the commands (either screenshots or snippets of the output). If it is sensitive data, please email support@vandyke.com and include "Attn Brenda - Forum Thread #2979" in the subject line.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #13  
Old 10-26-2016, 10:12 AM
gates2010 gates2010 is offline
Registered User
 
Join Date: Jun 2012
Posts: 19
Thumbs up

Here is an example as well for reference that is similar to what you requested, I use this everyday..

https://forums.vandyke.com/showthread.php?t=10539
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 01:59 PM.