透過 ftp -n 搭配指令檔,實現無人值守 FTP 登入、上傳/下載、斷點續傳。適用於備份腳本、CI/CD 部署、資料同步。
open 192.168.1.100
user myname mypassword
binary
hash
cd /remote/path
get remote_file local_file
put local_file remote_file
bye
說明:
-n:關閉自動登入,由指令檔控制。binary:二進位模式,避免文字檔換行符轉換。hash:顯示 # 進度條。passive(可選):若伺服器需被動模式,加入此行。cat ftp.txt | ftp -n
或寫入 Shell 腳本:
#!/bin/sh
ftp -n <<EOF
open 192.168.1.100
user myname mypassword
binary
cd /backup
put $(date +%F).tar.gz
bye
EOF
lftp -u user,pass -e "mirror -R /local/dir /remote/dir; quit" ftp://192.168.1.100
~/.lftp/rc 設定預設參數。chmod 600 ftp.txt。sftp 或 rsync over SSH。