Sftp,[put] all files that are .csv to server. Linux Ubuntu

Hello all,

I am trying to sftp files to a server, I am using keys as authentication and I can connect with no problems. I can also put a sample file on the server.

When I tried to automate this process with a .sh file I get the
"couldn't canonicalize: Failure".

This is my script: sftp -P22 -0IdentityFile=~/.ssh/is_rda/sftpKey sftp@ipAddressHere -v >>EOF

This shows that i connected with the -v debug and all keys are working.
My code to put the files then looks like this;

cd /./.
put *.csv*
echo "File has been sent"
EOF

my files have been given the chmod
I am trying to put all files that have a .csv on the server.
What am I doing wrong?

Many thanks for any help you provide.
This is my first post so I hope I have been descriptive enough.:b:

Hey,
have you checked the permissions of the identity file?
can you post the complete contents of the file, instead of bits and pieces?

Is it possible that you have a "-0" (zero) in lieu of the -o option? And, the here document's redirection points the wrong direction.

Thanks for your replies, I have checked the permission on the key folder. It has chmod 700 .

Here is the cron job that runs the code:

01 17 * * * /etc/cron.daily/Transfer.sh 2>&1 | etc/cron.daily/TimeStamp.sh >> /home/usert/Transferlogfile.log

Here is the full script:

#!/bin/bash -x
sftp -P 22 -oIdentityFile=~/.ssh/id_rsa/sftp -v username@192.168.17.181<<EOF
cd ../../
put ftpusers/TransferFolder/ftp/data/*
echo "File transfer has been completed"
bye
EOF

Using the same sftp line from my script connects to the server because debug shows the key verification process and says connected.
Basically, I need a script that will send any file with the .csv extension that is in the data folder, to the server each day at just after 5.

I also have another script which you can see referenced in the cronjob which is to create a log file with the date of transfer and file name sent, here that is:

#!/bin/bash
while read x; do
    echo -n `date +%d/%m/%Y\ %H:%M:%S`;
    echo -n " ";
    echo $x;
done

The path ftpusers/TransferFolder/ftp/data/ is 2 folder levels below my users home path, e.g ~/. So I need to ../../ to then go to my ftpusers/TransferFolder/ftp/data/ ,path. will this affect the process?

Again, thank you for your time and answers

What Linux distribution are you using?

Let's break up your script.

#!/bin/bash -x 
sftp -oIdentityFile=~/.ssh/id_rsa/sftp -v username@192.168.17.181<<EOF 

The above statement worked fine.

cd ../../

are you trying to change directory in your local machine or your remote machine?
if you are changing in remote "cd" will work.
if you are trying to change locally, try "lcd"

put ftpusers/TransferFolder/ftp/data/*

try using absolute path starting from "/" example "/home/user/ftpusers/TransferFolder/ftp/data/*"

echo "File transfer has been completed"

echo will not work inside FTP, need to use after EOF.

bye
EOF

This looks good

Also please see below a version of script that worked for me.

#!/bin/bash -x
sftp -o IdentityFile=~/.ssh/sftp -v remote_user@remote_machine<<EOF
cd files
put /home/localuser/files/*
bye
EOF
[[ ${?} -eq 0 ]] && { echo "File transfer has been completed"; } || { echo "File transfer Failed"; }

The above script transfers all the files from dir "/home/localuser/files/" on the local machine to
"/home/remote_user/files" directory on the remote machine.

hope this helps.

Hi,
Thank you for your reply, I was confusing the cd and lcd which when corrected now functions.
Thanks for all your help.:smiley: