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:
- #!/bin/bash
- Src_host=$1
- Src_username=$2
- Src_passwd=$3
- Dst_host=$4
- Dst_username=$5
- Dst_passwd=$6
- #在远程主机1上生成公私钥对
- Keygen ()
- {
- Expect << EOF
- Spawn ssh [email protected] $SRC _host ssh-keygen-t RSA
- While 1 {
- Expect {
- "Password:" {
- Send "$src _passwd\n"
- }
- "Yes/no*" {
- Send "yes\n"
- }
- "Enter file in which to save the key*" {
- Send "\ n"
- }
- "Enter passphrase*" {
- Send "\ n"
- }
- "Enter same passphrase again:" {
- Send "\ n"
- }
- "Overwrite (y/n)" {
- Send "n\n"
- }
- EOF {
- Exit
- }
- }
- }
- Eof
- }
- #从远程主机1获取公钥保存到本地
- Get_pub ()
- {
- Expect << EOF
- Spawn SCP [email protected] $SRC _host:~/.ssh/id_rsa.pub/tmp
- Expect {
- "Password:" {
- Send "$src _passwd\n"; exp_continue
- }
- "Yes/no*" {
- Send "yes\n"; exp_continue
- }
- EOF {
- Exit
- }
- }
- Eof
- }
- #将公钥的内容附加到远程主机2的authorized_keys
- Put_pub ()
- {
- Src_pub= "$ (cat/tmp/id_rsa.pub)"
- Expect << EOF
- Spawn ssh [email protected] $dst _host "chmod ~/.ssh;echo $src _pub >> ~/.ssh/authorized_keys;chmod Horized_ke
- Ys
- Expect {
- "Password:" {
- Send "$dst _passwd\n"; exp_continue
- }
- "Yes/no*" {
- Send "yes\n"; exp_continue
- }
- EOF {
- Exit
- }
- }
- Eof
- }
- Keygen
- Get_pub
- Put_pub
Copy Code
The script consists of 3 expect, which is relatively simple and uses
- ./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:
- Host1 user1 passwd1 host2 user2 passwd2
- Host3 user3 passwd3 host4 user4 passwd4
- Host5 user5 passwd5 host6 user6 passwd6
Copy Code
Use the following command to execute the script:
- 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