System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with ftp to sftp command

 
Thomas Jones2
Occasional Advisor

Need help with ftp to sftp command

I'm having a problem switching an ftp command over to sftp

ftp
ftp -i -v ${SVC_HOST} >> ${TMP} 2>&1 <<+

sftp
sftp -o PreferredAuthentications=publickey losapp02@${SVC_HOST} >> ${TMP} 2>&1 <<+

the 2>&1 <<+ at the end causes the sftp not to work, what am i doing wrong?
9 REPLIES
Steven Schweda
Honored Contributor

Re: Need help with ftp to sftp command

Potentially interesting:

uname -a
ssh -V

> [...] causes the sftp not to work, [...]

Which variety of "not to work" is this?

> what am i doing wrong?

What, exactly, are you _trying_ to do?
(And what, exactly, happens when you do
whatever you do?)

man sftp

Look for "-b"?
Thomas Jones2
Occasional Advisor

Re: Need help with ftp to sftp command

the script initiates the sftp session, then does a get or put then bye

unlike ftp, sftp sees 2>&1 as invalid sytax and replies with the help screen showing the switches.

if the stderr can't be captured and grep'd then i may have to check the return code

-b is for batch mode, and not useful in this script
Steven Schweda
Honored Contributor

Re: Need help with ftp to sftp command

> the script initiates the sftp session,
> then does a get or put then bye

Which "the script" is this? I can't see it.

> unlike ftp, sftp sees 2>&1 as invalid sytax
> and replies with the help screen showing
> the switches.

I doubt that sftp "sees 2>&1". The _shell_
may see (and interpret) that sort of
redirection, however. We might make more
progress if you reported what you actually
did, and what actually happened when you did
it, rather than what you think it all means.

> if the stderr can't be captured and grep'd
> then i may have to check the return code

You may. Probably a good idea in any case.

> -b is for batch mode, [...]

Yes, it is. And what is that "<<+" doing?

> and not useful in this script

You know, if I could see "this script", then
I might agree with that or not.

> What, exactly, are you _trying_ to do?
> [...]

Still shrouded in mystery.
rmueller58
Valued Contributor

Re: Need help with ftp to sftp command

I have defined host key exchange for users and run SCP/SFTP in Batch mode.

sudo -u USERNAME scp -B USERNAME@hostname:/SOURCEDIR/filename /DESTDIR/

you can also initiate remote shell scripts with SSH.

If you do the Key exchange you don't have to pass the Authentication params in your connection, it assumes trust.

I am not sure what your <<+ does, if the intent is to read in a carriage return it wouldn't work for either SFTP or FTP.


NOTE "\r" in script below passes carriage return.

One thing you might consider is using expect.
#!/usr/bin/expect -f

# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# set Variables

set password "REALPASS"
set ipaddr "REALHOST.DOMAIN.TLD"
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr)
spawn sftp REALUSER@$ipaddr $arg1
match_max 100000

expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\r"
exp_continue
#look for the password prompt
}

"password:" {
send -- "$password\r"
}
}
expect "sftp>"
send "lcd /SOURCEDIR/\r"
expect "sftp>"
send "put sourcefile\r"
expect "sftp>"
send "ls \r"
expect "sftp>"


Thomas Jones2
Occasional Advisor

Re: Need help with ftp to sftp command

I experimented with this over the weekend, and found -b batchmode works best. And Unix the return code works, so a grep of the error log is not required. I am not very good with Unix so if someone could look at the last part of this script and tell me the correct syntax for section # LOOP TO TRANSMIT THE TRANSACTION FILE TO THE CENTRAL DATABASE THROUGH FTP

###############################################################################
# DEFINE ENVIRONMENT VARIABLE DEFINITIONS
###############################################################################

CONFIG_FILE="../../environment.config"
LCDIR=`grep LOSDIR $CONFIG_FILE | awk '{print $2}'`
DBNAME=`grep LOSDB $CONFIG_FILE | grep -v LOSDB2 | awk '{print $2}'`
DBSERV=`grep LOSSERV $CONFIG_FILE | grep -v LOSSERV2 | awk '{print $2}'`
export DBNAME;

###############################################################################
# DEFINE DATA SET VARIABLES
###############################################################################

DATA_FILE="../../dataset.config"
#CDB_OUT_DATA=`grep CDB_OUT_DATA $DATA_FILE | awk '{print $2}'`
#CDB_DIR=`echo $CDB_OUT_DATA | cut -d"." -f1`
#CDB_DATASET=`echo $CDB_OUT_DATA | cut -d"." -f2-4`
SVC_OUT_DIR=`grep SVC_OUT_DIR $DATA_FILE | awk '{print $2}'`
SVC_HOST=`grep SVC_HOST $DATA_FILE | awk '{print $2}'`
#SPACE=`grep CDB_OUT_SPACE $DATA_FILE | awk '{print $2}'`
#LRECL=`grep CDB_OUT_LRECL $DATA_FILE | awk '{print $2}'`
#BLK=`grep CDB_OUT_BLK $DATA_FILE | awk '{print $2}'`

##########################################################################
# TEST FOR EXISTENCE OF OUTPUT FILE CREATED BY if040.eco. IF FILE EXISTS
# CONTINUE PROGRAM. IF NO FILE, EXIT SHELL.
##########################################################################

if [ ! -f /app/${LCDIR}/if/servicing/serv_work_out/svc.* ]
then
echo "There are no servicing transaction files staged in the"
echo "serv_work_out directory."
echo "Shell if040_cdb_send.sh ended."
exit 0
fi

cd /app/${LCDIR}/if/servicing/serv_work_out

##########################################################################
# ESTABLISH LOG FILE AND GET LIST OF FILES TO TRANSMIT
##########################################################################

TMP=ftp.out
rm -rf ${TMP}
find . -name "svc.*" > svcfiles

###########################################################################
# LOOP TO TRANSMIT THE TRANSACTION FILE TO THE CENTRAL DATABASE THROUGH FTP
###########################################################################

for filename in `cat svcfiles`
do
echo put ${filename} ${SVC_OUT_DIR} > batchfile
done

ret_code = sftp -b batchfile lossit02@${SVC_HOST}
quit

if ret_code != 0
then
echo "Transfer to Central Database FAILED."
echo "Check the Servicing Connection and Follow Restart Procedures."
exit 1
fi

rm svcfiles
rm batchfile
cat ${TMP}

Steven Schweda
Honored Contributor

Re: Need help with ftp to sftp command

> [...] tell me the correct syntax [...]

Tell me what you're trying to do?

> echo put ${filename} ${SVC_OUT_DIR} > batchfile

">" -> ">>"? Did you look at what's in this
"batchfile"?

> TMP=ftp.out

Who uses this?
Steven Schweda
Honored Contributor

Re: Need help with ftp to sftp command

> rm svcfiles
> rm batchfile

As a general practice, using such simple
names for temporary files may lead to
problems, as when the user lacks write access
to the current working directory, or when
more than one instance of the script is
running at one time.

Including the process ID ("$$") in the name
should avoid problems with multiple
simultaneous instances of the script.

Putting temporary files into "/tmp" (or some
other, similar place) should avoid local
write-permission problems.

Including some script-identifying text in a
temporary file name can help the victim
puzzle out who's putting all this junk into
"/tmp".

For example:

TMPDIR=${TMPDIR:-/tmp}
base_name=` basename $0 `

temp_file_1_name="${TMPDIR}/${base_name}_1_$$.tmp"
[...]

Also useful:

man
Thomas Jones2
Occasional Advisor

Re: Need help with ftp to sftp command

Thanks to everyone that contributed, this was a difficult project for me as I have no formal Unix training.

I have the sftp scripts & C programs converted to sftp and working. I am attaching an example script in the hopes that anyone working on a similar project can download & use it. To make it work modify the cd/get/put with your commands, and put in your user_id & hostname.

Make sure to install the keys on both hosts first, follow the Public Key Setup directions. Don't use a passphrase unless you have to.
http://sial.org/howto/openssh/publickey-auth/
Thomas Jones2
Occasional Advisor

Re: Need help with ftp to sftp command

click on the paperclip to download a sftp delete sample script