Scripting multiple file "puts" in sFTP...

I need to send multiple files to a remote server via sFTP. I had everything set up to connect and send a single file automatically using a batch file and key authentication. Recently, however, the process has changed and we now need to send multiple files, one at a time, pausing for up to ten minutes in between files. I am having trouble passing a variable from my .ksh script to the sftp command. Once I pass control over to the batch file for sftp, it only recognizes sftp commands. I can't use a wildcard like "file*.RPT" because it will send all of the files up at once, without pausing in between them.

Does anyone have any experience scripting with sFTP that can shed some light on this for me?

Looks like a job for a here-document. I don't use sFTP but in old fashioned unsecure ftp you would do something like:

for file in *.txt
do
ftp -n <destinationserver><<EOF
user <userid password>
cd <destinationdirectory>
put ${file}
bye
EOF
sleep 600
done

The commands between <<EOF and EOF (you can use any character string but the start and end strings must be identical with no leading/trailing spaces) are executed as if you typed them in at the keyboard

hope this helps

cheers

Cbish68,

To sleep for any amount of time, you would have to run a sleep command. Why don't you use a batch file. Try doing this:

$ cat batchfile
cd /tmp
lcd /etc
put issue
!sleep 10
put motd
bye

$ sftp -b batchfile username@hostname

Thanks, but sFTP works a bit differently than standard FTP. In order to run the sFTP script from cron, unattended, you must use sFTP with a batchfile. The batchfile only understands sFTP commands. It will not interpret variables, etc. You can't script sFTP inline like you can with standard FTP.

I tried that, but I need to handle multiple, unique filenames. In your example, how would you handle sending three files named issue8, issue23 and issue989 if you had to send them separately, sleeping for a given time in between each?

I can't pass the filenames from my script to the sFTP batchfile using variables or any "for filename in" construct. Once you pass control off to the sFTP batchfile, it only understands sFTP commands.

I ran into the same problem.. i needed to pass variables to the sftp batch..

As a "solution" i found this:

touch sftp_batch
for files in `ls -1 /dir` ; do
{
echo "get $files" >> sftp_batch
}
echo "quit" >> sftp_batch

sftp -b sftp_batch $server  2> errors

rm sftp_batch 

so basicly, i create the whole batch on the fly...