首页 > 代码库 > expect简单用法

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