如何使用VBScript将命令提示符输出到日志文件

时间:2012-05-08 17:53:11

标签: vbscript

我不是程序员所以我不想过分激怒这个论坛中的优秀人才。我的问题是我想使用VBScript远程登录到Linux设备,发出 DF 命令并将所有响应输出到我稍后可以解析的日志文件。我最初找到了成功Telnet的方法,但我一直在试验文本文件输出要求没有成功。以下代码肯定不起作用,但我想知道我是否接近正确的方法?

Dim WshShell, oExec  
Set WshShell = CreateObject("WScript.Shell")  
Set oExec = WshShell.Exec("cmd /c dir")

WshShell.run"cmd" '*** open command window ***  
WScript.Sleep 250  

WshShell.SendKeys("{Enter}")  
WshShell.SendKeys"telnet 10.13.2.2"  
WshShell.SendKeys("{Enter}")  
WScript.Sleep 2000  

WshShell.SendKeys"root"  
WshShell.SendKeys("{Enter}")  
WScript.Sleep 1500  

WshShell.SendKeys"password"  
WshShell.SendKeys("{Enter}")  
WScript.Sleep 1500  

Set objFSO  = CreateObject("Scripting.FileSystemObject")  
Set objLogFile = objFSO.OpenTextFile("C:\VBSmemSize.txt", 2, True)  

WshShell.SendKeys"df /mnt/cf"  
WshShell.SendKeys("{Enter}")  
Do  
  strFromProc = oExec.Stdout.Readline()  
  WScript.Echo strFromProc  
Loop While Not objLogFile.StdOut.atEndOfStream  

1 个答案:

答案 0 :(得分:0)

您可以捕获外部命令的输出,但不能像使用sendkeys那样与它们进行交互。这里有一个有效的例子

Function ExecPing(strTarget)
  Set objShell = CreateObject("WScript.Shell")
  Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
  strPingResults = LCase(objExec.StdOut.ReadAll)
  If InStr(strPingResults, "antwoord van") Then '"reply from" in E
    WScript.Echo VbCrLf & strTarget & " responded to ping."
    ExecPing = True
  Else
    WScript.Echo VbCrLf & strTarget & " did not respond to ping."
    ExecPing = False
  End If
End Function

ExecPing pcname