Shell脚本学习之expect命令(6)

五、expect实用案例

1、expect实现ssh无密钥登陆

说明:用了两个脚本,一个bash脚本(send_key.sh),在其中调用另外一个expect脚本(scp_key_to_node.exp),两个脚本放在同一个目录下:

(1)bash脚本:send_key.sh

#!/bin/bash 
ssh-keygen -t dsa 
for (( i = 1; i <= 100 ; i ++ )) 
do 
  ./scp_key_to_node.exp $i 
done 

(2)expect脚本:(scp_key_to_node.exp)

#!/usr/bin/expect 
set timeout 5 
set hostno [lindex $argv 0] 
spawn scp ~/.ssh/id_dsa.pub impala$hostno:~/.ssh/pub_key 
expect "*password*" 
send "111111\r" 
spawn ssh impala$hostno "cat ~/.ssh/pub_key/ >> ~/.ssh/authorized_keys" 
expect "*password*" 
send "111111\r" 
spawn ssh impala$hostno "chmod 600 ~/.ssh/authorized_keys" 
expect "*password*" 
send "111111\r" 
expect eof 

(3)分析:
set可以设置超时,或者设置一个变量的值
spawn是执行一个命令
expect等待一个匹配的输出流中的内容
send是匹配到之后向输入流写入的内容
[lindex $argv 0]表示脚本的第0个参数
expect eof表示读取到文件结束符
(4)脚本执行方式:
在脚本所在的目录下执行:
# ./send_key.sh 

2、ssh实现自动登录,并停在登录服务器上

#!/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          //交互模式,用户会停留在远程服务器上面.

运行结果如下:

root@Ubuntu:/home/zhangy# ./test.exp 192.168.1.130 admin
spawn ssh root@192.168.1.130
Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142
[root@linux ~]#

3、根据IP和密码连接到不同的机器.

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

运行结果如下:

root@ubuntu:/home/zhangy# ./web.exp
spawn ssh root@192.168.1.130
Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142

4、远程登录到服务器,并且执行命令,执行完后并退出

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

运行结果如下:

root@ubuntu:/home/zhangy# ./test3.exp
spawn ssh root@192.168.1.130
root@192.168.1.130'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.

5、远程登录到ftp,并且下载文件

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

运行结果如下:

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/4d21e88bf7fde00ac72f360d0d64aea8.html