lawlRedneck

July 8, 2009

sftp/scp scripting

Filed under: nerdery —— elmidgeto @ 4:15 pm

The situation: I need to move files from one server to another using only the OpenSSH tools. All authentication/authorization is done via public key handshakes.

The restrictions: I cannot use expect or any other scripting language beyond shell scripts.

The goal: To minimize the number of characters I need to type and provide a platform for our batch guy to build on for the automated transfers.

And without further ado, have at thee. (Note: I did remove username, sftp connection string, and the root paths from my script to eliminate identification)

#!/bin/bash
######################################################
###### Utility script for Pushing/Pulling files ######
###### from the enterprise sftp solution. ############
######################################################

##################################################
############# SETTING THE VARIABLES ##############
##################################################

TYPE=$1
FILENAME=$2

USER=”"
SFTP=”"
BASEPATH=”"

SFTPPATH=$BASEPATH
LOCALPATH=`pwd`

MAX=$#
ARGS=(“$@”)

for ((COUNT=2; COUNT < MAX; COUNT++))
do
 if [ ${ARGS[$COUNT]} == “-s” ];then
      SFTPPATH=$SFTPPATH/${ARGS[$COUNT+1]}
      COUNT=$COUNT+1
 
 elif [ ${ARGS[$COUNT]} == “-l” ];then
      LOCALPATH=${ARGS[$COUNT+1]}
      COUNT=$COUNT+1 
 fi
done

##################################################
########### COPY FROM THE SFTP SERVER ############
##################################################

if [ $TYPE == "GET" ]; then
     scp $USER@$SFTP:$SFTPPATH/$FILENAME $LOCALPATH/$FILENAME

##################################################
############ COPY TO THE SFTP SERVER #############
##################################################

elif [ $TYPE == "PUT" ]; then
     scp $LOCALPATH/$FILENAME $USER@$SFTP:$SFTPPATH/$FILENAME

##################################################
########## DELETE FROM THE SFTP SERVER ###########
##################################################

elif [ $TYPE == "DEL" ]; then
     sftp -b /dev/stdin $USER@$SFTP <<INP
     cd $SFTPPATH
     rm $FILENAME
     ls
     bye
INP

##################################################
############### SCRIPT ARGS HELP #################
##################################################

elif [ $TYPE == "-h" ]; then
     echo “  sftp_util syntax is:”
     echo “  ./sftp_util.sh <GET|PUT|DEL|-h> <FileName> [-s] [-l]”
     echo “  GET GET is used to retrieve a file from the remote server.”
     echo “  PUT PUT is used to place a file on the remote server.”
     echo “  DEL DEL is used to delete a file from the remote server.”
     echo “  -h -h is used to print this help menu.”
     echo “  -s -s <sftp server path of/to file, do not include a leading ‘/’ (ex. test/test2)>”
     echo “  -l -l <sftp server path of/to file>”
 
else
     echo “Please use ./sftp_util.sh -h for help”
fi

Usage goes a little something like this:

server1$> ./sftp_util.sh PUT file -s test/empty_dir -l /home/user

server2$> ./sftp_util.sh GET file -s test/empty_dir -l /here

server2$ ./sftp_util.sh DEL file -s test/empty_dir

 

Nothing overly fancy, just a quick little script to save me time and have a bit of fun on a slow work day.

9 Responses to “sftp/scp scripting”

  1.   opello Says:

    WordPress fancified some of your quotes.

    For the ‘delete’ action you could just use:
    ssh $USER@$SFTP rm $FILENAME
    At least under simple configurations. If you’re jailing sftp connections to a specific path keeping it all sftp is a better plan.

    Since you are invoking it bash up front (sh has it too as I recall, but not csh/etc.) you can use getopts, which is a builtin. Instead of a for loop to iterate over your arguments.
    while getopts “s:l:” flag
    do
    case “$flag” in
    s)
    SFTPPATH=$OPTARG
    ;;

    l)
    LOCALPATH=$OPTARG
    ;;
    esac
    done
    (As long as you shift off the TYPE and FILENAME arguments anyway.)

  2.   elmidgeto Says:

    This was a simple, quick, and dirty script to get what I needed done.
    Though I really do like your getopts call.

    The delete action is set up the way it is because I am piggy backing on an existing production system, so the folders I need to use are not the standard folders for the user. The sftp server is an enterprise server, so a lot of the actions/permissions on it are folder to folder and user to user so there is nothing jailed, just a bunch of crazy ACLs.

  3.   Richelle Says:

    hello…

    really good article. Ready to hear more next week,my blog http://darknile.com/mycom/blogs/posts/Markcarl Many Thanks….

  4.   Dehmer Says:

    really good article…

    I have spent a bit of time going through your posts, more than I should have but I must say, its worth it! http://baihe071.sexusblog.com/2011/06/13/professional-photographers/ many Thanks….

  5.   Ehantelle Says:

    hello…

    Hello there thanks for the quality post! http://alexs11.over-blog.com/ ,i’d a great read.thank you for your article,My problem continues to be resolved….

  6.   Kugenie Says:

    very helpful…

    I preferred to thank you for this good article. http://bbzgm.honmag.com/ I by all odds liked every little bit of it…

  7.   kander Says:

    Great…

    You did a great job! http://demetria.blogcenter.de/2011/06/17/a-musing-near-to-the-achievement-of-muse/...

  8.   TadWinett Says:

    Great…

    You did a great job! http://dena.bloginthedark.com/2011/06/19/the-bronze-age/...

  9.   Khantelle Says:

    Great…

    love your blog, http://phlog.net/users/jean11/entries/1725831 ,Thanks again….

Leave a Reply

lawlRedneck is proudly powered by WordPress running on http://blogs.dootdoot.com.
Create a new blog and join in the fun! Entries (RSS) and Comments (RSS).