72 lines
1.4 KiB
Bash
72 lines
1.4 KiB
Bash
#!/bin/sh
|
|
|
|
# Add signal handling
|
|
cleanup() {
|
|
echo "Received shutdown signal, stopping FTP client..."
|
|
exit 0
|
|
}
|
|
|
|
# Trap SIGTERM and SIGINT
|
|
trap cleanup SIGTERM SIGINT
|
|
|
|
echo "Starting FTP client script..."
|
|
|
|
# FTP server details (from environment variables with defaults)
|
|
FTP_HOST="${FTP_HOST:-ftp-server}"
|
|
FTP_PORT="${FTP_PORT:-21}"
|
|
FTP_USER="${FTP_USER:-anonymous}"
|
|
FTP_PASS="${FTP_PASS:-anonymous}"
|
|
|
|
echo "Configuration:"
|
|
echo " FTP_HOST: $FTP_HOST"
|
|
echo " FTP_PORT: $FTP_PORT"
|
|
echo " FTP_USER: $FTP_USER"
|
|
echo " FTP_PASS: $FTP_PASS"
|
|
echo ""
|
|
|
|
|
|
while true; do
|
|
echo "Executing FTP commands..."
|
|
|
|
lftp -d -u $FTP_USER,$FTP_PASS $FTP_HOST <<EOF
|
|
set ftp:ssl-allow no
|
|
set cmd:fail-exit yes
|
|
set net:timeout 10
|
|
set net:max-retries 3
|
|
ls -la
|
|
echo 'Changing to pub directory:'
|
|
cd pub
|
|
echo 'Listing pub directory:'
|
|
ls -la
|
|
echo 'Downloading welcome.txt:'
|
|
get welcome.txt
|
|
echo 'Download completed!'
|
|
quit
|
|
EOF
|
|
|
|
echo "FTP operations completed!"
|
|
|
|
# Force stdout flush
|
|
sync
|
|
|
|
# Show downloaded file if it exists
|
|
if [ -f "welcome.txt" ]; then
|
|
echo "Downloaded file contents:"
|
|
echo "===================="
|
|
cat welcome.txt
|
|
echo ""
|
|
echo "===================="
|
|
|
|
rm -f welcome.txt
|
|
echo "welcome.txt has been removed after download."
|
|
else
|
|
echo "Warning: welcome.txt was not downloaded"
|
|
fi
|
|
|
|
sync
|
|
|
|
echo "Waiting 5 seconds before next FTP operation..."
|
|
sleep 5 &
|
|
wait $!
|
|
done
|