expect简单用法

 1 #!/usr/expect/bin/expect -f
 2 
 3 
 4 set loginuser [lrange $argv 0 0]
 5 set loginpass [lrange $argv 1 1]
 6 set ipaddr [lrange $argv 2 2]
 7 set port   [lrange $argv 3 3]
 8 set timeout [lrange $argv 4 4]
 9 set from [lrange $argv 5 5]
10 set to [lrange $argv 6 6]
11 set cmd_prompt "#|~|]"
12 
13 if { $loginuser == "" || $loginpass == "" || $ipaddr == "" || $port == "" || $timeout == "" || $from == "" || $to == "" }  {
14       puts "Usage: <user> <password> <ip> <port> <timeout> <from> <to> \n"
15       exit 1
16 }
17 #-------------------------------------------------- 
18 if { [file isfile $from ]==1 } { 
19     spawn scp -P $port $from $loginuser@$ipaddr:$to 
20 } else {
21     spawn scp -P $port -r $from $loginuser@$ipaddr:$to 
22 }
23 set timeout $timeout
24 
25 expect {
26 -re "assword:" {
27 send "$loginpass\r"
28 } -re "Permission denied, please try again." {
29 puts "\nERROR_PASSWORD"
30 exit
31 } -re "port $port: Connection refused" {
32 puts "ERROR_PORT"
33 exit
34 } -re "Connection closed" {
35 puts "ERROR_UNKNOW"
36 exit
37 } timeout {
38 puts "ERROR_TIMEOUT_OR_IP"
39 exit
40 } eof {
41 puts "ERROR_UNKNOW"
42 exit
43 }
44 }
45 
46 expect {
47 -re "assword:" {
48 puts "\nERROR_USERNAME_OR_PASSWORD"
49 exit
50 } -re "100%" {
51 puts "ERROR_NONE"
52 }
53 }
54 
55 #--------------------------------------------------
56 expect {
57 -re $cmd_prompt {
58 send "exit\r"
59 }
60 }
61 expect eof
62 
63 exit
64 #interact

 

转载于:https://www.cnblogs.com/gdutzzh/p/3726407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值