2012-05-08 50 views
1

我不是程序員,所以我不想過分激怒本論壇中的好人。我的問題是,我想使用VBScript Telnet到Linux設備上,發出一個DF命令,並將所有響應輸出到日誌文件,我可以稍後解析。我最初發現了一種成功實現Telnet的方法,但是我一直在試驗文本文件輸出要求沒有成功。下面的代碼當然不起作用,但我想知道我是否接近正確的方法?如何使用VBScript將命令提示符輸出到日誌文件

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 
+0

你可能比'telnet'使用'ssh'成功分批。如果你還沒有設置這個,我警告你這不是微不足道的,但完全值得。 –

回答

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 
相關問題