With expect implementation of automatic login 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
Set Timeout 30
Spawn ssh-l username 192.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. [Set timeout 30]
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 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.
Tip: Don't forget to add "/R" at the end of the command string, and check if there is an abnormal waiting state.
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 Whereis expect a bit)
# Change a login shell to bash
Set user [lindex $argv 0]
Spawn Bash $user
Expect "]:"
Send "/bin/bash"
Expect EOF
Exit
Example:
#!/usr/bin/expect-f
Set Password 123456
Set date [exec date-d Yesterday "+%y-%m-%d"]
#exp_internal 1
SPAWN/USR/BIN/SCP [email protected]:/home/oracle/cai-website.log. $date/home/oracle/zy/backup/
Set Timeout 300
Expect "Password:"
Send "$password \ r"
Set Timeout 300
Log_file/home/oracle/zy/test
Expect EOF
Exit
If the file is named expect-scp.sh then execute
expect expect-scp.sh or
The ./expect-scp.sh will get the following results:
SPAWN/USR/BIN/SCP [email protected]:/home/oracle/cai-website.log.2011-10-24/home/oracle/zy/backup/
cai-website.log.2011-10-24 100% 1183KB 1.2mb/s 00:01
Send:spawn ID EXP6 not open
While executing
"Send" $password \ r ""
(file "expect.sh" line 8)
Turn from
Shell expect simple usage-little rookie of the World-blog channel-csdn.net
http://blog.csdn.net/zhuying_linux/article/details/6902135
Simple usage of shell expect "go"