Expect is used to provide automatic interaction with the tool. For example, if you want to ssh into the server, each time you enter the password you feel trouble, then you can use expect to do automatic interaction, so that you do not have to enter the password every time.
First look at the example:
# !/usr/bin/expect#set Timeout #设置超时时间
Spawn ssh [email protected]"*password:" "123\r" # expect "*#"interact
Explain:
1.#!/usr/bin/expect: You need to install the software first, then to explain the use of expect to perform
2.spawn ssh [email protected]: Spawn is a expect internal command that can be executed after entering the expect environment to execute the commands behind it.
3.expect "*password:" is also the internal command of expect, used to doubt the key string, if any, will immediately return the contents of the following settings, if not to see if the time-out is set.
4.send "123\r": When an interactive action is performed, it is equivalent to manually entering the password, and after the expect intercepts the keyword, it enters the content after the send.
5.interact: After the completion of the control of the interactive State, the console, this time you can do what you want to do. If this is not the case, it will exit after the login is completed, rather than stay on the remote terminal.
Today suddenly think of expect, so I looked again, hope to help you.
Introduction and usage of Linux expect