Skip to content

Instantly share code, notes, and snippets.

@suuhm
Last active April 19, 2024 09:19
Show Gist options
  • Save suuhm/9a50536956558da6a2b19810977bb791 to your computer and use it in GitHub Desktop.
Save suuhm/9a50536956558da6a2b19810977bb791 to your computer and use it in GitHub Desktop.
FTP Download , Extracting files and Antivirus scan on linux
#!/bin/bash
#
# "========================="
# "FTP Directory Downloader "
# " ----------------------- "
# " Xtrtact & AV scanning "
# " ----------------------- "
# " (c) 2024 by suuhmer "
# "========================="
# VARS:
ftp_url=$1
file_extension=$2
OUTDIR=/mnt/ftpoutdir/
clear
echo "========================="
echo "FTP Directory Downloader "
echo " ----------------------- "
echo " Xtrtact & AV scanning "
echo " ----------------------- "
echo " (c) 2024 by suuhmer "
echo "========================="
echo;sleep 1
if [ -z "$1" ]; then
echo "No URL provided. Usage: $0 <ftp://URL/ftpdir/> <file_extension>"
exit 1
fi
# Check if a file extension was provided as an argument
if [ -z "$2" ]; then
echo "No file extension provided. Usage: $0 <ftp://URL/ftpdir/> <file_extension>"
exit 1
fi
#
## MAIN
#
echo;echo "[?] Select the authentication method:"
echo "1. With username and password"
echo "2. Anonymous"
echo
read -p "Choice (1 or 2): " auth_choice
# Set authentication parameters based on user choice
auth_params=""
if [ "$auth_choice" == "1" ]; then
read -p "Username: " username
read -sp "Password: " password
echo
auth_params="--user $username:$password"
elif [ "$auth_choice" == "2" ]; then
auth_params="--user anonymous:anonymous"
else
echo "Invalid selection. Exiting script."
exit 1
fi
echo;echo "Connecting to $ftp_url with selected authentication method."
# List directory contents and save subdirectories
echo "Loading directory list..."
subdirs=$(curl -sl $auth_params $ftp_url)
echo; echo $subdirs ; echo; sleep 2
# Check if subdirectories are available
if [ -z "$subdirs" ]; then
echo "No subdirectories found or access denied."
exit 1
fi
# Ask the user if they want to download *.fq.gz files from the directory
echo
read -p "[?] Do you want to download *.$file_extension files from the directory? (y/n) " answer
if [[ $answer =~ ^[Yy]$ ]]; then
# Create a download directory if it does not exist
mkdir -p ftp_downloads
# Loop through each subdirectory
for subdir in $subdirs; do
echo;echo "[*] Searching for .$file_extension files in $subdir..."
# List all .fq.gz files in the subdirectory and download them
curl -sl $auth_params "${ftp_url}${subdir}/" | grep "\.$file_extension$" | while read filename; do
echo "[~] Downloading $filename..."
curl -f $auth_params "${ftp_url}${subdir}/${filename}" -o "ftp_downloads/${filename}" --globoff
done
done
echo;echo "[!] Downloads completed. Files are located in ./ftp_downloads/"
else
echo;echo "[!] No downloads performed."
fi
# Extracting files:
echo
read -p "[?] Do you want to extrtact *.$file_extension files to $OUTDIR? (y/n) " answer
if [[ $answer =~ ^[Yy]$ ]]; then
# Create a dir for outfiles
mkdir -p $OUTDIR
for FILE in *.$file_extension ; do
echo -n "Extract File $FILE... "
gzip -c $FILE > ${FILE%.gz}
echo "Done"
done
echo;echo "[!] Xtrtact completed. Files are located in $OUTDIR"
else
echo;echo "[!] Bye"; exit 0
fi
echo
read -p "[?] Do you want to scan all files in $OUTDIR? (y/n) " answer
if [[ $answer =~ ^[Yy]$ ]]; then
clamscan $OUTDIR/*
echo;echo "[!] Scan completed in $OUTDIR"
else
echo;echo "[!] Bye"; exit 0
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment