Use the TCL/EXPECT script to implement telnet Batch Processing

Source: Internet
Author: User

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:

 
 
  1. #************************************************  
  2. # telnet login routine  
  3. #  
  4. # @PARAMS  
  5. # ipaddr - remote device ip address  
  6. # user   - user name to login in  
  7. # passwd - login password  
  8. #  
  9. # @RETURN  
  10. # spawn_id if login success, otherwise 0  
  11. #************************************************  
  12.  proc login   
  13. {  
  14.  ipaddr user passwd  
  15.  }  
  16.  
  17. {  
  18.  global g_prompt g_usrPrompt g_pwdPrompt  
  19.  
  20.   spawn telnet $ipaddr  
  21.   expect   
  22.  {  
  23.   "$g_usrPrompt"   
  24.   {  
  25.    exp_send "$user\r\n"  
  26.     exp_continue  
  27.    }  
  28.   "$g_pwdPrompt"   
  29.   {  
  30.    exp_send "$passwd\r\n"  
  31.     exp_continue  
  32.    }  
  33.   -ex "$g_prompt"   
  34.   {  
  35.    dbgLog "Login Successful\n"  
  36.     return $spawn_id  
  37.    }  
  38.   timeout   
  39.   {  
  40.    send_user "timeout"  
  41.     return 0  
  42.    }  
  43.  }  

In this way, the TCL/CT script process for telent batch processing is completed.

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.