0
votes

I am using secureCRT to connect to a Linux server. SecureCRT reads VBScript, and I am new to this language so my problem might sound easy for you.

When I connect to the server from secureCRT, I a script containing a command let's say "date" ,the output of the command must come out on a text file on my local host ( windows ) and not on the server.

This is the script that I am using:

# $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
' used 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.
  '
  Set file = fso.OpenTextFile("C:\Users\elieme\Desktop\TTX1.txt", ForWriting, True)

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

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

  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, 45 )

      ' 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

End Sub

I read the script several time to understand it, and tried to manipulate it for hours, and asking for help was my last resort.

In the script I have crt.Screen.Send "date" & Chr(10) which will send my command and execute it. Then when I go in the loop, I don't understand what does this mean.

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

What do they mean by if we saw the prompt? Is it something that i have to input for it to exit ? Because i tried several keys and I'm always stuck in this while loop.

I tried to manipulate the script but every time I was either getting an empty file as output, or wrong data in the file.

Is there anyway to make this script execute only the command that I sent ("date"), and output what this command will do on the text file ?

If not, is there any shortcut to stop the script without having to go in the menu and selecting cancel script?

Thank you

EDIT:

I fixed it guys, very easy.

The rt.Screen.WaitForStrings takes as a second parameter a timeout number, so that fixed everything.

Thanks

1

1 Answers

0
votes

Fixed

The rt.Screen.WaitForStrings takes as a second parameter a timeout number, so that fixed everything.