LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-13-2006, 02:14 AM   #1
OzTEXS
LQ Newbie
 
Registered: Dec 2006
Distribution: Redhat ES 4.0
Posts: 21

Rep: Reputation: 15
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 !!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-13-2006, 11:05 AM   #2
cmsps
LQ Newbie
 
Registered: Jul 2006
Location: sheffield, uk
Distribution: fedora 18
Posts: 9

Rep: Reputation: 2
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
 
Old 12-13-2006, 05:21 PM   #3
OzTEXS
LQ Newbie
 
Registered: Dec 2006
Distribution: Redhat ES 4.0
Posts: 21

Original Poster
Rep: Reputation: 15
This is great !! Thanks .. I will try this ASAP
 
Old 10-23-2008, 05:20 AM   #4
cmsps
LQ Newbie
 
Registered: Jul 2006
Location: sheffield, uk
Distribution: fedora 18
Posts: 9

Rep: Reputation: 2
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
 
Old 06-23-2013, 10:33 AM   #5
cmsps
LQ Newbie
 
Registered: Jul 2006
Location: sheffield, uk
Distribution: fedora 18
Posts: 9

Rep: Reputation: 2
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
 
2 members found this post helpful.
Old 06-23-2013, 01:37 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,540

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
nice looking script cmsps.
 
Old 06-23-2013, 05:33 PM   #7
Z038
Member
 
Registered: Jan 2006
Distribution: Slackware
Posts: 804

Rep: Reputation: 157Reputation: 157
Agreed, very nice script. Thank you cmsps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remote backup server: vsftpd/SSL or openssh/sftp or... ? krog Linux - Security 3 04-24-2006 11:50 PM
setting up remote or sftp access helpme0904 Linux - Newbie 2 08-02-2005 03:12 PM
What is the config file for sftp-server? cthulueldergod Linux - Networking 1 08-30-2004 09:42 PM
Remote Win Bat File execute Shell Script on AIX Server DriveMeCrazy AIX 5 05-26-2004 06:24 PM
Files truncated by sftp/sftp-server at 65kb gato Linux - Networking 1 12-18-2003 10:29 AM


All times are GMT -5. The time now is 08:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration