Solved

convert ftp to sftp Shell script

Posted on 2011-09-26
10
2,065 Views
Last Modified: 2012-05-12
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
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 

Author Comment

by:sunhux
ID: 36648985

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

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

Author Comment

by:sunhux
ID: 36660420

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
 

Author Comment

by:sunhux
ID: 36663536

 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

Author Comment

by:sunhux
ID: 36700134

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
 
LVL 62

Expert Comment

by:gheist
ID: 36713604
rsync?

scp collection1 collection2
0
 

Author Comment

by:sunhux
ID: 36813083

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
 
LVL 62

Assisted Solution

by:gheist
gheist earned 500 total points
ID: 36813178
RSYNC_RSH=`which ssh`rsync (-z) 192.168.10.10:/usr/bin/* /tmp/.rubish/

0
 

Author Comment

by:sunhux
ID: 36828801

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
 
LVL 62

Accepted Solution

by:
gheist earned 500 total points
ID: 36890192
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
 

Author Closing Comment

by:sunhux
ID: 36899047
ok
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question