From http://blog.csdn.net/winstary/archive/2009/08/08/4422156.aspx
Use expect to implement automatic logon script, there are many online, but there is no clear explanation, beginners are generally copied, collection. But why do you want to write this but do not know it. This article uses a shortest example to illustrate the principle of scripting.
The script code is as follows:
#!/usr/bin/expect
Settimeout30
spawnssh-lusername192.168.1.1
Expect "Password:"
Send "ispass\r"
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 is required on the first line of the script.
2.[SETTIMEOUT30]
Basically know English is known this is set timeout time, now you just remember his timing unit is: seconds
3.[spawnssh-lusername192.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 "Whichspawn" 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.
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
#!/usr/bin/expect# Note the path of the installation, not sure whereisexpect a bit
#Changealoginshelltobash
SETUSER[LINDEX$ARGV0]
Spawnbash$user
Expect "]:"
Send "/bin/bash"
Expecteof
Exit
Spawn represents the statement executed at the local terminal, and after the statement begins execution, expect starts capturing the terminal's output information and then makes the corresponding operation. The captured (yes/no) content in the expect code is used to complete the operation of saving the key the first time the target host is accessed. With this sentence, the mandate of the SCP reduced the situation of disruption. The expect EOF at the end of the code corresponds to spawn, which indicates the termination of the capture terminal output information.
Linux expect simple explanation