自动化测试之---Expect交互实例
来源:互联网 发布:js获取select值 编辑:程序博客网 时间:2024/04/29 11:05
Expect使用例子,更多关于Expect的资料,请参考Exploring Expect和ActiveTcl User Guide。
###############################################################################
#@@Proc
#Name: Get_Key_From_Config
#Desc: get config value from configuration file
#Args: section - name of section in the configuration file
# key - name of key in the configuration file
#Ret: the value of the section and key
###############################################################################
proc Get_Key_From_Config {section key {comment "#"} {equal "="}} {
global conf_file
set section_list ""
set foundkey 0
if {[catch {open $conf_file "r"} fd]} {
error "Cannot open $conf_file file:$fd"
return 0
}
seek $fd 0 start
while {[gets $fd line] >= 0} {
set findsection [regexp -- "^//s*///[$section///]//s*$" $line]
if {[regexp -- "^//s*$comment" $line] || $line == "" || $findsection == 0} {
continue
} elseif {[regexp -- "^//s*///[$section///]//s*$" $line]} {
while {[gets $fd new_line] >= 0} {
if {[regexp -- "^//s*$comment" $new_line] || $new_line == ""} {
continue
} elseif {[regexp -- "^//s*///[.*?///]//s*$" $new_line]} {
break
} else {
lappend section_list $new_line
}
}
break
}
}
close $fd
foreach lst $section_list {
if {[regexp -- "^//s*$key//s*$equal//s*(.*?)//s*$" $lst - value]} {
set foundkey 1
break
}
}
if {$foundkey != 1} {
set value ""
}
return $value
}
###############################################################################
#@@Proc
#Name: Get_Capture_From_Windows
#Desc: Using to get capture from windows pc.
#Ret: The capture packets
###############################################################################
proc Get_Capture_From_Windows {} {
global conf_file
set user [Get_Key_From_Config common dscp_user]
set password [Get_Key_From_Config common dscp_password]
set wan_ip [Get_Key_From_Config common dscp_ip]
set int [Get_Key_From_Config common dscp_int]
set count [Get_Key_From_Config common dscp_count]
set cap_string "tshark -B 10 -i $int -c $count -V dst $wan_ip > cap.log"
if {$user == "" || $password == "" || $wan_ip == "" || $int == "" || $count == ""} {
error "/nThe dscp_user or dscp_password or dscp_ip or dscp_int or dscp_count key/
can not be empty in the common section, please check the $conf_file."
return 0
}
puts "/nBegin capture packets to verify whether DSCP is mark correct, it will take a while......"
set ppid [spawn plink -telnet $wan_ip]
#By default, the send/expect dialogue is logged to stdout (and a logfile if open). The logging to stdout is disabled by the command log_user 0 and reenabled by log_user 1". Logging to the logfile is unchanged.
log_user 0
set timeout 20
#set send_slow {10 .002} would force send -s to send strings with 2 millisecond in between each 10 characters sent.
set send_slow {10 .002}
set cap {}
expect {
-re "login:" {send -s "$user/r"; set cap $cap$expect_out(buffer); set timeout 60; exp_continue}
-re "password:" {send -s "$password/r"; set cap $cap$expect_out(buffer); exp_continue}
-re "Welcome to Microsoft Telnet Server.*>" {send -s "$cap_string/r"; set cap $cap$expect_out(buffer); exp_continue}
-re "packets captured.*>" {send -s "findstr DSCP cap.log/r"; set cap $cap$expect_out(buffer)}
timeout {error "can not connect the $wan_ip, please check the connection."}
#timeout {}
}
#expect -re "packets captured.*>" {send -s "type cap.log/r"; set cap $cap$expect_out(buffer)}
expect -re (.+) {
set cap $cap$expect_out(1,string)
if {[regexp -- ".*$user>$" $cap]} {
send -s "exit/r"
} else {
exp_continue
}
}
expect eof {}
return $cap
}
###############################################################################
#@@Proc
#Name: Get_Capture_From_Linux
#Desc: Using to get capture from linux pc.
#Ret: The capture packets
###############################################################################
proc Get_Capture_From_Linux {} {
global conf_file
set user [Get_Key_From_Config common dscp_user]
set password [Get_Key_From_Config common dscp_password]
set wan_ip [Get_Key_From_Config common dscp_ip]
set int [Get_Key_From_Config common dscp_int]
set count [Get_Key_From_Config common dscp_count]
#set cap_string "nohup tethereal -i $int -c $count -V dst $wan_ip > /tmp/cap.log 2>&1 &"
set cap_string "tethereal -i $int -c $count -V dst $wan_ip | grep -w DSCP -B 3 -A 17"
if {$user == "" || $password == "" || $wan_ip == "" || $int == "" || $count == ""} {
error "/nThe dscp_user or dscp_password or dscp_ip or dscp_int or dscp_count key/
can not be empty in the common section, please check the $conf_file."
return 0
}
puts "/nBegin capture packets to verify whether DSCP is mark correct, it will take a while......"
set ppid [spawn plink -telnet $wan_ip]
#By default, the send/expect dialogue is logged to stdout (and a logfile if open). The logging to stdout is disabled by the command log_user 0 and reenabled by log_user 1". Logging to the logfile is unchanged.
log_user 0
set timeout 30
#set send_slow {1 .02} would force send -s to send strings with 0.2 seconds in between each 1 characters sent.
set send_slow {1 .2}
set cap {}
expect {
-re "Kernel.*login:" {exp_send -s "$user/r"; set cap $cap$expect_out(buffer); exp_continue}
-re "assword:" {exp_send -s "$password/r"; set cap $cap$expect_out(buffer); exp_continue}
-re "Last login.*]#" {exp_send -s "$cap_string/r"; set cap $cap$expect_out(buffer)}
-re "/[0-9/]+/s*]#" {exp_send -s "exit/r"}
timeout {error "can not connect the $wan_ip, please check the connection."}
#timeout {}
}
expect -re (.+) {
set cap $cap$expect_out(1,string)
if {[regexp -- "]#$" $cap]} {
exp_send -s "exit/r"
} else {
exp_continue
}
}
expect eof {}
return $cap
if {0} {
expect eof {}
sleep 10
set ppid [spawn plink -telnet $wan_ip]
#log_user 0
set send_slow {1 .1}
set cap {}
expect {
-re "Kernel.*login:" {exp_send -s "$user/r"; exp_continue}
-re "assword:" {exp_send -s "$password/r"; exp_continue}
-re "Last login.*]#" {exp_send -s "cat /tmp/cap.log/r"}
timeout {error "can not connect the $wan_ip, please check the connection."}
#timeout {}
}
expect -re (.+) {
set cap $cap$expect_out(1,string)
if {[regexp -- "]#$" $cap]} {
exp_send -s "exit/r"
} else {
exp_continue
}
}
expect eof {}
return $cap
}
}
任何形式的转载,请写明出处:
http://blog.csdn.net/clhon
- 自动化测试之---Expect交互实例
- TCL/EXPECT自动化测试脚本实例
- 自动化交互脚本Expect简单介绍及应用实例
- TCL/EXPECT自动化测试脚本实例七 --- 构造报文测试
- TCL/EXPECT自动化测试脚本实例七 --- 构造报文测试
- expect 自动化测试使用
- TCL/EXPECT自动化测试脚本实例二 --- 主程序
- TCL/EXPECT自动化测试脚本实例三 --- 全局变量
- TCL/EXPECT自动化测试脚本实例四 --- 批命令执行
- TCL/EXPECT自动化测试脚本
- 自动化测试专题-tcl,expect
- java版本的expect: 简单方式实现ssh/telnet与服务器端的交互和自动化测试
- TCL/EXPECT自动化测试脚本实例六 --- SNMP community长度测试
- TCL/EXPECT自动化测试脚本实例一 --- telnet到目标机器
- TCL/EXPECT自动化测试脚本实例五 --- 由文件中读取一行
- expect自动化测试脚本
- android自动化测试之单元测试实例
- android自动化测试之单元测试实例 .
- pictureBox上透明Label的实现
- 一些很酷的.Net技巧
- 今天笔试记录
- 从A股暴跌看中国经济发展
- 將圖片轉換為HTML TABLE
- 自动化测试之---Expect交互实例
- Chapter 2 The first steps in Vim
- 技术站点推荐
- 用jdom解析applicationContext.xml出错
- 用VFP读写*.INI 文件
- bfin-xxx-gcc-4.3.1中config.status的生成及运行(7):创建临时目录
- amoeba for mysql 0.22 发布
- 重写html代码当中的img标签内容,图片地址不变
- Linux 运行级详解