1. What is expect
In the system management, we need to enter a password many times, for example: Connect ssh, connect ftp, then how can do not enter the password, we need to have a tool, can replace us to achieve with the terminal interaction, it can replace us to achieve with the terminal interaction, we do not have to wait for the computer next to enter the password, or run the corresponding commands according to the output of the system, which can be done by expect instead of us.
What the hell is expect?
Expect is a scripting language that is very simple to use.
2, installation expect
Yum-y Install expect
3. Example
#!/usr/bin/expectset Timeout 30spawn ssh-l username 192.168.1.1expect "Password:" send "ISPASSR" interact
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 needs to be in the first line of the script
2.[set Timeout []
basically know English is known this is set timeout time, now you just remember his timing unit is: 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, this command is brought by the shell, you can't find an executable file of dir.com or Dir.exe
Its main function is to add a shell to the SSH running process to pass the interactive instructions
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 "ISPASSR"]
This is the interactive action, 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 there is an abnormal waiting state can check
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
SVN update
#!/usr/bin/expect-f Set USER admin set PASS 123456# The working copy we ' re going to Updateset WC /data/www/www.da ta.com set Log/var/log/svn.log set SVNBIN/USR/BIN/SVN set timeout spawn $SVNBIN update-r HEAD--for Ce $WC expect { "Password for ' root ':" {send "\ r"; Exp_continue; } "*username:*" {send "$USER \ r"; exp_continue;} " *password for ' * ':* "{send" $PASS \ r "; exp_continue;} " *store Password unencrypted (yes/no)? * "{send" no\r "; exp_continue;} }
The use of Linux tool expect