Linux expect automatic login ssh,ftp

Source: Internet
Author: User

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

    1. 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?
  1. #!/usr/bin/expect-f
  2. Set IP [lindex $argv 0]//receive the first parameter and set IP
  3. Set password [lindex $argv 1]//receive the second parameter and set a password
  4. Set timeout 10//setting Time-out
  5. Spawn ssh [email protected] $ip//send SSH please 滶
  6. Expect {//return information match
  7. "*yes/no" {send "yes\r"; exp_continue}//The first SSH connection will prompt yes/no, continue
  8. "*password:" {send "$password \ r"}//password prompt, send password
  9. }
  10. Interact//interactive mode, the user will stay on the remote server above.

The results of the operation are as follows:

View copy print?
    1. [Email protected]:/home/zhangy#./test.exp 192.168.1.130 Admin
    2. Spawn ssh [email protected]
    3. Last Login:fri Sep 7 10:47:43 from 192.168.1.142
    4. [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?
    1. #!/USR/BIN/EXPECT -F  
    2.  set ip 192.168.1.130  
    3.  set  password admin  
    4.  set timeout 10   
    5.  spawn ssh [email protected] $ip   
    6.  EXPECT {  
    7.   "*yes/no"  { send  "yes\r";  exp_continue}  
    8.   "*password:"  { send  "$password \ r"  }  
    9.  }  
    10.  INTERACT  

The results of the operation are as follows:

View copy print?
    1. [Email protected]:/home/zhangy#./web.exp
    2. Spawn ssh [email protected]
    3. Last Login:fri Sep 7 12:59:02 from 192.168.1.142
    4. [Email protected] ~]#

2,SSH telnet to the server and executes the command, after execution and exits

View copy print?
  1. #!/usr/bin/expect-f
  2. Set IP 192.168.1.130
  3. Set Password admin
  4. Set Timeout 10
  5. Spawn ssh [email protected] $ip
  6. Expect {
  7. "*yes/no" {send "yes\r"; Exp_continue}
  8. "*password:" {send "$password \ r"}
  9. }
  10. Expect "#*"
  11. Send "pwd\r"
  12. Send "exit\r"
  13. Expect EOF

The results of the operation are as follows:

View copy print?
    1. [email protected]:/home/zhangy# ./test3.exp   
    2. spawn ssh [email protected]  
    3. [email protected] ' s password:  
    4. last login: fri sep  7 14:05:07 2012  from 116.246.27.90  
    5. [[email protected]  ~]# pwd  
    6. /ROOT  
    7. Span style= "color: #ed1c24;" >[[email protected] ~]# exit  
    8. logout   
    9. connection to 192.168.1.130 closed.   

3, Telnet to FTP, and download the file

View copy print?
  1. #!/usr/bin/expect-f
  2. Set IP [lindex $argv 0]
  3. Set dir [lindex $argv 1]
  4. Set file [lindex $argv 2]
  5. Set Timeout 10
  6. Spawn FTP $ip
  7. Expect "name*"
  8. Send "zwh\r"
  9. Expect "password:*"
  10. Send "zwh\r"
  11. Expect "ftp>*"
  12. Send "LCD $dir \ r"
  13. Expect {
  14. "*file" {send_user "local $_dir No such file or directory"; "Send" quit\r "}
  15. "*now*" {send "get $dir/$file $dir/$file \ r"}
  16. }
  17. Expect {
  18. "*failed" {send_user "remote $file No such file"; "Send" quit\r "}
  19. "*ok" {send_user "$file has been download\r"; "Send" quit\r "}
  20. }
  21. Expect EOF

The results of the operation are as follows:

View copy print?
  1. [Email protected]:/home/zhangy#./test2.exp 192.168.1.130/var/www/www aaa.html
  2. Spawn FTP 192.168.1.130
  3. Connected to 192.168.1.130.
  4. (VsFTPd 2.0.5)
  5. Name (192.168.1.130:root): ZWH
  6. 331 Specify the password.
  7. Password:
  8. Successful Login.
  9. Remote system type is UNIX.
  10. Using binary mode to transfer files.
  11. Ftp> lcd/var/www/www
  12. Local Directory Now/var/www/www
  13. Ftp> get/var/www/www/aaa.html/var/www/www/aaa.html
  14. Local:/var/www/www/aaa.html Remote:/var/www/www/aaa.html
  15. PORT command successful. Consider using PASV.
  16. Opening BINARY Mode data Connection for/var/www/www/aaa.html (bytes).
  17. 226 File send OK.
  18. Bytes Received in 0.00 secs (515.6 kb/s)
  19. Quit aaa.html has been download
  20. 221 Goodbye.

Linux expect automatic login ssh,ftp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.