There are many methods to complete telnet batch processing. Here we can use a TCL/CT script. Next we will introduce this process. This is a simple TCL/terminal CT script to complete telnet to a remote device. Through this example, we can see that it is very concise to construct a test program using the TCL/CT script.
Starting from today, I have successively pasted some of my automated test scripts to help beginners. Because no suitable server is found to store the code, the code is first pasted in the blog body, and then complete code download is provided.
The telnet batch processing test script runs in debian linux and is interpreted and executed using/usr/bin/verify 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 in
- # 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
- expect
- {
- "$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
- }
- }
- }
In this way, the TCL/CT script process for telent batch processing is completed.