Scripts/sftp_transfer
2025-05-08 01:47:17 +00:00

82 lines
2.2 KiB
Bash

#!/bin/bash
# wget https://git.technozone.com.au/vijay/Scripts/raw/branch/main/sftp_transfer -O sftp_transfer.sh && chmod +x sftp_transfer.sh && ./sftp_transfer.sh && rm sftp_transfer.sh
# Function to display the menu
show_menu() {
echo "=============================="
echo " SFTP Transfer Menu "
echo "=============================="
echo "1. Zip a folder"
echo "2. Transfer zipped file to remote server"
echo "3. Exit"
echo "=============================="
}
# Function to zip a folder
zip_folder() {
read -p "Enter the folder path to zip: " folder_path
read -p "Enter the name for the zipped file (without .zip): " zip_name
if [ -d "$folder_path" ]; then
zip -r "${zip_name}.zip" "$folder_path"
echo "Folder zipped successfully as ${zip_name}.zip"
else
echo "Error: Directory does not exist."
fi
}
# Function to list available zip files
list_zip_files() {
echo "Available zip files:"
ls *.zip 2>/dev/null
}
# Function to transfer the zipped file to a remote server
transfer_file() {
read -p "Enter the remote server username: " username
read -p "Enter the remote server address: " server_address
read -p "Enter the remote directory to upload the file: " remote_dir
read -sp "Enter the password for the remote server: " password
echo
# List available zip files
list_zip_files
# Prompt user to select a zip file
read -p "Enter the name of the zipped file to transfer (with .zip): " zip_name
if [ -f "$zip_name" ]; then
# Use sshpass to provide the password for SFTP
sshpass -p "$password" sftp -v "${username}@${server_address}" <<EOF
put "$zip_name" "$remote_dir"
bye
EOF
echo "File transferred successfully to ${username}@${server_address}:${remote_dir}"
else
echo "Error: Zipped file does not exist."
fi
}
# Main script loop
while true; do
show_menu
read -p "Select an option [1-3]: " option
case $option in
1)
zip_folder
;;
2)
transfer_file
;;
3)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
done