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 factj@www.factj.com
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 root@www.factj.com
03
root@www.factj.com'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.
 

相关内容

    暂无相关文章