LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to sftp file to remote server (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-sftp-file-to-remote-server-509942/)

OzTEXS 12-13-2006 02:14 AM

script to sftp file to remote server
 
Hi All,

I am looking to setup a script on my Redhat distro that will transfer a file to a remote server via sftp. I have already added a private key from the remote server to my box using ssh-add and I have tested the connection without having to enter a password.

This is how I would like to do it;

1. take a file from a directory /home/user/latest_file
(the file name is always changing i.e file_120306)

2. mv the newly transfered file to a archive dir /home/user/archive

3. include this into my crontab for checking 4 times a day.

Anyone who can help me with this, it would be greatly appreciated.

Cheers !!

cmsps 12-13-2006 11:05 AM

Here are the scripts I use for that job. I don't know if they work with Open-SSH but they do work with SSH.com's SSH. First, putting files onto another server:

Code:

#!/bin/sh
#
# SFTPput [user@]host directory file ... - (secure) FTP files in dir from host
#
# Sat Mar 11 11:18:20 GMT 2006
#


# usage - display usage message
#
usage () {
  echo "Usage: $NAME [user@]host dir file ..." >&2
  exit 1
}


# mkTmp - make temp dir and delete it automatically on exit
#        Eg: ... > $TMP/temp
#
mkTmp() {
  TMP=/tmp/$NAME.$$
  trap 'rm -r $TMP > /dev/null 2>&1' 0 1 2 3 4 5 6 7 8 9 10    12 13 14 15
  mkdir $TMP
}


# checkCurrent - check IP adress for home is today's
#
checkCurrent () {
  set `date`
  today=$2$3
  set -- `ls -l $HOME/var/home`
  stored=$6$7
  timeORyear=$8
  case $timeORyear in
      ??:??)
            test $today = $stored || old
            ;;
      *)
            old
            ;;
  esac
}


# old - say it's old
#
old () {
  printf "$NAME: home's IP older than today\n" >&2
  exit 3
}


NAME=`basename $0`
if [ $# -lt 3 ]
then usage
else host=$1
    dir=$2
    shift 2
fi
case $host in
    *@home)
          checkCurrent
          sedCmd="s/home/`cat $HOME/var/home`/"
          host=`echo $host | sed "$sedCmd"`
          ;;
    home)
          checkCurrent
          host=`cat $HOME/var/home`
          ;;
    *@office)
          host=`echo $host |
            sed 's/office/cms-2313-02.cms.shu.ac.uk/'`
          ;;
    office)
          host=cms-2313-02.cms.shu.ac.uk
          ;;
esac
case $host in
    *@*)
          ;;
    *)
          host=cmsps@$host
          ;;
esac
mkTmp
{
    printf "binary\ncd $dir\n"
    for file
    do  if [ -f $file ]
          then echo put $file
          else echo "$NAME: $file: doesn't exist (skipped)" >&2
          fi
    done
    echo quit
} > $TMP/commands
sftp -B $TMP/commands $host > $TMP/errors
return=$?
case $return in
    0)
          exit
          ;;
    2)
          echo $NAME: $dir: no such directory >&2
          exit $return
          ;;
    *)
          echo $NAME: ERROR $return >&2
          cat $TMP/errors >&2
          exit $return
esac

Here is its partner:
Code:

#!/bin/sh
#
# SFTPget [user@]host directory file ... - (secure) FTP files in dir from host
#
# Sat Mar 11 11:28:20 GMT 2006
#


# usage - display usage message
#
usage () {
  echo "Usage: $NAME [user@]host dir file ..." >&2
  exit 1
}


# mkTmp - make temp dir and delete it automatically on exit
#        Eg: ... > $TMP/temp
#
mkTmp() {
  TMP=/tmp/$NAME.$$
  trap 'rm -r $TMP > /dev/null 2>&1' 0 1 2 3 4 5 6 7 8 9 10    12 13 14 15
  mkdir $TMP
}


# checkCurrent - check IP adress for home is today's
#
checkCurrent () {
  set `date`
  today=$2$3
  set -- `ls -l $HOME/var/home`
  stored=$6$7
  timeORyear=$8
  case $timeORyear in
      ??:??)
            test $today = $stored || old
            ;;
      *)
            old
            ;;
  esac
}


# old - say it's old
#
old () {
  printf "$NAME: home's IP older than today\n" >&2
  exit 4
}


NAME=`basename $0`
if [ $# -lt 3 ]
then usage
else host=$1
    dir=$2
    shift 2
fi
case $host in
    *@home)
          checkCurrent
          sedCmd="s/home/`cat $HOME/var/home`/"
          host=`echo $host | sed "$sedCmd"`
          ;;
    home) checkCurrent
          host=`cat $HOME/var/home`
          ;;
    *@office)
          host=`echo $host |
            sed 's/office/cms-2313-02.cms.shu.ac.uk/'`
          ;;
    office)
          host=cms-2313-02.cms.shu.ac.uk
          ;;
esac
case $host in
    *@*)
          ;;
    *)
          host=cmsps@$host
          ;;
esac
mkTmp
{
    printf "binary\ncd $dir\n"
    for file
    do  echo get $file
    done
    echo quit
} > $TMP/commands
sftp -B $TMP/commands $host > $TMP/errors
return=$?
case $return in
    0)
          exit
          ;;
    2)   
          echo $NAME: $dir: no such directory >&2
          exit $return
          ;;
    6)   
          printf "$NAME: no such file(s): " >&2
          grep '(src): no such file' $TMP/errors |
            sed  's?.*/??
              s/.).*//' |
              fmt >&2
          exit $return
          ;;
    *)
          echo $NAME: ERROR $return >&2
          cat $TMP/errors >&2
          exit $return
esac


OzTEXS 12-13-2006 05:21 PM

This is great !! Thanks .. I will try this ASAP

cmsps 10-23-2008 05:20 AM

Updated to OpenSSH
 
I've updated the SFTPget/put scripts so that they work with OpenSSH as well as SSH.com SSH. You can get them here: http://homepages.shu.ac.uk/~cmsps/freeScripts

cmsps 06-23-2013 10:33 AM

Home page moved
 
I have had to move my home page so the link in the previous post is outdated. Also, the the three scripts have been rolled into one.

You can get the SFTP script from: http://www.apxd65.dsl.pipex.com/freeScripts/#SFTP

lleb 06-23-2013 01:37 PM

nice looking script cmsps.

Z038 06-23-2013 05:33 PM

Agreed, very nice script. Thank you cmsps.


All times are GMT -5. The time now is 10:03 PM.