Last active
July 13, 2022 18:58
-
-
Save MContagious/5b9ae6baeceae1b76f3498814592f900 to your computer and use it in GitHub Desktop.
sftp command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/env bash | |
# | |
# pasScp.sh is a sftp command line bash script to transfer localfiles to remote host using sshpass command. | |
# | |
function usage { | |
echo "usage: " | |
echo "sftp --user <username> --host <remotehost> -i <identifier-passwordfile> -f|--file <localfile> --path <remote_location> --help --verbose" | |
echo " --user|-u user name to use to login to sftp" | |
echo " --host|-h remote host ip or name" | |
echo " --passfile|-i passwordfile" | |
echo " --file|-f local file - source" | |
echo " --path|-p remote path - destination" | |
echo " --verbose|-v verbose logging" | |
echo " --quite|-q quite execution - supress logging" | |
echo " --help To print this help message" | |
} | |
function assert_param { | |
local param=$1 | |
local value=$2 | |
if [ "${value}" == "" ]; then | |
echo " ${param} is missing" | |
usage && exit 1 | |
fi | |
return 1 | |
} | |
if [ $# -gt 0 ]; then | |
echo "" | |
else | |
echo "Your command line contains no arguments" | |
usage && exit 1 | |
fi | |
verbose=0 | |
quite=0 | |
# | |
# Parse the command line arguments. | |
# | |
while [ "$1" != "" ]; do | |
case $1 in | |
-u | --user ) shift | |
username=$1 | |
;; | |
-h | --host ) shift | |
host=$1 | |
;; | |
-i | --passfile ) shift | |
passfile=$1 | |
;; | |
-f | --file ) shift | |
localfile=$1 | |
;; | |
-p | --path ) shift | |
remotefile=$1 | |
;; | |
-d | --dir ) shift | |
remotedir=$1 | |
;; | |
-v | --verbose ) verbose=1 | |
;; | |
-q | --quite ) quite=1 | |
;; | |
-h | --help ) usage | |
exit | |
;; | |
* ) usage | |
exit 1 | |
esac | |
shift | |
done | |
missing=0 | |
if [ $quite -eq 0 ]; then | |
echo "======================================================================" | |
fi | |
assert_param "--user" $username | |
assert_param "--host" $host | |
assert_param "--passfile" $passfile | |
assert_param "--file <localfile>" $localfile | |
if [ "$remotefile" == "" ]; then | |
basefile=$(basename $localfile) | |
remotefile="${remotedir}/${basefile}" | |
fi | |
assert_param "--path <remote location>" $remotefile | |
# | |
# Now we have all required parameters to transfer the file to remote host. | |
# | |
extraArgs="" | |
if [ $verbose -gt 0 ]; then | |
extraArgs="-v" | |
fi | |
if [ $quite -eq 0 ]; then | |
echo "INFO: $(date +"%x %X") $localfile Transfer Started at $(date +"%x %X")" | |
echo "INFO: $(date +"%x %X") sshpass -f ****** scp ${extraArgs} ${localfile} ${username}@${host}:${remotefile}" | |
fi | |
sshpass -f ${passfile} scp ${extraArgs} ${localfile} ${username}@${host}:${remotefile}.tmp | |
sshpass -f ${passfile} ssh ${extraArgs} ${username}@${host} mv ${remotefile}.tmp ${remotefile} | |
localFileSize=$(stat --format "%s" $localfile) | |
remoteFileSize=$(sshpass -f ${passfile} ssh ${extraArgs} ${username}@${host} stat --format "%s" $remotefile) | |
if [ "$remoteFileSize" == "" ]; then | |
echo "ERROR: $(date +"%x %X") Failed to copy $localfile to remotelocation($remotefile)" | |
exit 2 | |
fi | |
if [ $localFileSize -ne $remoteFileSize ]; then | |
echo "INFO: $(date +"%x %X") LocalFileSize ==> $localFileSize" | |
echo "INFO: $(date +"%x %X") RemoteFileSize ==> $remoteFileSize" | |
echo "ERROR: $(date +"%x %X") Mismatch in local and remote file sizes." | |
echo exit 2 | |
elif [ $quite -eq 0 ]; then | |
echo "INFO: $(date +"%x %X") LocalFileSize ==> $localFileSize" | |
echo "INFO: $(date +"%x %X") RemoteFileSize ==> $remoteFileSize" | |
echo "SUCC: $(date +"%x %X") Matched file sizes." | |
echo "INFO: $(date +"%x %X") $localfile Transfer Completed at $(date +"%x %X")" | |
echo "INFO: $(date +"%x %X") File transferred successfully." | |
fi | |
if [ $quite -eq 0 ]; then | |
echo "======================================================================" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment