SSH Download Script

Posted by zbrom on August 01, 2013 · 2 mins read

This script allows you to download a file from a remote SSH server and verify the md5sum to ensure no corruption during transfer. Feel free to use it as is or modify it to your liking.


Copy the code into your favorite text editor.

Code:

#! /bin/bash
# www.ghorr.org - 2013

#ssh server to download from (e.g., server=user@127.0.0.1)
#path on remote host
echo ""
echo "Input the path you wish to download from."
read "path1"
#listing files in above path and choosing the file to download
ssh $server ls $path1

echo ""
echo "Choose the file you wish to download."
read "file"

#path on local host
echo ""
echo "Input the destination directory."
read "path2"

scp $server:/$path1/$file $path2/

#md5sum
echo ""
echo "Would you like to run an md5sum? (Y/n)"
read "run"

if [ "$run" = "Y" ]
then
#collecting the md5sums for the downloaded file
cd $HOME
md5sumfile1=$(ssh $server md5sum $path1/$file | cut -c -32)
cd $HOME
cd $path2
md5sumfile2=$(md5sum $file | cut -c -32)

#displaying the two md5sums
echo ""
cd $HOME
echo $(ssh $server md5sum $path1/$file)
echo ""
cd $HOME
cd $path2
echo $(md5sum $file)

#determining if the two md5sums match
if [ "$md5sumfile1" = "$md5sumfile2" ]
then
echo "SUCCESS, md5sums match"
else
echo "md5sums do NOT match"
fi

else
exit
fi

You first need to define the server you wish to use, modify the server variable to your liking.

server=user@127.0.0.1)

Save your file and place it in a directory that is easily accessible. I currently have mine placed in the bin folder in my home directory. You can also place it in the /usr/bin directory if you like. Ensure you save the script with UTF-8 encoding.

Make the script executable by running the following command in terminal, with the correct path to the script.

chmod +x [path to file]