Tcl/expect automated test script instance 1-Telnet to the target machine

Source: Internet
Author: User
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
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.