SecureCRT 內置 VBScript 引擎,通過 `crt.Screen`、`crt.Session`、`crt.Dialog` 等對象模型,可實現網絡設備 (交換機/路由器/防火牆) 的自動化登錄、配置下發、巡檢信息采集、日志記錄。本文系統整理核心 API、語句結構並提供完整巡檢腳本模板。
| 對象 | 核心方法/屬性 | 用途 |
|---|---|---|
| crt.Screen | Synchronous = True/False | 同步模式 (逐字符等待回顯) / 異步。發送命令前設 True,結束設 False。 |
WaitForString(str, timeout) | 等待屏幕出現字符串,返回 Boolean。超時秒數可省略 (無限等待)。 | |
WaitForStrings(arr, timeout) | 多字符串等待,返回匹配索引 (1-based),超時/無匹配返回 0。配合 Select Case 分支。 | |
Send(str) | 發送字符串到設備。需手動加回車 `& Chr(13)` 或 `vbCr`。 | |
Get(row, col, row2, col2) | 讀取屏幕指定區域文本 (用於解析輸出)。 | |
| crt.Session | Connect("/telnet ip") / Disconnect | 建立/斷開連接。支持 SSH/Telnet/Serial 協議參數。 |
LogFileName = "path" / Log(True/False) | 啟動/停止會話日志記錄。 | |
| crt.Dialog | Prompt(msg, title, default, isPassword) | 彈窗輸入框 (密碼模式可選),返回字符串或空/取消。 |
MessageBox(msg, title, buttons+icon) | 彈窗提示/確認,返回按鈕常量 (IDOK, IDYES...)。 | |
| crt.Sleep(ms) | 腳本級睡眠 (毫秒),等待命令輸出緩沖。 |
功能:交互式輸入 IP/用戶/密碼 → 自動登錄 → 開啟日志 → 批量執行巡檢命令 (硬件、網絡、軟件、存儲、性能、日志) → 完成提示退出。
' ========== 配置常量 ==========
Const ICON_INFO = 64, BUTTON_YESNO = 4, DEFBUTTON2 = 256
Const IDYES = 6, IDNO = 7
' ========== 全局變量 ==========
Dim ip, username, password, logFile
' ========== 輔助函數 ==========
Function ReadName(raf) ' 讀取固定長度字符串並清理空字符
' ... 略 ...
End Function
Sub SendCmd(cmd, waitSec)
crt.Screen.Send cmd & vbCr
crt.Sleep waitSec * 1000
End Sub
' ========== 核心功能函數 ==========
Function Login()
crt.Session.Disconnect
ip = crt.Dialog.Prompt("請輸入服務器IP:", "AIX巡檢", "192.168.1.100", False)
If ip = "" Then crt.Quit
username = crt.Dialog.Prompt("用戶名:", "AIX巡檢", "root", False)
password = crt.Dialog.Prompt("密碼:", "AIX巡檢", "", True) ' 密碼模式
crt.Screen.Synchronous = True
crt.Session.Connect "/telnet " & ip
crt.Screen.WaitForString "login:"
crt.Screen.Send username & vbCr
crt.Screen.WaitForString "Password:"
crt.Screen.Send password & vbCr
If crt.Screen.WaitForString("invalid login", 3) Then
crt.Dialog.MessageBox "登錄失敗", "錯誤", ICON_INFO
crt.Quit
End If
crt.Screen.Synchronous = False
End Function
Function StartLog()
logFile = crt.Dialog.Prompt("日志保存路徑:", "AIX巡檢", "C:\logs\" & ip & ".log", False)
crt.Session.LogFileName = logFile
crt.Session.Log True
End Function
Sub CollectInfo()
' 系統基本信息
SendCmd "hostname", 1
SendCmd "prtconf | grep 'Machine Serial Number'", 6
' 硬件設備
SendCmd "lsdev -C | grep proc", 2
SendCmd "lsattr -El mem0", 2
SendCmd "lsdev -Cc disk", 2
SendCmd "lsdev -Cc adapter", 2
' 網絡
SendCmd "ifconfig -a", 2
SendCmd "more /etc/hosts", 2
' 軟件/補丁
SendCmd "uname -a", 2
SendCmd "oslevel -s", 5
SendCmd "instfix -i | grep ML", 10
' 存儲/文件系統
SendCmd "lsvg", 2
SendCmd "lsvg -o", 2
SendCmd "lsvg -l rootvg", 2
SendCmd "df -g", 2
' 日志/性能
SendCmd "errpt", 2
SendCmd "errpt -a", 2
SendCmd "sysdumpdev -l", 2
SendCmd "lsps -a", 2
SendCmd "vmstat 2 10", 25
SendCmd "iostat 2 10", 25
End Sub
' ========== 主入口 ==========
Sub Main
Call Login
Call StartLog
Call CollectInfo
Dim result
result = crt.Dialog.MessageBox("巡檢完成,是否退出 SecureCRT?", "完成", ICON_INFO + BUTTON_YESNO + DEFBUTTON2)
If result = IDYES Then crt.Quit
crt.Session.Log False
End Sub