【Linux学习】expect远程下载和上传样例

expect远程下载和上传样例

 1 #!/bin/sh
 2 
 3 if [ $# -ne 7 ];then
 4 exit 8;
 5 fi
 6 dstip="$1";
 7 dstport="$2";
 8 id="$3";
 9 pw="$4";
10 dstpath="$5";
11 localpath="$6";
12 action="$7";
13 if [ ${action} = "upload" ];then
14     command="scp -pP ${dstport} ${localpath} ${id}@${dstip}:${dstpath}";
15 elif [ ${action} = "download" ];then
16     command="scp -pP ${dstport} ${id}@${dstip}:${dstpath} ${localpath}";
17 else
18     exit 9;
19 fi
20 expect -c "set timeout 300;
21             spawn ${command};
22             expect {
23                 \"(yes/no)?\" {
24                     send \"yes\r\";                                                        
25                         expect {
26                             \"*assword:\" {        
27                                 send \"${pw}\r\";
28                                 expect {
29                                     \"100%\" {    
30                                         expect eof;
31                                         exit 0;
32                                         } 
33                                     \"*assword:\" {
34                                         exit 1;
35                                         }
36                                         timeout {exit 5;}
37                                         eof {exit 5;}
38                                     }
39                             }
40                             timeout {exit 3;}
41                             eof {exit 3;}
42                         }                            
43                 }
44                 \"*assword:\" {        
45                     send \"${pw}\r\";
46                     expect {
47                         \"100%\" {    
48                             expect eof;
49                             exit 0;
50                             } 
51                         \"*assword:\" {
52                             exit 1;
53                             }
54                             timeout {exit 5;}
55                             eof {exit 5;}
56                         }
57                 }
58                 timeout { exit 1;}
59                 eof {exit 1;}
60             }
61             "
62 result=$?
63 exit $result

 

posted @ 2020-04-09 18:40  gtea  阅读(353)  评论(0编辑  收藏  举报