在SecureCRT中使用VBSCRIPT腳本,確實能夠提高我們的工作效率,並且可以實現完全的自動化。
SecureCRT給我們提供了很好的平台——腳本工具制作和運行。下面就SecureCRT工具常用到的幾個函數闡述如下:
1. 在SecureCRT裡,用得最多的應該就是crt.Screen,基本上很多操作都是基於屏幕的返回字來決定下一步該作何操作:
(1):crt.Screen.WaitForString("KeyString",timewaiting)
該函數是單字符串判斷,KeyString是需要查找的關鍵字,timewaiting是一個超時閥值,例如:crt.Screen.WaitForString("people:",5)該行代碼的意思就是在5秒內沒有檢測到people:出現,就執行下一條語句,如果改成:crt.Screen.WaitForString("people:")那就是指直到people:出現才執行下一行代碼。
WaitForString是有返回值的,返回值是True 或者 False。因此,可以根據返回值進行條件判斷以確定一下條代碼。例如:
If (crt.Screen.WaitForString ("current state : UP",1)<>False) Then
portStatus="PortUP"
Else
portStatus="PortDown"
End If
msgbox portStatus
這段代碼用於判斷端口狀態情況並記錄下來.
(2):crt.Screen.WaitForStrings("KeyString1","KeyString2",...,timeout)
用於多個字符串的判斷,timeout的作用是一樣的。例如:
crt.Screen.WaitForStrings("cisco","huawei","H3C",5)
意思就是在5秒內有檢測到相應的字符時,返回相應的索引號(索引號是從1開始的)。如果都沒有檢查到,則返回0.因此,該函數的使用可以如下:
Dim SwitchKey
SwitchKey=crt.Screen.WaitForStrings("cisco","huawei","H3C",5)
Select case SwitchKey
case 1
MsgBox "思科設備"
case 2
MsgBox "華為設備"
case 3
MsgBox "華三設備"
case else
MsgBox "未知設備"
End Select
(3) 其實SecureCRT支持的腳本語言就是VBS,這個腳本語言與VB有較大的不同,對於界面的支持性較差。不過也有幾個對話性的函數
<1>、InputBox :提示用戶輸入參數
temp = inputbox("提示用戶你輸入參數的名稱","對話框的名稱") :需要將輸入的參數賦值給某一個參數進行使用。
<2>、輸出函數 msgbox
msgbox “給用戶輸出的信息對話框”
eg.求正方形面積的腳本
dim r,s
r=inputbox("請輸入正方形的邊長:","求正方形面積的程序")
s=r*r
msgbox(s)
-------------------------------------------------------------------------------------------
語句結構:
1. 順序執行的腳本,舉個網上泛濫的例子,那個自動登錄系統的例子,稍加修改如下。
# $language = "VBScript"
# $interface = "1.0"
Sub Main
'連接主機192.168.0.2
crt.session.Connect("/telnet 192.168.0.2")
'等待出現登陸用戶名提示login,等待時間是10s
crt.screen.WaitForString "login:",10
'輸入用戶名,回車
crt.screen.send "minico" & Chr(13)
'等待出現登陸密碼提示login,等待時間是10s
crt.screen.WaitForString "Password:",10
'輸入密碼,回車
crt.screen.send "123456"
crt.screen.send Chr(13)
End Sub
2. 選擇結構的腳本
if ... then ...else...結構和case結構見基礎知識舉例
3. 循環結構
腳本實例
#=====================================================
# $language = "VBScript"
# $interface = "1.0"
'============================================================================================='
' 程序名稱:AIX.VBS
' 程序說明:AIX主機系統配置/巡檢腳本
' 作者:鄭繼東
' 完成時間:2008-5-7
'============================================================================================='
'============================================================================================='
' 程序全局變量區
'============================================================================================='
dim ip
'============================================================================================='
' 程序全局常量區
'============================================================================================='
' button parameter options
Const ICON_STOP = 16 ' display the ERROR/STOP icon.
Const ICON_QUESTION = 32 ' display the '?' icon
Const ICON_WARN = 48 ' display a '!' icon.
Const ICON_INFO= 64 ' displays "info" icon.
Const BUTTON_OK = 0 ' OK button only
Const BUTTON_CANCEL = 1 ' OK and Cancel buttons
Const BUTTON_ABORTRETRYIGNORE = 2 ' Abort, Retry, and Ignore buttons
Const BUTTON_YESNOCANCEL = 3 ' Yes, No, and Cancel buttons
Const BUTTON_YESNO = 4 ' Yes and No buttons
Const BUTTON_RETRYCANCEL = 5 ' Retry and Cancel buttons
Const DEFBUTTON1 = 0 ' First button is default
Const DEFBUTTON2 = 256 ' Second button is default
Const DEFBUTTON3 = 512 ' Third button is default
' Possible MessageBox() return values
Const IDOK = 1 ' OK button clicked
Const IDCANCEL = 2 ' Cancel button clicked
Const IDABORT = 3 ' Abort button clicked
Const IDRETRY = 4 ' Retry button clicked
Const IDIGNORE = 5 ' Ignore button clicked
Const IDYES = 6 ' Yes button clicked
Const IDNO = 7 ' No button clicked
'============================================================================================='
' 程序輔助函數區
'============================================================================================='
'登陸函數
Function login
'定義IP地址,登陸用戶名,密碼變量
dim passwd
dim username
Dim result
Dim flag
flag =1
'斷開主機連接
crt.session.Disconnect
'開啟對話框,取得IP地址,登陸用戶名稱,密碼等變量
ip = crt.Dialog.Prompt("請輸入服務器IP地址:", "AIX", "192.1.1.207", false)
If (Trim(ip) = "") Or (ip = IDABORT) Then
result = crt.Dialog.MessageBox("您沒有輸入登陸的IP地址,CRT將被退出!", "提示信息",ICON_INFO)
crt.quit
End If
flag =1
While flag = 1
username = crt.Dialog.Prompt("請輸入登陸用戶名:", "AIX", "root", false)
If username = IDABORT Then
result = crt.Dialog.MessageBox("您選擇了沒有輸入用戶名稱,CRT將被推出!", "提示信息",ICON_INFO)
crt.quit
End If
If (Trim(username) = "")Then
result = crt.Dialog.MessageBox("請輸入登陸用戶名稱!", "提示信息",ICON_INFO)
Else
flag = 0
End If
wend
passwd = crt.Dialog.Prompt("請輸入登陸用戶密碼:", "AIX", "congine", true)
'連接主機
crt.screen.Synchronous = true
crt.session.Connect("/telnet " & ip)
'等待出現登陸用戶名提示login,等待時間是10s
crt.screen.WaitForString "login:"
'輸入用戶名,回車
crt.screen.send username & chr(13)
'等待出現登陸密碼提示login,等待時間是10s
crt.screen.WaitForString "Password:"
'輸入密碼,回車
crt.screen.send passwd & chr(13)
If crt.screen.WaitForString("invalid login name or password", 3) = True Then
result = crt.Dialog.MessageBox("服務器登陸失敗,請檢查IP地址、用戶名、密碼是否輸入正確!", "提示信息",ICON_INFO)
crt.quit
End If
crt.screen.Synchronous = false
End Function
'記錄當前會話日志函數
Function writelog
Dim result
Dim logfilename
Dim flag
flag =1
While flag =1
logfilename = crt.Dialog.Prompt("請輸入本次會話LOG文件位置", "AIX", "c:\" & ip &".log", false)
If Trim(logfilename) = "" Or (logfilename = IDABORT) then
result = crt.Dialog.MessageBox("強烈建議保存會話日志", "提示信息",ICON_INFO)
Else
flag = 0
End if
wend
crt.session.LogFileName = logfilename
crt.session.Log(true)
End Function
Function setline
crt.screen.send chr(13) & chr(13)
' crt.Sleep 1000
End Function
Function setcommand(cmdstr, sec)
setline
sec = sec * 1000
crt.screen.send cmdstr & Chr(13)
crt.Sleep sec
End Function
'取得服務器基本信息
Function get_machinfo
'主機基本信息
setcommand "hostname",1
setcommand "prtconf |grep 'Machine Serial Number'",6
'主機設備情況
setcommand "lsdev -C |grep proc",2
setcommand "lsattr -El mem0",2
setcommand "lsdev -Cc disk",2
setcommand "lsdev -Cc adapter",2
setcommand "lsdev -Cc tape",2
'主機網卡情況
setcommand "ifconfig -a",2
setcommand "more /etc/hosts",2
'主機軟件信息
setcommand "uname -a ",2
setcommand "oslevel -s",5
setcommand "instfix -i |grep ML",10
'主機卷組信息
setcommand "lsvg ",2
setcommand "lsvg -o",2
setcommand "lsvg -l rootvg",2
'主機文件系統信息
setcommand "df -g ",2
'主機日志信息
setcommand "errpt ",2
setcommand "errpt -a",2
setcommand "sysdumpdev -l ",2
'主機系統性能
setcommand "lsps -a",2
setcommand "vmstat 2 10",25
setcommand "iostat 2 10",25
End Function
'============================================================================================='
' 程序主函數(main)區
'============================================================================================='
'主函數
Sub Main
Dim result
' crt.screen.Synchronous = true
'系統登陸
login
'寫日志
writelog
'取得服務器信息
get_machinfo
result = crt.Dialog.MessageBox("信息收集完畢,是否推出CRT?", "提示信息", ICON_QUESTION Or BUTTON_YESNO Or DEFBUTTON2)
If result = IDYES Then
crt.quit
End If
'結束會話日志
crt.session.Log(false)
' crt.screen.Synchronous = false
End Sub