|
开发环境搭建好了,就可以开始着手进行tcl/expect开发。expect是tcl的一个扩展包,主要用于模拟终端设备的人机交互,从而实现自动化配置。要用expect包,需要在脚本中加一条package require Expect,类似c里面的include.
基本的逻辑和语法也很简单,即
send "xxx\r"
expect "yyy"
用send命令,输入字符串xxx,\r是回车;用expect期待界面返回信息yyy.
下面给一个实际的例子:
login.tcl,一个通用的登录脚本过程,可以模拟用户以telnet的方式登录某种终端网络设备,并生成登录日志,使用方法Login IP。
#Common login model for Ethertone products #
# #
#Usage: LOGIN IP #
# #
#Fuctionogin Ethernetone products #
#Lastest update at 2009.9.23 #
#2009.9.23:rewrite title matching regexp #
###############################################################################
proc LOGIN {IP} {
package require Expect
##define login varible in array Login_arg
set Login_arg(command) "craft"
set Login_arg(user) "admin"
set Login_arg(pass) "password#1"
#set IP 192.168.24.86
#Declare global var:
global log_dir log_file id
global oem model ver label
global expect_out
#define log_dir ,"\\" means "\"
set log_dir "D:\\test_report"
### Create log file
#check log_dir to verify files exist or not ,if the file is not exist then ask user to create it or not ,
#if the file exist echo user and open the files set to write state and then to continue.
if {![file exists $log_dir]} {
file mkdir $log_dir
}
set log_file [open "$log_dir/[join [split [exp_timestamp -format %c] :] -].txt" "a+"]
puts "A log file will be stored in d:\\test_report \n"
#record test starting time
puts $log_file "Test is staring at [timestamp -format "%c"]"
#Login device
spawn telnet $IP
set id $spawn_id
expect -re "Welcome to pSOSystem..." {
send "\r"
} default {
puts "Error: Unable to login,please check network connection!"
puts $log_file "\n[timestamp -format "%X"] Login Error: Unable to login,please check the network connection!\r"
exit 2
}
expect -re "Telnet>|ER1006>|ER2212-telnet>"
send "$Login_arg(command)\r"
expect -re "Login:"
send "$Login_arg(user)\r"
expect -re "assword:"
send "$Login_arg(pass)\r"
expect -re "Enter selection:>" {
puts $log_file "\n[timestamp -format "%X"] Chassis $IP logined successfully.\n"
#what model,what version
} default { puts "\nLogin Error: Wrong user ID or password,please check it and try again.\n"
puts $log_file "\n[timestamp -format "%X"] Login Error:Wrong user ID or password,please check it and try again.\n"
exit 3
}
#get model and version
regexp {(xxxx NETWORKS|yyyy)\s+(\S+\s*\w*)\s+(\S+)\s+(\S+)} $expect_out(buffer) chassis_info oem model ver label
puts $log_file "Login chassischassis_info"
puts $log_file "OEM: $oem"
puts $log_file "MODEL: $model"
puts $log_file "VERSION: $ver"
puts $log_file "LABEL: $label"
puts $log_file "\n"
#close $log_file
#print a divide line
#divide 60
#End
}
涉及内容有点多,以后再另行说明。
login_test.tcl
#登录测试脚本
set dir ../common/
source $dir/login.tcl
#LOGIN 192.168.24.86
#LOGIN 192.168.24.68
LOGIN 192.168.24.219
#LOGIN 192.168.24.85
global delay log_dir log_file id
global model
switch -regexp $model {
"EP2108" {puts "I'm EP2108"}
default {puts "I'm not EP2108"}
}
运行效果截图:
A log file will be stored in d:\test_report
Trying 192.168.24.219...
Connected to 192.168.24.219.
Escape character is '^]'.
pSOSystem (192.168.24.219)
Copyright (c) Integrated Systems, Inc., 1992.
Welcome to pSOSystem...
Telnet> craft
CRAFT INSTANCE #1
xxxx NETWORKS EP2108 V1.0.0(19) 24-219 Jan 06, 2003 00:49:21
Login: admin
Password: **********
xxxx NETWORKS EP2108 V1.0.0(19) 24-219 Jan 06, 2003 00:49:21
MAIN MENU
SYSTEM INFORMATION
INVENTORY
PROVISION
MAINTENANCE
ALARMS INFORMATION
SNMP
SSH
INBAND MANAGEMENT PROVISION
LOGOUT
Enter selection:> I'm EP2108
[ 本帖最后由 武汉老徐 于 2009-10-14 10:56 编辑 ] |
|