1 install expect Tool
expect to establish a suite automated interactive tcl basis, in some scenarios require interactive input commands, interactive communications may be provided by a script which automatically interaction process is:
spawn start the specified process -> expect to obtain the specified keyword -> send want to specify a process to send the specified command -> execution is complete, quit.
Because tcl expect is based, it is necessary to ensure that the system is installed tcl:
# Check whether the installation TCL: [root @ localhost ~] # whereis TCL TCL: /usr/lib64/tcl8.5 /usr/include/tcl.h /usr/share/tcl8.5 # If not, use yum install tcl and expect: [the root @ localhost ~] # yum the install -Y tcl [the root @ localhost ~] # yum expect the install -Y # expect to see the path of the installation: [the root @ localhost ~] # Command expect -v / usr / bin / expect
2 expect of commonly used commands
command | Explanation |
---|---|
spawn | Start a new interactive process, followed by a specified program or command |
expect | Operation after receiving information from the process, if the match is successful, we expect the implementation of |
send | Send string to the process |
send exp_send | For transmitting the information specified string |
exp_continue | In expect many times you need to use match |
send_user | It corresponds to the printout of the shell echo |
interact | Allow user interaction |
exit | Exit expect script |
eof | expect execution quits |
set | Define the variable |
puts | Output variables |
set timeout | Set the timeout |
3 action principle Introduction
3.1 Example scripts
Here ssh remote login script to a server as an example, assume that this script name is remote_login.sh:
#!/usr/bin/expect set timeout 30 spawn ssh -l root 172.16.22.131 expect "password*" send "123456\r" interact
3.2 Interpretation scripting capabilities
(1) #!/usr/bin/expect
Above must be in the first line of the script file that tells the operating system, this script needs to use the system which script parsing engine to perform.
Specific path can be viewed through the command -v expect command.
note:
expect here and Linux bash, Windows and other programs, like the cmd, is a script execution engine.
Scripts need to be executable (chmod + x remote_login.sh, or chmod 755 auto_login.sh), then you can run the command ./remote_login.sh;
If you enter sh remote_login.sh, meaning is not the same: a clear call sh engine to execute this script, this time the first line of # / usr / bin / expect becomes ineffective!.
(2) set timeout 30
Connection timeout is 30 seconds.
(3) spawn ssh -l root 172.16.22.131
spawn, send commands are internal commands expect the tool, if the tool is not installed expect, "spawn not found" error occurs and so on.
Do not use which command to look like the spawn spawn, because there is no such program.
(4) expect "password*"
This command is used to determine the last string in the output contains "password *", if there is an immediate return, otherwise after waiting for some time to return. While waiting here a long timeout is set previously, that is, 30 seconds.
(5) send "123456\r"
Here is the implementation of the interactive action, and is equivalent to manually enter the password.
Tip: Command the end of the string with \ r, so, if the abnormal state of waiting appeared to be able to stay down for further verification.
(6) interact
keep users expect to perform after the completion of the cross-state, this time the user can manually operate.
If not this one, expect to perform after the completion of the script will exit just past Telnet terminal, the user would be unable to continue to operate.
Other examples of the use of scripts 4
4.1 Direct execute multiple commands expect
Note that the first line of content, it can only be performed by such a script ./script.sh this case:
! # / usr / bin / -f Expect SET timeout 10 # switch to root, and then the ls command df: the spawn SU - root Expect "Password *" send "123456 \ R & lt" Expect "] *" # wildcard send " LS \ r " expect" # * "# wildcard another form of the send" df -Th \ r " the send" exit \ r "# exit spawn open process expect eof # expect to exit this interactive program
4.2 execute multiple commands by calling expect shell
Note that the first line of content, in this case by sh script.sh, bash script.sh or ./script.sh, can execute such a script:
#!/bin/bash ip="172.16.22.131" username="root" password="123456" # 指定执行引擎 /usr/bin/expect <<EOF set time 30 spawn ssh $username@$ip df -Th expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$password\r" } } expect eof EOF
5 spawn not found solutions
This error occurs basically a scholar: Linux shell script in two ways:
One is the script as a command line parameter sh, as sh remote_login.sh, or sh /data/remote_login.sh;
One is the script as an executable script has execute permissions, such as ./remote_login.sh, or /data/remote_login.sh.
As sh command-line parameters to run, then the first line of the script #! / Usr / bin / expect will fail, it will spawn not found, send not found error, etc., all of the above script must automate_login.sh run the following command:
./automate_expect.sh