武汉老徐 发表于 2009-10-14 10:52:55

开始进行tcl/expect开发

开发环境搭建好了,就可以开始着手进行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                                                            #
#                                                                           #
#Fuction:Login 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 mkdir $log_dir
}
set log_file :] -].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 "


#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 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 "Password:"
   
send "$Login_arg(pass)\r"
expect -re "Enter selection:>" {
                puts $log_file "\n 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 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 chassis:$chassis_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 NETWORKSEP2108 V1.0.0(19) 24-219            Jan 06, 200300:49:21



Login: admin
Password: **********


xxxx NETWORKSEP2108 V1.0.0(19) 24-219            Jan 06, 200300: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 编辑 ]

武汉老徐 发表于 2009-10-22 18:30:39

proc LOGIN {IP} { }
#定义一个过程LOGIN,形参IP

set Login_arg(command)   "craft"
set Login_arg(user)      "admin"
set Login_arg(pass)      "password#1"
#设置登录参数,这里用set给数组Login_arg()的元素赋值

#Declare global var:
global log_dir log_file id
global oem model ver label
global expect_out
#用global声明或引用全局变量,其中expect_out是expect的输出buffer,之所以定义为全局变量,在其它应用中用到,这里没有用



#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 mkdir $log_dir
}
set log_file :] -].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 "

#创建日志文件,文件名命名是根据当前时间动态生成的


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 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 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 Login Error:Wrong user ID or password,please check it and try again.\n"
                exit 3
            }
#用spawn开一个telnet会话,登录到目标设备,自动生成一个spawn_id放到id中备用

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"

#用正则表达式攫取expect_out中的有价值信息,并记录在log中


LOGIN 192.168.24.219
#调用上面的过程,即可。
页: [1]
查看完整版本: 开始进行tcl/expect开发