sunhux
asked on
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 ??
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 ??
ASKER
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
ASKER
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
ASKER
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?
rsync?
scp collection1 collection2
scp collection1 collection2
ASKER
What's the exact syntax with rsync?
Note that due to security reason, I'm not allowed to set up
a permanent ssh tunnel
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:/us
^
|_____ is there suppose to be space here?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok
ASKER
Or should the syntax be (ie use -b option) ::
{
echo ...
. . .
} | sftp -b $USER@$IPADDR > $LOG_DIR/$TMP_LOG_NAME