"Go" using expect for automatic shell interaction

Source: Internet
Author: User

Original address: http://www.nginx.cn/1934.html

Where the shell script needs to interact with the here document is the implementation, but some commands require the user to manually go on the interaction such as passwd, SCP

It is very painful to eliminate user interaction for automatic deployment, expect can solve this kind of problem very well.

The core of expect is spawn expect send set

Spawn calling the command to execute
Expect waits 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 expect never timeout
Set Timeout-1

Set expect 300-second timeout, if more than 300 no expect content appears, then launch
Set Timeout 300

Expect writes the syntax, expect uses the TCL syntax.

A TCL command consists of words separated by spaces. Where the first word is the command name and the rest is the command parameter
CMD arg arg arg

The $ symbol represents the value of the variable. In this case, the variable name is foo.
$foo

The 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
[cmd Arg]

Double quotes mark 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
{Some stuff}

Backslash symbols are used to refer to special symbols. For example, n means wrapping. 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 that the expect package is 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, which can be

#which expect

/usr/bin/expect

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

#!/usr/bin/expect-f//   This expect path is the result of which expect viewing spawn Su-nginx       //Switch User expect "password:"      // Prompt to enter password send "TESTR"       //input Nginx Password interact                //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/expect   Set timeout 5 set server [lindex $argv 0] Set user [lindex $argv 1] Set passwd [lindex $argv 2]  

SCP Copy File

#!/usr/bin/expectset Timeout 10set host [lindex $argv 0]        //1th parameter, other 2,3,4 parameters similar to set username [lindex $argv 1]set Password [Lindex $argv 2]set src_file [lindex $argv 3]set dest_file [lindex $argv 4]spawn SCP $src _file [email protected] $host: $de St_file expect {"(yes/no)?"   {    send "yesn"    expect "*assword:" {send "$passwordn"}} "*assword:" {send "$passwordn"}}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

The same example:

Automatically establish SSH trust scripts

In the work often encountered to two hosts to establish SSH trust, manually set up too much trouble, simply write a script ssh_trust.sh from the Dynamic Build trust:

  1. #!/bin/bash
  2. Src_host=$1
  3. Src_username=$2
  4. Src_passwd=$3
  5. Dst_host=$4
  6. Dst_username=$5
  7. Dst_passwd=$6
  8. #在远程主机1上生成公私钥对
  9. Keygen ()
  10. {
  11. Expect << EOF
  12. Spawn ssh [email protected] $SRC _host ssh-keygen-t RSA
  13. While 1 {
  14. Expect {
  15. "Password:" {
  16. Send "$src _passwd\n"
  17. }
  18. "Yes/no*" {
  19. Send "yes\n"
  20. }
  21. "Enter file in which to save the key*" {
  22. Send "\ n"
  23. }
  24. "Enter passphrase*" {
  25. Send "\ n"
  26. }
  27. "Enter same passphrase again:" {
  28. Send "\ n"
  29. }
  30. "Overwrite (y/n)" {
  31. Send "n\n"
  32. }
  33. EOF {
  34. Exit
  35. }
  36. }
  37. }
  38. Eof
  39. }
  40. #从远程主机1获取公钥保存到本地
  41. Get_pub ()
  42. {
  43. Expect << EOF
  44. Spawn SCP [email protected] $SRC _host:~/.ssh/id_rsa.pub/tmp
  45. Expect {
  46. "Password:" {
  47. Send "$src _passwd\n"; exp_continue
  48. }
  49. "Yes/no*" {
  50. Send "yes\n"; exp_continue
  51. }
  52. EOF {
  53. Exit
  54. }
  55. }
  56. Eof
  57. }
  58. #将公钥的内容附加到远程主机2的authorized_keys
  59. Put_pub ()
  60. {
  61. Src_pub= "$ (cat/tmp/id_rsa.pub)"
  62. Expect << EOF
  63. Spawn ssh [email protected] $dst _host "chmod ~/.ssh;echo $src _pub >> ~/.ssh/authorized_keys;chmod Horized_ke
  64. Ys
  65. Expect {
  66. "Password:" {
  67. Send "$dst _passwd\n"; exp_continue
  68. }
  69. "Yes/no*" {
  70. Send "yes\n"; exp_continue
  71. }
  72. EOF {
  73. Exit
  74. }
  75. }
  76. Eof
  77. }
  78. Keygen
  79. Get_pub
  80. Put_pub
Copy Code

The script consists of 3 expect, which is relatively simple and uses

    1. ./ssh_trust.sh host1 user1 passwd1 host2 user2 passwd2
Copy Code

This is to establish an SSH trust from [email protected] to [email protected].
Description
1, of course, to install expect
2, the script is placed on the third party machine (can telnet host1 and host2) on the run can, of course, on the host1 and Host2 run on the line.
3, if you want to build trust in bulk, you can edit a folder file such as:

    1. Host1 user1 passwd1 host2 user2 passwd2
    2. Host3 user3 passwd3 host4 user4 passwd4
    3. Host5 user5 passwd5 host6 user6 passwd6
Copy Code

Use the following command to execute the script:

    1. Xargs-n6./ssh_trust.sh < file
Copy Code

4, hastily written, script is simply to implement the function, before use to ensure the availability of parameters (user password hostname), otherwise it is easy to error
5, only in the Linux Redhat test, the operation is successful, welcome everyone to advise ~ ~

"Go" using expect for automatic shell interaction

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.