VBScript - 输出命令到文件

时间:2014-08-05 08:56:27

标签: linux vbscript

我正在使用secureCRT连接到Linux服务器。 SecureCRT读取VBScript,我是这种语言的新手,所以我的问题听起来可能很简单。

当我从secureCRT连接到服务器时,我是一个包含命令的脚本,例如“date”,该命令的输出必须出现在我本地主机(windows)上的文本文件中而不是服务器上。< / p>

这是我正在使用的脚本:

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

我多次阅读脚本来理解它,并试图操纵它几个小时,寻求帮助是我最后的手段。

在脚本中我有crt.Screen.Send "date" & Chr(10),它将发送我的命令并执行它。然后,当我进入循环时,我不明白这是什么意思。

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

如果我们看到提示,他们是什么意思?这是我必须输入它退出的东西吗?因为我尝试了几个键而且我总是在这个循环中停留。

我试图操作脚本,但每次我要么输出一个空文件作为输出,要么是文件中的错误数据。

有没有让这个脚本只执行我发送的命令(“date”),并输出这个命令对文本文件的作用?

如果没有,是否有任何快捷方式可以停止脚本而无需进入菜单并选择取消脚本?

谢谢

编辑:

我修好了,非常容易。

rt.Screen.WaitForStrings将超时数作为第二个参数,以便修复所有内容。

由于

1 个答案:

答案 0 :(得分:0)

固定

rt.Screen.WaitForStrings将超时数作为第二个参数,以便修复所有内容。