Pls HELP: Redirecting input to SFTP connection

Hi,

Can somebody please let me know if we can redirect variable value to the sftp connection. I am trying to run the attached snippet but facing an error.

dev@UAT.com> export USER1=ftp
dev@UAT> export HOST1=XX.XX.XX.XX
dev@UAT> export INPUTDATA2='lcd /Z02/apps/output/UAT/CMUP/incoming cd apl/apl001/download get UPLOAD.DAT bye'
dev@UAT> sftp -oProxyCommand='/usr/bin/nc -v -X 5 -x proxy:99 %h %p' -oPort=22 ${USER1}@${TARGETHOST1} < ${INPUTDATA2}
ksh: lcd /Z02/apps/output/UAT/CMUP/incoming cd apl/apl001/download get UPLOAD.DAT bye: cannot open [No such file or directory]

Please note that the operations which needs to be done after sftp connection is stored in

INPUTDATA2

Could someone suggest a better way to do that or identify the mistake I am making?

Regards,

The redirection command < pathname feeds the contents of the file whose name is pathname into the standard of the command specified by command .

It would seem that you want something more like:

USER1=ftp
TARGETHOST1=XX.XX.XX.XX
printf '%s\n' 'lcd /Z02/apps/output/UAT/CMUP/incoming' \
	'cd apl/apl001/download' \
	'get UPLOAD.DAT' \
	'bye' |
    sftp -oProxyCommand='/usr/bin/nc -v -X 5 -x proxy:99 %h %p' -oPort=22 ${USER1}@${TARGETHOST1}
1 Like

Thanks Don !! I will check and update.

---------- Post updated 04-12-16 at 04:03 AM ---------- Previous update was 04-11-16 at 10:26 PM ----------

Thanks it did worked.

Just out of curiosity can the same be achieved by using

<

The script I am changing was using

< ${INPUTDATA2}

while making a FTP connection and I am changing it to use SFTP connection.

Regards,

sftp reads its commands from stdin, no matter if this is filled from the terminal, a pipe that forwards another program's output, or a redirection ("<") that opens a file to read from. But - it has to be a file! Your sample above offers the commands in a shell variable, which is interpreted as a filename and thus leads to the error message posted.

1 Like

If working in a recent shell, you may want to lookup "Here Documents" that offer another way to redirect stdin.

1 Like

Thanks Rudic for the elaboration.

The link was not functional. Could you please paste it again?

Regards,

No link. man bash or man ksh .

Thanks Rudic.

Taking my earlier suggestion and converting it to use a file instead of a pipeline:

USER1=ftp
TARGETHOST1=XX.XX.XX.XX
printf '%s\n' 'lcd /Z02/apps/output/UAT/CMUP/incoming' \
	'cd apl/apl001/download' \
	'get UPLOAD.DAT' \
	'bye' > /tmp/tmp$$
sftp -oProxyCommand='/usr/bin/nc -v -X 5 -x proxy:99 %h %p' -oPort=22 ${USER1}@${TARGETHOST1} < /tmp/tmp$$
rm -f /tmp/tmp$$

which wastes system resources and runs slower than my earlier suggestion.

Converting it to use a here-document:

USER1=ftp
TARGETHOST1=XX.XX.XX.XX
sftp -oProxyCommand='/usr/bin/nc -v -X 5 -x proxy:99 %h %p' -oPort=22 ${USER1}@${TARGETHOST1} <<-"EOF"
	lcd /Z02/apps/output/UAT/CMUP/incoming
	cd apl/apl001/download
	get UPLOAD.DAT
	bye
EOF

which might be a little bit faster than my earlier suggestion. Since printf is a built-in utility in most shells, it won't make a lot of difference.

Hope this helps,

  • Don
1 Like

Thanks Don Cragun. Appreciated !!

Regards,