需求:
將/home/log/下的所有.txt后綴的文件上傳至sftp服務器中
方法:
1 #!/bin/bash 2 3 logdir="/home/log/" 4 sftpremote='172.29.251.11' 5 sftp_user='collection_log' 6 stp_port='22' 7 sftp_password='hello23+' 8 9 cd $logdir 10 11 /usr/bin/expect << EOF 12 set timeout 20 13 spawn sftp -P $stp_port ${sftp_user}@${sftpremote} 14 expect { 15 "yes/no" { 16 send "yes\r" 17 exp_continue 18 } 19 "assword:" { 20 send "$sftp_password\r" 21 } 22 } 23 24 expect "sftp> " 25 send "cd log\r" 26 expect "sftp> " 27 send "put *.txt\r" 28 expect "sftp> " 29 send "bye" 30 EOF