1. 程式人生 > >通過expect執行scp,傳輸檔案不完整的解決方法

通過expect執行scp,傳輸檔案不完整的解決方法

寫了一個指令碼來傳輸檔案,類似於這樣:

sendsystem(){
expect -c "
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/system01.dbf oracle@$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
expect eof ; " } sendsysaux(){ expect -c " spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/sysaux01.dbf oracle@$S_IP:$ORACLE_BASE/oradata/standby/ expect { yes/no { send \"yes\r\"; exp_continue } *assword* { send \"oracle\r\" } }
; expect 100% expect eof ; " }

結果:

spawn scp /home/oracle/std_control01.ctl oracle@192.168.134.134:/oracle/oradata/standby/std_control01.ctl
oracle@192.168.134.134's password: 
std_control01.ctl                                                                               100% 9520KB   9.3MB/s   00:00    
spawn scp /oracle/oradata/orcl/system01.dbf [email protected]:/oracle/oradata/standby/
[email protected]'
s password: system01.dbf 92% 646MB 33.9MB/s 00:01 ETA spawn scp /oracle/oradata/orcl/sysaux01.dbf oracle@192.168.134.134:/oracle/oradata/standby/ oracle@192.168.134.134's password: sysaux01.dbf 100% 600MB 31.6MB/s 00:19 spawn scp /oracle/oradata/orcl/temp01.dbf [email protected]:/oracle/oradata/standby/ [email protected]'s password: temp01.dbf 100% 200MB 66.7MB/s 00:03 spawn scp /oracle/oradata/orcl/undotbs01.dbf oracle@192.168.134.134:/oracle/oradata/standby/ oracle@192.168.134.134's password: undotbs01.dbf 100% 200MB 40.0MB/s 00:05

system01.dbf沒傳完就不傳了
又試了幾次發現偶爾其他檔案也會傳不完就不傳了

最後發現是因為expect預設timeout為30S
手動新增set timeout -1設定超時時間為無窮大,問題解決

sendsystem(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/system01.dbf oracle@$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}