Solved

convert ftp to sftp Shell script

Posted on 2011-09-26
10
1,747 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
  • 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
 

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 61

Expert Comment

by:gheist
ID: 36713604
rsync?

scp collection1 collection2
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 61

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 61

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now