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

如何使用VBScript将命令提示符输出到日志文件,vbscript,Vbscript,我不是一个程序员,所以我不想过分激怒这个论坛的优秀人士。我的问题是,我想使用VBScript将Telnet连接到Linux设备,发出DF命令,并将所有响应输出到日志文件,我可以稍后解析该文件。我最初发现了一种成功实现Telnet的方法,但我一直在试验文本文件输出要求,但没有成功。下面的代码当然不起作用,但我想知道我是否接近正确的方法 Dim WshShell, oExec Set WshShell = CreateObject("WScript.Shell") Set oExec = W

我不是一个程序员,所以我不想过分激怒这个论坛的优秀人士。我的问题是,我想使用VBScript将Telnet连接到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  

您可以捕获外部命令的输出,但不能像使用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

使用
ssh
批处理可能比使用
telnet
更成功。如果你还没有设置,我警告你,这不是小事,但完全值得。