In shell development, when we connect to FTP or passwd or sudo, we need to enter the password manually. This is obviously inappropriate for automation. And expect's strong interaction model solves this problem. I am involved in this work, the individual is also simple to understand, solve practical problems, as a learning record.
One: Installation "Reference address: https://www.cnblogs.com/lixigang/articles/4849527.html"
Expect needs the support of TCL programming language, so expect need to install two things. A Tcl, a expect;[before installation, you can determine whether the server is installed by "Which/whereis expect"
1, tcl download and installation
Download: wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gzdecompression: TAR-XZVF tcl8.4.11-src.tar.gz "-C Destination Address]configuration: cd Tcl8.4.11/unix &&./configure--prefix=/usr/tcl--enable-shared && make &&make Insta llcopy: After installation, copy the tclUnixPort.h under directory UNIX to the generic directory in the tcl8.4.11 directory
2, expect download and installation Download: wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gzdecompression: TAR-XZVF expect5.45.tar.gzconfiguration: CD expect5.45 [switch to decompression path]installation:./configure--prefix=/usr/expect--with-tcl=/usr/tcl/lib--with-tclinclude=. /tcl8.4.11/generic && make && make install
3. Create a soft chain ln-s/usr/expect/bin/expect/usr/bin/expect [point to the reference, you can check if your Linux is installed expect. Some words are installed correctly. ]
II: Command Introduction Let's look at how expect is implemented by analyzing a simple script.
1#!/usr/bin/expect2Set timeout53Setpasswd{9m[s) uwe4r}4SpawnsuRoot5 Expect {6 "Password"{Send"$passwd \ r"; Exp_continue}7 "Password"{Send"$passwd \ r"; Exp_continue}8 EOF {;}9 }TenExecSleep 5 One Expect { A "#"send{"Touchtt\r "} - EOF {;} - } theExpect EOF
1, #!/usr/bin/expect
if called through the SH command, the statement is ignored, the actual script is still executed in the Bash scripting language, the file is invoked by applying the expect command, and the expect file is given the executable permission, the corresponding path plus the file name (or./file name), Can implement expect scripting language processing "If you want to use bash in the process, you can call the bash shell by adding a/bin/bash way to the bash script. " 2, set timeout tenset time-out, units: seconds3. Set the variable passwdby Set mode, you do not need to add "=", you can pass parameters through $args, using [lindex $args n] Call parameters, where n starting from 0, respectively, the first, the second, the third .... Parameters. As we assign the first parameter passed to the script internal variable: set My_var "[lindex $args 0]" 4. Spawn su Rootstarts a process and adds a shell to the process that can be used to pass the interaction information. 5. Expect ...is an internal command of the expect that waits for some string (such as "password") sent by the process, which can wait for multiple strings at the same time and perform different operations on each string, in Switch/case. In this case, curly braces (must be two) are used, and strings such as expect "Password" can be directly followed; send "$passwd \ r"once the exp_continue is used, it is re-executed from the beginning of the current expect block and can be easily understood by the continue of the while loop.6. Sleepset sleep time in seconds because automatic execution is limited to system response time and execution time for in-send instructions 7. EOFThe end of the process has two options in expect. One is an EOF match, and EOF is sent to expect at the end of the spawn process to end the process. You can also use interact to leave control to the console after the execution is complete, keeping the interaction State; Thank you!
Original Blog, reproduced please specify the source! Welcome Email communication: [email protected]
Interaction mode in the shell: expect