1

我不是程序员,所以我不想过分激怒这个论坛里的好人。我的问题是我想使用 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  
4

1 回答 1

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
于 2012-06-01T08:08:50.637 回答