When using some basic shell commands, the machine needs to interact with the person to determine the execution of the command. such as CP Test.txt Boo/test.txt, will ask whether to overwrite? SSH remote login, you need to enter a manual password before you can continue to execute the SSH command and so on. How do you write a command line that can automatically interact with the shell without the need for manual intervention? Expect was born here.
"Expect is a free programming language that enables automated and interactive tasks to communicate without the need for human intervention. Expect's author, Don Libes, began writing Expect in 1990 with the following definition of Expect: Expect is a software suite for automatic interactivity (Expect [is a] software suite for automating Interactive tools). Using it, the system administrator can create scripts to provide input to commands or programs that are expected to be entered from the terminal (terminal), which in general need to be entered manually. ”
Below we install the expect and do a simple test to make sure that the expect script is used correctly. Expect requires the support of the TCL programming language and TCL must first be installed to run expect on the system.
Expect
Http://download.chinaunix.net/download/0003000/2845.shtml
Tcl
Http://download.chinaunix.net/download/0001000/22.shtml
1. Install TCL First
Enter the TCL unzip directory and enter the UNIX directory
#./configure
#make
#make Install
2. Post-Installation expect
Go to expect extract directory
#./configure--with-tclinclude=/usr/src/tcl8.4.19/generic/--with-tclconfig=/usr/local/lib/
#make
#make Install
Complete, test
#expect
Expect1.1>
Expect1.1>
All OK, the installation is successful.
Write a test script to test the expect as it works. Touch test.exp, file with exp end or TCL end, can be executed. Note "Spawn is a expect internal command that can be executed after entering the expect environment, if no expect is installed or the Spawn command is not found directly under the default shell. 】
# # #author: [Email protected]
#!/usr/local/bin/expect
Set Timeout 30
Set Password ZHISHI2016&*!DB1
Spawn ssh-l Root 118.76.22.83-p 14010
Expect "*assword:" {send "$password \ n"}
Expect "*#"
Send "echo success,we get into the remote directory!\r"
Expect "100%"
Expect EOF
Interact
After saving, run expect test.exp, after successful output: Success,we get into the remote directory!
Expect first introduced here, in-depth understanding of the Web-related information can be browsed.
How do I write a shell script without human intervention?