20130610

VBScript 自動化 Telnet 指令

要利用 VBScript 作自動化,首先要知道如何執行外部程式以及模擬鍵盤輸入
執行外部程式是利用 WScript.Shell 物件中的 run 方法
模擬鍵盤輸入是使用SendKeys方法

範例如下:

'建立 WScript.Shell 物件
set WshShell = WScript.CreateObject("WScript.Shell")
'開啟命令視窗
WshShell.run"cmd.exe"
WScript.Sleep 1500
'Send keys to active window; change the
' ip address as needed.
'WshShell.SendKeys"telnet IP address"
'Emulate the enter key
'WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'write the user name to the cmd window
WshShell.SendKeys"username"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'write the password to the cmd window
WshShell.SendKeys"password"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'change to sudo access
WshShell.SendKeys"change to sudo"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'Start SM
WshShell.SendKeys"run UNIX start script"
WshShell.SendKeys("{Enter}")
WScript.Sleep 20000
'exit sudo
WshShell.SendKeys"exit"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'exit the telnet session
WshShell.SendKeys"exit"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
'Exit the program
WshShell.SendKeys"% "

沒有留言:

張貼留言