securecrt中script脚本使用心得在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有较大的不同,对于界面的支持性较差。不过也有几个对话性的函数
、inputbox :提示用户输入参数
temp = inputbox(提示用户你输入参数的名称,对话框的名称) :需要将输入的参数赋值给某一个参数进行使用。
、输出函数 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
http://www.bkjia.com/phpjc/1124519.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1124519.htmltecharticlesecurecrt中script脚本使用心得 在securecrt中使用vbscript脚本,确实能够提高我们的工作效率,并且可以实现完全的自动化。 securecrt给我们提供了...