Starting from today, I have successively pasted some of my automated test scripts to help beginners. Because no suitable server storage is found Code So the code is first pasted in the blog body, and then complete code download is provided.
My automated test script runs in Debian Linux and is interpreted and executed using/usr/bin/benchmark CT. To simplify the processing, write some common functions into functions and put them in the commonlib. Exp file. Other script files can use the source commonlib. Exp command to reference these functions.
The following function completes Telenet to the target machine and login. From its implementation, we can see the conciseness of the test script written by Tcl/expect.
This function has three parameters: the IP address of the target machine, ipaddr, logon username, and logon password. The Telenet port number uses the default port 23.
The function uses three global variables, g_prompt, g_usrprompt, and g_pwdprompt, respectively, to indicate the command prompt after logon, prompt for user name input, and prompt for password input, the three global variables are defined in global. exp. Global variables are used because these values are widely used but are different from each other. Global variables can be easily modified.
The Code is as follows:
#*************************************** *********
# Telnet login routine
#
# @ Params
# Ipaddr-remote device IP Address
# User-user name to login
# Passwd-Login Password
#
# @ Return
# Spawn_id if login success, otherwise 0
#*************************************** *********
Proc login {ipaddr user passwd }{
Global g_prompt g_usrprompt g_pwdprompt
Spawn Telnet $ ipaddr
CT {
"$ G_usrprompt "{
Exp_send "$ user/R/N"
Exp_continue
}
"$ G_pwdprompt "{
Exp_send "$ passwd/R/N"
Exp_continue
}
-Ex "$ g_prompt "{
Dbglog "Login successful/N"
Return $ spawn_id
}
Timeout {
Send_user "timeout"
Return 0
}
}
}