1, Here Document interaction free
1.1 overview and format
- Provides a list of commands to interactive programs using I/O redirection
- An alternative to standard input
- Syntax format
command <<sign ... ... sign
1.2 precautions for use
- The tag can use any legal character
- The slogan at the end must be written in the top grid, and there must be no characters in front of it
- There must also be no characters (including spaces) after the tag at the end
- Spaces before and after the opening mark are omitted
1.3 example 1: accept input and print through read command
[root@localhost opt]# cat 00.sh #!/bin/bash read -i <<EOF Hello Word! EOF echo $i [root@localhost opt]# chmod +x 00.sh [root@localhost opt]# ./00.sh Hello Word!
1.4 example 2: set password for user through passwd
[root@localhost opt]# cat 00.sh #!/bin/bash passwd zhangsan <<EOF 123456 123456 EOF [root@localhost opt]# ./00.sh Change user zhangsan Your password. New password: invalid password: password is less than 8 characters Re enter the new password: passwd: All authentication tokens have been successfully updated.
1.5 turn off variable replacement function
[root@localhost opt]# cat 00.sh #!/bin/bash cat <<'EOF' $abc EOF [root@localhost opt]# ./00.sh $abc
ps: use single quotation marks to realize the function of closing variable replacement
1.6 multiline notes
[root@localhost opt]# cat 00.sh #!/bin/bash : <<EOF this is a test file EOF [root@localhost opt]# ./00.sh [root@localhost opt]#
1.7 remove TAB characters before each line
[root@localhost opt]# cat 00.sh #!/bin/bash cat <<-'EOF' this is a test file EOF [root@localhost opt]# ./00.sh this is a test file
ps: '-' indicates inhibiting the TAB effect at the beginning of the line
2, Expect no interaction
2.1 basic commands
- A tool based on tcl
- For automatic control and testing
- Solve interaction related problems in shell scripts
expect
- Judge whether the last output result contains the specified string. If so, it will be returned immediately. Otherwise, it will be returned after the timeout
- Only the output of processes started by spawn can be captured
- It is used to receive the output after the command is executed, and then match the expected string
send
- Send a string to the process to simulate user input
- This command cannot automatically enter and line feed. Generally, / r (enter) should be added
spawn
- Start the process and track the subsequent interaction information
Terminator
- Expect EOF: wait for execution to end
- interact: keep the interactive state after execution and hand over the control to the console
set
- Set the timeout time, and continue to execute subsequent instructions when it expires
- The unit is seconds
- timeout -1 means never timeout
- By default, the timeout is 10 seconds
exp_continue
- Allow expect to continue executing instructions downward
send_user
- Echo command, equivalent to echo
Accept parameters
- The Expect script can accept parameters passed from bash
- It can be obtained using [lindex $argv n]
- n starts from 0 and represents the first, second and third... Parameters respectively
2.2 Expect syntax
- Single branch grammar
expect "password:" {send "mypassword\r";}
- Multi branch pattern syntax
expect "aaa" {send "AAA\r"} expect "bbb" {send "BBB\r"} expect "ccc" {send "CCC\r"}
ps:Send command does not have the function of carriage return and line feed. Generally, it needs to add \ r or \ n
ps: as long as any one of aaa, bbb or ccc is matched, execute the corresponding send statement and exit the expect statement
expect { "aaa" {send "AAA";exp_continue} "bbb" {send "BBB";exp_continue} "ccc" {send "CCC"} } ps:exp_continue Indicates to continue the following matching. If it matches aaa,End of execution send Continue to match downward after the statement bbb2.3 Expect Execution mode
#!/usr/bin/expect #overtime set timeout 20 # Open log log_file test.log #display information log_user 1 #Define variables set hostname [lindex $argv 0] set password [lindex $argv 1] #Tracking instruction spawn ssh root@${hostname} #Capture tips expect { "connecting(yes/no)" {send "yes\r";exp_continue} "*password:" {send "${password}\r";} } #Transfer of control interact2.4 Embedded execution
#/bin/bash hostname=$1 password=$2 #expect embedding /usr/bin/expect <<-EOF spawn ssh root@${hostname} #Capture tips expect { "connecting(yes/no)" {send "yes\r";exp_continue} "*password:" {send "${password}\r";} } expect "*]#" {send "exit\r"} expect eof EOFIII Expect case
- Case 1: create user and set password
#!/bin/bash user=$1 password=$2 useradd $user expect <<EOF spawn passwd $user expect "New password:" send "${password}\r" expect "Retype new password:" send "${password}\r" expect eof; EOF
- Case 2: use Expect to complete the SSH login process
#!/usr/bin/expect set timeout 5 set hostname [lindex $argv 0] set password [lindex $argv 1] spawn ssh $hostname expect { "Connection refused" exit "Name or service not known" exit "to continue" {send "yes\r";exp_continue} "password:" {send "$password\r"} } interact exit
- Case 3: using Expect to complete the FTP login process
#!/bin/expect -f set timeout 10 spawn ftp 192.168.10.10 expect "Name" send "ftp\r" expect "Password:*" send "\r" expect "ftp>*" interact expect eof