Problem in SFTP using heredoc

Hi,
I'm having heredoc to get files from ftp server.

#!/bin/bash
/export/opt/SCssh/3.7.1_C0/bin/sftp csi@192.168.1.100 <<GET_FILES
lcd /WEBSERVER_LINK/data_logs/
ls -l pub/csidata/GeneralAppFields_8_1_Feed.out pub/csidata/CtcCatalog_7_3_3_Feed.out
get pub/csidata/GeneralAppFields_8_1_Feed.out
get pub/csidata/CtcCatalog_7_3_3_Feed.out
quit
GET_FILES

When I execute this script, first command lcd only getting executed. Remaining commands are not getting executed. If I run these commands explicitly in sftp command prompt, all the commands are getting executed fine. I don't know why only one command is working and it is coming out of the script without executing other commands in heredoc.

Also I'm not getting any error while running the script.

Have any one faced the same issue before? Please reply why it is getting out when execute first command?

I can't see any problem in this script. Maybe, if you have another version of ssh/sftp on another box, try the script out there to check if that version behaves different. You could also check with vi, using :set list , if there is something awkward in this file after the line including that lcd . You could also try it with another shell maybe.

I tried it with bash and openssh on a Debian GNU Linux and it behaved as expected.

The above command is a unix command not a ftp command. It will try to output a directory listing to a named file.

Try as two ftp directory list commands:

ls pub/csidata/GeneralAppFields_8_1_Feed.out
ls pub/csidata/CtcCatalog_7_3_3_Feed.out

Methyl is correct - the rest of the script should continue executing though.

sftp version :

/export/opt/SCssh/3.7.1_C0/bin/sftp: SSH Tectia Server 4.4.8 on sparc-sun-solaris2.8
Build: 21

Also I forgot to mention in my previous post that, if we use commands in batch file and execute sftp it is working fine.

Script :

#!/bin/bash
 
 commands_get_files="get_files"
 /export/opt/SCssh/3.7.1_C0/bin/sftp -B $commands_get_files csi@192.168.1.100
 
cat get_files
lcd /WEBSERVER_LINK/data_logs/
ls -l pub/csidata/GeneralAppFields_8_1_Feed.out pub/csidata/CtcCatalog_7_3_3_Feed.out
get pub/csidata/GeneralAppFields_8_1_Feed.out
get pub/csidata/CtcCatalog_7_3_3_Feed.out
quit

When I run this script I'm able to get files from ftp server. If I put commands in batch file it is working fine. If I use heredoc, it's not working. I can go for batch mode design but I have to maintain lot of batch files. So I don't want to go for that option.

Is using ssh and/or scp instead an option?

In case sftp automation required, -b option is recommended to use for sftp batch mode.
Put all those command in a file and use -b file while invoking sftp.
It doen't work EXACTLY as ftp AFAIK.

     -b batchfile
             Batch mode reads a series of commands from an input batchfile instead of stdin.  Since it lacks user interaction it should be used in conjunction
             with non-interactive authentication.  A batchfile of '-' may be used to indicate standard input.

Thanks for the reply.

I heard from my colleague that, there is some restriction set in sftp server, that's why heredoc is executing only one command. If I put get as a first command in heredoc, I'm able to get file from sftp server. Colleague told me that he faced this problem and resolved this by using batch file.

I'm having doubt that, Is it possible to make such restrictions in server?
Have anyone of you faced these kind of problem before?

If you are able to run the commands explicitly with the same user, there shouldn't be any problem regarding access. [ Though I don't have much idea about that part ]. May be other could help us.

Just for a try, you could try to run those commands on command line (with here doc).
Press "enter" after each line and see what happens.

$>> sftp user@host <<FTP
lcd /WEBSERVER_LINK/data_logs/
ls -l pub/csidata/GeneralAppFields_8_1_Feed.out
get pub/csidata/GeneralAppFields_8_1_Feed.out
quit
FTP

Thanks anchal.

I tried this heredoc in command line, I'm getting the same result which I'm getting in script.

bash-3.00$ /export/opt/SCssh/3.7.1_C0/bin/sftp  csi@192.168.1.100 <<FTP
> lcd /WEBSERVER_LINK/data_logs/
> ls -l pub/csidata/GeneralAppFields_8_1_Feed.out
> get pub/csidata/GeneralAppFields_8_1_Feed.out
> quit
> FTP
You are authorized to use this System for approved business purposes only.
Use for any other purpose is prohibited. All transactional records, reports,
e-mail, software, and other data generated by or residing upon this System
are the property of the Company and may be used by the Company for any purpose.
Authorized and unauthorized activities may be monitored.

sftp>
/WEBSERVER_LINK/data_logs
sftp>
-rw-r--r-- 1000416  300017   10631705 Mar 15 20:09 GeneralAppFields_8_1_Feed.out
sftp>

---------- Post updated at 08:43 PM ---------- Previous update was at 08:42 PM ----------

Please explain clearly, because I couldn't understand this.

...but you're still not using -b - , which I really suggest you do. The noninteractive behavior may be quite different.

scp does a simple file-copy. scp local-source username@host:path/to/remote/dest
scp username@host:pub/csidata/GeneralAppFields_8_1_Feed.out path/to/local/dest && ls -l path/to/local/dest Timestamps on the retrieved file should match timestamps on the remote one.

ssh is of course secure shell. You can use it to grab files by using a pipe:

ssh -T username@host cd pub/csidata/ '&&' tar -cf - GeneralAppFields_8_1_Feed.out |
    tar -C path/to/destdir/ -xf -

will retrieve the GeneralAppFields_8_1_Feed.out file and extract it on your local machine, including proper timestamps.

Or, in general, you can just do the one command, the retrieval, and do the rest locally.

Sorry to labout this point but the above command will attempt to write a directory listing to file called pub/csidata/GeneralAppFields_8_1_Feed.out .

Use:
ls -l
Or:
ls pub/csidata/GeneralAppFields_8_1_Feed.out
Not:
ls -l pub/csidata/GeneralAppFields_8_1_Feed.out

Thanks Corona.
Now my heredoc is working fine.
Code:

 
/export/opt/SCssh/3.7.1_C0/bin/sftp -B - csi@192.168.1.100 <<FTP
lcd /WEBSERVER_LINK/data_logs/
ls pub/csidata/GeneralAppFields_8_1_Feed.out
get pub/csidata/GeneralAppFields_8_1_Feed.out
quit
FTP

Now I'm able to get files from FTP server.

You sure its working?
"-B" is for buffer size. You must use "-b".