#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
}
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
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 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"]"
#创建日志文件,文件名命名是根据当前时间动态生成的
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
}
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
}
#用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