• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2302
  • Last Modified:

convert ftp to sftp Shell script

I have a Linux running ftp client which ftp to a remote Windows server (running ftp server).

We're going to replace ftp with sftp, so the Linux box becomes sftp client while
the Windows box become sftp/ssh server.  I don't have access to test the scripts
/ codes currently, so appreciate some quick inputs :

Current ftp script:
==============
. . . . . some initial codes / variable settings . . . . .
       DATERUN=`date '+%Y%m%d%H%M'`

       {
        echo "open $IPADDR"
        echo user
        echo "$USER \c"
        echo "$PASS"
        echo verbose
        echo "prompt off"
        echo "lcd $LOCAL_DIR"
        echo "cd $REMOTE_DIR"

        for FILE_NAME in $FILE_LIST
        do
        if [ -r $LOCAL_DIR/$FILE_NAME ]
        then
                        a1="tar"
                        a2=`echo $FILE_NAME | cut -f2 -d .`
                         if [ $a2 == $a1 ]
                         then
                                 echo binary
                         else
                                 echo ascii
                         fi
                        echo "rename $FILE_NAME $FILE_NAME.bak"
                        echo "put $FILE_NAME"

                        echo ascii
                        echo "rename $FILE_NAME.md5 $FILE_NAME.md5.bak"
                        echo "put $FILE_NAME.md5"
                fi
        done

        echo bye

        }  | ftp -n 2>&1 > $LOG_DIR/$TMP_LOG_NAME

*********************************************************************************************************

Sftp script I'm coding (need someone to test / check) :
==========================================
. . . . . some initial codes / variable settings . . . . .

       unix2dos $FILE_NAME.md5
       DATERUN=`date '+%Y%m%d%H%M'`

       {
        echo "lcd $LOCAL_DIR"
        echo "cd $REMOTE_DIR"

        for FILE_NAME in $FILE_LIST
        do
        if [ -r $LOCAL_DIR/$FILE_NAME ]
        then
                        a1="tar"
                        a2=`echo $FILE_NAME | cut -f2 -d .`
                        echo "rename $FILE_NAME $FILE_NAME.bak"
                        echo "put $FILE_NAME"

                        # echo ascii <== circumvented by dos2unix at the top
                        echo "rename $FILE_NAME.md5 $FILE_NAME.md5.bak"
                        echo "put $FILE_NAME.md5"
                fi
        done

        echo quit

        }  | sftp $USER@$IPADDR > $LOG_DIR/$TMP_LOG_NAME
           ^ ^
            |  |
            === is the last line above's syntax correct/useable in RHES Linux Bash ??
0
sunhux
Asked:
sunhux
  • 7
  • 3
2 Solutions
 
sunhuxAuthor Commented:

Or should the syntax be (ie use -b option) ::

{
 echo ...
 . . .
 }  | sftp -b $USER@$IPADDR > $LOG_DIR/$TMP_LOG_NAME
0
 
sunhuxAuthor Commented:

Basically I need all those sample ftp codes to be converted for use for sftp.
Eg ftp codes :
  http://www.askdavetaylor.com/redirecting_input_in_a_unix_shell_script.html

need sample syntax for sftp asap
0
 
sunhuxAuthor Commented:

 echo "cd inbox"; echo "pwd"; echo "ls" | sftp -b - $IPADDR
Above syntax works but I don't know how to group those
echo statements into brackets.  Keep getting error below:


USR="loginid"
IP="192.168.3.3"

{
 echo "cd inbox"
 echo "pwd"
 echo "ls"
 echo "quit"
} | sftp -b - $USR@$IP


sftp> cd inbox
Couldn't canonicalise: No such file or directory
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sunhuxAuthor Commented:

Ok I've fixed the above as inbox dir does not exist in the remote Windows ssh server.

Now what I need is if there's an error, how can I make the subsequent
echo "..." commands for sftp to continue to proceed without crashing out?
0
 
gheistCommented:
rsync?

scp collection1 collection2
0
 
sunhuxAuthor Commented:

What's the exact syntax with rsync?

Note that due to security reason, I'm not allowed to set up
a permanent ssh tunnel
0
 
gheistCommented:
RSYNC_RSH=`which ssh`rsync (-z) 192.168.10.10:/usr/bin/* /tmp/.rubish/

0
 
sunhuxAuthor Commented:

Q1:
What if the remote ssh server's end is a Windows server & the datafiles are
in Windows DOS text format (ie every line separator/terminator is  <CR><LF>
while my local ssh client is a Linux Unix text datafile (line terminator is <LF>) ?


Q2:
  if I previously used ftp to do an Ascii mode put to the remote Windows, would
the remote Windows datafile still get updated as Windows text format?

Q3:
  if I previously used ftp to do an Ascii mode get from the remote Windows box,
would my local Linux datafile still get to retain its Linux/Unix text format ?

Q4:
 if I previoulsy used ftp Append mode, to append to a remote Windows datafile,
 would rsync help to append ?

Q5:
Where can I insert the remote ssh login id?   Is it
`which ssh` rsync (-z) remoteid@192.168.10.10:/usr/bin/* /tmp/.rubish/
                   ^
                   |_____ is there suppose to be space here?
0
 
gheistCommented:
A1 RSYNC and SSH does not do any file conversions on the fly (some SSH clients do it ,but what if they do that wrong?)
A2-3 your script uses unix2dos for that purpose. Available with cygwin on windows, you can extract only one binary file from installation.
A4 No such thing, aparently it died out with dinosaurs. You can use remote SSH login to emulate functionality:
cat localdata | SSH user@host "cat >> remotelog"

A5 Yes, you need space there and you found right place for login ID.
You can actually use rsync server without encryption (you need to run it on one side at least)
0
 
sunhuxAuthor Commented:
ok
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now