Expect is a tool built on TCL to automate tasks that require interaction.
Because expect is TCL-based, you need to have TCL installed in your system
How to check?
[Email protected] ~]# Whereis tcl
Tcl:/usr/lib/tcl8.4/usr/share/tcl8.4
If you do not see the results, please install TCL first
Installation
> Installation Tcl
After extracting the TCL installation package
CD tcl8.4.11/unix/
./configure--prefix=/usr/local/tcl/--enable-shared
Make && make install
> Installation Expect
After unpacking the expect installation package
CD expect-5.43
./configure--prefix=/usr/local/expect/--with-tcl=/usr/local/tcl/lib/--with-tclinclude=/opt/tcl8.4.11/generic/- -enable-shared
Make && make install
Note: The specified/opt/tcl8.4.11/generic/is the TCL directory that we unzipped above
> Create a connection symbol
Ln-s/usr/local/expect/bin/expect/usr/bin/expect
> View Connection Symbols
Ls-l/usr/bin/expect
lrwxrwxrwx. 1 root root 28 September 8 11:21/usr/bin/expect-/usr/local/expect/bin/expect
This symbolic link will be used when writing the expect script file, for example, in the header of the expect file to specify the shell that executes the script
#!/usr/bin/expect
> Testing
[email protected] opt]# expect
Expect1.1> exit
[Email protected] opt]#
This will allow you to start running the expect script.
Usage
1. [#!/usr/bin/expect]
This line tells the code in the operating system script to use that Shell to execute. The expect here is actually a kind of thing with bash under Linux and cmd under Windows.
Note: This line is required on the first line of the script.
2. [Set timeout 30]
Basically know English is aware that this is set timeout time, now you just remember his timing unit is: seconds. Timeout-1 is never timed out, by default, timeout is 10 seconds;
3. [Spawn ssh-l username 192.168.1.1]
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. So don't use commands like "which spawn" to find spawn commands. Like dir in Windows is an internal command that comes with the shell and you can't find an executable file for dir.com or Dir.exe.
Its main function is to add a shell to the SSH running process to pass the interactive instructions.
Spawn is followed by shell commands that need to be executed, such as Spawn sudo touch testfile
4. [Expect "Password:"]
Here expect is also a expect internal command, a bit dizzy, expect shell command and internal command is the same, but not a function, habit is good. This command means to determine whether the last output contains the string "Password:", if any, return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds
5. [Send "ispass\r"]
This is where the interactive action is performed, which is equivalent to the action of entering the password manually.
Tips: The end of the command string do not forget to add "\ r", if the status of abnormal waiting can be checked.
6. [Interact]
After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. If this is not done, it will exit instead of remaining on the remote terminal. If you just log in past to execute
7. $ARGV parameter array
The expect script can accept parameters passed from bash. can be obtained using [lindex $argv n], n starting from 0, representing the first, second, third .... Parameters
Where $ARGC is the number of command-line arguments, $ARGV 0 is the script name itself, $argv as a command-line argument. [lrange $argv 0 0] represents the 1th parameter, [lrange $ARGV 0 4] is the first to the fifth parameter. Unlike the C language, the $ARGV does not contain the script name itself.
8. Send and Send_user
Send sends the information needed in the expect script to the spawn-initiated process, and Send_user just echoes the message from the user, similar to echo in the shell.
9. If you use the-d (debug parameter) on the first line (#!), you can output some useful information at run time.
Like you'll see
Argv[0] =/usr/bin/expect argv[1] =-D argv[2] =./launch.exp Argv[3] = 1 Argv[4] = 2 argv[5] = 3
You can also use these to complete parameter passing
Usage of exp_continue
Expect {
-re "Permission denied, please try again." {
Send_user "Error:permission denied.\n"
Exit
}
-re "is sure you want to continue connecting (yes/no)?" {
Send "yes\r"; exp_continue
}
-re "Assword:" {
Send "$loginpass \ r"; exp_continue
}
-re "Connection refused" {
Exit
}
Timeout {
Exit
}
EOF {
Exit
}
}
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.
Example of the following script example, implemented: Login to a Linux server, execute df-h and other commands
VI expectexample.sh After you enter the following script content to save, use the chmod +x expectexample.sh command to give the script executable permissions.
#!/usr/bin/expect-f
#--------------------------------------------Set the variable,you can modify the value
Set ipaddr [lrange $argv 0 0]
Set port [Lrange $argv 1 1]
Set Loginuser [Lrange $argv 2 2]
Set Loginpass [Lrange $argv 3 3]
Set Timeout 30
Set Cmd_prompt "]#|~]?"
#--------------------------------------------Login by SSH
Spawn ssh-p $port [email protected] $ipaddr
Set Timeout 30
Expect {
-re "is sure you want to continue connecting (yes/no)?" {
Send "yes\r"
}
-re "Assword:" {
Send "$loginpass \ r"
}
-re "Permission denied, please try again." {
Send_user "Error:permission denied.\n"
Exit
}
-re "Connection refused" {
Exit
}
Timeout {
Exit
}
EOF {
Exit
}
}
Expect {
#password not correct
-re "Permission denied, please try again." {
Send_user "Error:password is wrong.\n"
Exit
}
}
Expect {
-re "Assword:" {
Send "$loginpass \ r"
}
-re $cmd _prompt {
Send "\ r"
}
}
Expect {
#password not correct
-re "Permission denied, please try again." {
Send_user "Error:password is wrong.\n"
Exit
}
}
#--------------------------------------------Now,we do some commands
EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "df-h\r"
}
}
EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "free-m\r"
}
}
EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "uptime\r"
}
}
EXEC sleep 1
#--------------------------------------------Exit
Expect {
-re $cmd _prompt {
Send "exit\r"
}
}
Exit
#interact
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux expect usage