expect is a program that allows you to "session" with an interactive program in a way that is set in the script content. Depending on the content of the script, expect can tell what content the program prompts or feeds and what is the correct answer. It is an interpreted scripting language that provides "branching and nesting Structures" to guide program flow. the shell is powerful, but it is not possible to implement multi-machine operations that have interactivity, such as SSH and FTP. And expect can help us to achieve.
One, install expect
- Yum Install expect
In fact, expect the same bash situation.
Two, examples
1,ssh to automatically log on and stop at the login server
View copy print?
- #!/usr/bin/expect-f
- Set IP [lindex $argv 0]//receive the first parameter and set IP
- Set password [lindex $argv 1]//receive the second parameter and set a password
- Set timeout 10//setting Time-out
- Spawn ssh [email protected] $ip//send SSH please 滶
- Expect {//return information match
- "*yes/no" {send "yes\r"; exp_continue}//The first SSH connection will prompt yes/no, continue
- "*password:" {send "$password \ r"}//password prompt, send password
- }
- Interact//interactive mode, the user will stay on the remote server above.
The results of the operation are as follows:
View copy print?
- [Email protected]:/home/zhangy#./test.exp 192.168.1.130 Admin
- Spawn ssh [email protected]
- Last Login:fri Sep 7 10:47:43 from 192.168.1.142
- [Email protected] ~]#
This example has a unified interface that can be connected to different machines depending on the IP and password. If you are bothered to enter IP and password, look at the following example
View copy print?
- #!/USR/BIN/EXPECT -F  
- set ip 192.168.1.130
- set password admin
- set timeout 10
- spawn ssh [email protected] $ip
-  EXPECT {  
- "*yes/no" { send "yes\r"; exp_continue}
- "*password:" { send "$password \ r"  }  
- }
-  INTERACT  
The results of the operation are as follows:
View copy print?
- [Email protected]:/home/zhangy#./web.exp
- Spawn ssh [email protected]
- Last Login:fri Sep 7 12:59:02 from 192.168.1.142
- [Email protected] ~]#
2,SSH telnet to the server and executes the command, after execution and exits
View copy print?
- #!/usr/bin/expect-f
- Set IP 192.168.1.130
- Set Password admin
- Set Timeout 10
- Spawn ssh [email protected] $ip
- Expect {
- "*yes/no" {send "yes\r"; Exp_continue}
- "*password:" {send "$password \ r"}
- }
- Expect "#*"
- Send "pwd\r"
- Send "exit\r"
- Expect EOF
The results of the operation are as follows:
View copy print?
- [email protected]:/home/zhangy# ./test3.exp
- spawn ssh [email protected]
- [email protected] ' s password:
- last login: fri sep 7 14:05:07 2012 from 116.246.27.90
- [[email protected] ~]# pwd
- /ROOT  
- Span style= "color: #ed1c24;" >[[email protected] ~]# exit
- logout
- connection to 192.168.1.130 closed.
3, Telnet to FTP, and download the file
View copy print?
- #!/usr/bin/expect-f
- Set IP [lindex $argv 0]
- Set dir [lindex $argv 1]
- Set file [lindex $argv 2]
- Set Timeout 10
- Spawn FTP $ip
- Expect "name*"
- Send "zwh\r"
- Expect "password:*"
- Send "zwh\r"
- Expect "ftp>*"
- Send "LCD $dir \ r"
- Expect {
- "*file" {send_user "local $_dir No such file or directory"; "Send" quit\r "}
- "*now*" {send "get $dir/$file $dir/$file \ r"}
- }
- Expect {
- "*failed" {send_user "remote $file No such file"; "Send" quit\r "}
- "*ok" {send_user "$file has been download\r"; "Send" quit\r "}
- }
- Expect EOF
The results of the operation are as follows:
View copy print?
- [Email protected]:/home/zhangy#./test2.exp 192.168.1.130/var/www/www aaa.html
- Spawn FTP 192.168.1.130
- Connected to 192.168.1.130.
- (VsFTPd 2.0.5)
- Name (192.168.1.130:root): ZWH
- 331 Specify the password.
- Password:
- Successful Login.
- Remote system type is UNIX.
- Using binary mode to transfer files.
- Ftp> lcd/var/www/www
- Local Directory Now/var/www/www
- Ftp> get/var/www/www/aaa.html/var/www/www/aaa.html
- Local:/var/www/www/aaa.html Remote:/var/www/www/aaa.html
- PORT command successful. Consider using PASV.
- Opening BINARY Mode data Connection for/var/www/www/aaa.html (bytes).
- 226 File send OK.
- Bytes Received in 0.00 secs (515.6 kb/s)
- Quit aaa.html has been download
- 221 Goodbye.
Linux expect automatic login ssh,ftp