歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

shell補充工具-expect

shell補充工具-expect   linux 下面,在shell編程裡面,有些程序的輸入是需要接收tty的輸入而不是stdin的輸入,如果需要一種實現,那麼就是expect。    一,安裝expect 1 yum install expect   二,實例 1,ssh實現自動登錄,並停在登錄服務器上   01 #!/usr/bin/expect -f 02  set ip www.factj.com 03  set password [lindex $argv 0 ]   //接收第一個參數,並設置密碼 04  set timeout 10                   //設置超時時間 05  spawn ssh root@$ip       //發送ssh請滶 06  expect {                 //返回信息匹配 07  "*yes/no" { send "yes\r"; exp_continue}  //第一次ssh連接會提示yes/no,繼續 08  "*password:" { send "$password\r" }      //出現密碼提示,發送密碼 09  } 10  interact          //交互模式,用戶會停留在遠程服務器上面.    運行結果如下: 1 root@factj:/home/factj# ./test.exp factj 2 spawn ssh root@w<span></span> Last login: Fri Sep  7 10:47:43 2013 from www.factj.com 3 [root@linux ~]#   這個例子有統一的接口,根據IP和密碼可以連接到不同的機器.如果你嫌輸入IP和密碼麻煩,看下面的例子   01 #!/usr/bin/expect -f 02  set ip www.factj.com 03  set password factj 04  set timeout 10 05  spawn ssh root@$ip 06  expect { 07  "*yes/no" { send "yes\r"; exp_continue} 08  "*password:" { send "$password\r" } 09  } 10 interact 運行結果如下:  1 root@ubuntu:/home/zhangy# ./web.exp 2 spawn ssh [email protected] 3 Last login: Fri Agu  7 12:59:02 2013 from www.factj.com 4 [root@linux ~]# 2,ssh遠程登錄到服務器,並且執行命令,執行完後並退出  01 #!/usr/bin/expect -f 02  set ip www.factj.com 03  set password factj 04  set timeout 10 05  spawn ssh root@$ip 06  expect { 07  "*yes/no" { send "yes\r"; exp_continue} 08  "*password:" { send "$password\r" } 09  } 10  expect "#*" 11  send "pwd\r" 12  send  "exit\r" 13  expect eof  運行結果如下:  01 root@ubuntu:/home/zhangy# ./test3.exp 02 spawn ssh [email protected] 03 [email protected]'s password: 04 Last login: Fri agu 7 14:05:07 2013 from 116.246.27.90 05 [root@localhost ~]# pwd 06 /root 07 [root@localhost ~]# exit 08 logout 09   10 Connection to www.factj.com closed. 3,遠程登錄到ftp,並且下載文件  01 #!/usr/bin/expect -f 02  set ip [lindex $argv 0 ] 03  set dir [lindex $argv 1 ] 04  set file [lindex $argv 2 ] 05  set timeout 10 06  spawn ftp $ip 07  expect "Name*" 08  send "zwh\r" 09  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  運行結果如下:  01 root@ubuntu:/home/zhangy# ./test2.exp www.factj.com /var/www/www aaa.html 02 spawn ftp www.factj.com 03 Connected to www.factj.com. 04 220 (vsFTPd 2.0.5) 05 Name (www.factj.com:root): zwh 06 331 Please specify the password. 07 Password: 08 230 Login successful. 09 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.  
Copyright © Linux教程網 All Rights Reserved