Linux Expect tools use

Source: Internet
Author: User

The shell script needs to interact where the here document is implemented, but some commands require the user to manually go on the interaction such as passwd, SCP, to eliminate the user interaction of automatic deployment is very painful, expect can solve such problems well.

The core of expect is spawn expect send set
  Spawn calls the command to execute to wait for the command prompt to appear, which is the prompt to capture user input:
   send sends values that need to be interacted with instead of manually entering content by the user
   Set Variable value
  Interact After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. If this is not done, it will exit instead of remaining on the remote terminal.
  expect EOF This must be added, corresponding to the spawn to indicate that the capture terminal output information is terminated, similar to IF....ENDIF
  expect scripts must end with interact or expect EOF, and automating tasks usually expect EOF is enough.
  Set timeout-1 settings expect never time out
  Set timeout setting expect 300 seconds timeout, if more than 300 no expect content appears, exit

Expect writes the syntax, expect uses the TCL syntax.
   cmd arg arg arg a TCL command consists of words separated by spaces. Where the first word is the command name and the rest is the command parameter
  The $foo $ symbol represents the value of the variable. In this case, the variable name is foo.
   [cmd arg] square brackets execute a nested command. For example, if you want to pass the result of a command as a parameter to another command, then you use this symbol
  the "some stuff" double quote marks the phrase as a parameter of the command. The "$" symbol and square brackets are still interpreted inside the double quotation marks
  {Some stuff} curly braces also mark the phrase as a parameter of the command. However, other symbols are not interpreted in curly braces
Backslash symbols are used to refer to special symbols. For example, \ n represents a newline. Backslash symbols are also used to close the "$" symbol, the special meaning of quotation marks, square brackets and curly braces

Expect use instances
1, first confirm the expect package to be placed.
    #rpm-qa | grep expect

If not, you need to download the installation,
    #yum Install expect

2, after the installation is complete, check the path of the expect, you can use
    #which expect
/usr/bin/expect

  3. Editing scripts
    #vi autosu.sh
Add the following content

// This expect path is the result of the which expect view su // Switch Users " Password: " // prompt to enter a password " test\r " // Enter the password for Nginx // Operation completed

4. Determine if the script has executable permissions
    chmod +x autosu.sh

5. Execute script expect autosu.sh or ./autosu.sh

Expect common scripts
Log on to the remote server

#!/usr/bin/expectset Timeout5set server [lindex $argv0] Set user [lindex $argv1] Setpasswd[Lindex $argv2] SpawnSSH-L $user $server expect {"(yes/no)"{Send"yes\r"; Exp_continue}"Password:"{Send"$passwd \ r"}} expect"*last login*"Interact SCP Copy File #!/usr/bin/expectset TimeoutTenset host [lindex $argv0]//the 1th parameter is similar to the other 2,3,4 parametersSet username [lindex $argv1]set Password [lindex $argv2]set src_file [lindex $argv3]set dest_file [lindex $argv4]spawnSCP$src _file [email protected] $host: $dest _fileexpect {"(yes/no)?"{Send"yes\n"expect"*assword:"{Send"$password \ n"}}"*assword:"{Send"$password \ n"}}expect"100%"expect EOF

How to use
    ./EXPECT_SCP 192.168.75.130 Root 123456/root/src_file/root/dest_file
When the above command executes, the Src_file file under the local/root directory will be copied to/root in the host 192.168.75.130 with the username root, password 123456, and the source file will be renamed to Dest_file

Linux Expect tools use

Related Article

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.