linux expect自動登錄ssh,ftp



expect是一種能夠按照腳本內容裏面設定的方式與交互式程序進行“會話”的程序。根據腳本內容,Expect可以知道程序會提示或反饋什麼內容以及 什麼是正確的應答。它是一種可以提供“分支和嵌套結構”來引導程序流程的解釋型腳本語言。

shell功能很強大,但是不能實現有交互功能的多機器之前的操作,例如ssh和ftp.而expect可以幫助我們來實現.

一,安裝expect

  1. yum install expect  
yum install expect

其實expect根bash形勢上差不多的.

二,實例

1,ssh實現自動登錄,並停在登錄服務器上

  1. #!/usr/bin/expect -f  
  2.  set ip [lindex $argv 0 ]     //接收第一個參數,並設置IP  
  3.  set password [lindex $argv 1 ]   //接收第二個參數,並設置密碼  
  4.  set timeout 10                   //設置超時時間  
  5.  spawn ssh root@$ip       //發送ssh請滶  
  6.  expect {                 //返回信息匹配  
  7.  "*yes/no" { send "yes\r"; exp_continue}  //第一次ssh連接會提示yes/no,繼續  
  8.  "*password:" { send "$password\r" }      //出現密碼提示,發送密碼  
  9.  }  
  10.  interact          //交互模式,用戶會停留在遠程服務器上面.  
#!/usr/bin/expect -f
 set ip [lindex $argv 0 ]     //接收第一個參數,並設置IP
 set password [lindex $argv 1 ]   //接收第二個參數,並設置密碼
 set timeout 10                   //設置超時時間
 spawn ssh root@$ip       //發送ssh請滶
 expect {                 //返回信息匹配
 "*yes/no" { send "yes\r"; exp_continue}  //第一次ssh連接會提示yes/no,繼續
 "*password:" { send "$password\r" }      //出現密碼提示,發送密碼
 }
 interact          //交互模式,用戶會停留在遠程服務器上面.

運行結果如下:

  1. root@ubuntu:/home/zhangy# ./test.exp 192.168.1.130 admin  
  2. spawn ssh [email protected]  
  3. Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142  
  4. [root@linux ~]#  
root@ubuntu:/home/zhangy# ./test.exp 192.168.1.130 admin
spawn ssh [email protected]
Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142
[root@linux ~]#

這個例子有統一的接口,根據IP和密碼可以連接到不同的機器.如果你嫌輸入IP和密碼麻煩,看下面的例子

  1. #!/usr/bin/expect -f  
  2.  set ip 192.168.1.130  
  3.  set password admin  
  4.  set timeout 10  
  5.  spawn ssh root@$ip  
  6.  expect {  
  7.  "*yes/no" { send "yes\r"; exp_continue}  
  8.  "*password:" { send "$password\r" }  
  9.  }  
  10.  interact  
#!/usr/bin/expect -f
 set ip 192.168.1.130
 set password admin
 set timeout 10
 spawn ssh root@$ip
 expect {
 "*yes/no" { send "yes\r"; exp_continue}
 "*password:" { send "$password\r" }
 }
 interact

運行結果如下:

  1. root@ubuntu:/home/zhangy# ./web.exp  
  2. spawn ssh [email protected]  
  3. Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142  
  4. [root@linux ~]#  
root@ubuntu:/home/zhangy# ./web.exp
spawn ssh [email protected]
Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142
[root@linux ~]#

2,ssh遠程登錄到服務器,並且執行命令,執行完後並退出

  1. #!/usr/bin/expect -f  
  2.  set ip 192.168.1.130  
  3.  set password admin  
  4.  set timeout 10  
  5.  spawn ssh root@$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  
#!/usr/bin/expect -f
 set ip 192.168.1.130
 set password admin
 set timeout 10
 spawn ssh root@$ip
 expect {
 "*yes/no" { send "yes\r"; exp_continue}
 "*password:" { send "$password\r" }
 }
 expect "#*"
 send "pwd\r"
 send  "exit\r"
 expect eof

運行結果如下:

  1. root@ubuntu:/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. [root@localhost ~]# pwd  
  6. /root  
  7. [root@localhost ~]# exit  
  8. logout  
  9. Connection to 192.168.1.130 closed.  
root@ubuntu:/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
[root@localhost ~]# pwd
/root
[root@localhost ~]# exit
logout
Connection to 192.168.1.130 closed.

3,遠程登錄到ftp,並且下載文件

  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  
#!/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

運行結果如下:

  1. root@ubuntu:/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. 220 (vsFTPd 2.0.5)  
  5. Name (192.168.1.130:root): zwh  
  6. 331 Please specify the password.  
  7. Password:  
  8. 230 Login successful.  
  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. 200 PORT command successful. Consider using PASV.  
  16. 150 Opening BINARY mode data connection for /var/www/www/aaa.html (66 bytes).  
  17. 226 File send OK.  
  18. 66 bytes received in 0.00 secs (515.6 kB/s)  
  19. quit aaa.html has been download  
  20. 221 Goodbye.  



轉載
作者:海底蒼鷹
地址:
http://blog.51yip.com/linux/1462.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章