From: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 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 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 viewed with which expect
spawn su-nginx // Switch user
expect "password:" // Prompt for password
send "test \ r" // Enter the 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]
spawn ssh -l $user $server
expect { "(yes/no)" { send "yes\r"; exp_continue } "password:" { send "$passwd\r" }
}
expect "*Last login*" interact
SCP Copy File
#! / usr / bin / expect
set timeout 10
set host [lindex $ argv 0] // The first parameter, the other 2,3,4 parameters are similar
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: $ dest_file
expect {
"(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
[reprint] Use expect for automatic shell interaction