Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #The following script will look at the folder you specify
- #Determine what files are in that folder
- #How much space that they will take up
- #And copy them to a remote location of your choosing
- #TITLE="SFTP File Transfer"
- #Print the string variable on the screen
- echo $TITLE
- echoPS3='Choose how you would like to transfer files:'
- options=("SFTP Push" "SFTP Get" "Quit")
- quit=false
- until $quit; do
- select opt in "${options[@]}"; do
- case $opt in
- "SFTP Push")
- clear;
- #SFTP Push
- echo "Use this option to move files from a local folder"
- echo "to a remote folder"
- echo
- #
- echo -e "What is the server IP?"
- read ipaddress
- #
- echo -e "What is the username?"
- read user
- #
- echo -e "What is the password?"
- read pass
- #
- echo -e "What is the local folder location?"
- read local
- #
- echo -e "What is the remote folder location?"
- read remote
- #
- #Place files in the remote folder
- #Since we are using sftp, I set a variable of port
- #to hard code port 22.
- #
- #The find command will locate
- #all files in the folder stated in variable $local
- #and place them into a file called filelist.
- #
- #Now for the meat of this script. We need to make sure
- #that "expect" is installed as it will help us to send
- #what sftp is needing from us automate the process.
- #
- #sftp will launch and with the variables collected above
- #to get the files copied from local to remote locations.
- #
- PORT=22
- find $local -type f > filelist
- /usr/bin/expect <<EOD
- spawn /usr/bin/sftp -o Port=$PORT $user@$ipaddress
- expect "password:"
- send "$pass\r"
- expect "sftp>"
- send "put -r $local/* $remote\r"
- expect "sftp>"
- send "bye\r"
- EOD
- break
- ;;
- "SFTP Get")
- clear;
- #SFTP Get
- echo "Use this option to move files from a Remote folder"
- echo "to a local folder"
- echo
- #
- echo -e "What is the server IP?"
- read ipaddress
- #
- echo -e "What is the username?"
- read user
- #
- echo -e "What is the password?"
- read pass
- #
- echo -e "What is the local folder location?"
- read local
- #
- echo -e "What is the remote folder location?"
- read remote
- #
- #Place files in the local folder
- #Since we are using sftp, I set a variable of port
- #to hard code port 22.
- #
- #The find command will locate
- #all files in the folder stated in variable $remote
- #and place them into a file called filelist.
- #
- #Now for the meat of this script. We need to make sure
- #that "expect" is installed as it will help us to send
- #what sftp is needing from us automate the process.
- #
- #sftp will launch and with the variables collected above
- #to get the files copied from local to remote locations.
- #
- PORT=22
- find $remote -type f > filelist
- /usr/bin/expect <<EOD
- spawn /usr/bin/sftp -o Port=$PORT $user@$ipaddress
- expect "password:"
- send "$pass\r"
- expect "sftp>"
- send "put -r $remote/* $local\r"
- expect "sftp>"
- send "bye\r"
- EOD
- break
- ;;
- "Quit")
- quit=true
- break
- ;;
- *) echo invalid option
- ;;
- esac
- done
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement