File transfer using SFTP

Hi,

I want to a transfer file from remote machine to a local machine using SFTP where both my local and remote machines are Ubuntu machines.So i wanted to write a unix bash script which uses SFTP command to transfer the file from remote to local machine.

My remote server is 178.28.30.106.
I'm trying to transfer the file file1 located in the remote machine location "/home/user/datafiles/" to the local machine directory "/home/user/receivedFiles/".

So how to write unix bash script to achieve the file transfer using SFTP?

Thanks,
Shree

So a few questions:-

  • What have you tried so far?
  • What is you thinking about how to code it?
  • Can you do this manually first so you can work on automation?
  • Have you set up SSH keys for passwordless authentication?
  • Have you used an editor on unix to work on a simple text file?

Most importantly, What have you tried so far?

I don't want to seem aggressive or petty, but I could give you an answer that meets your stated need, but it may either not be quite what you intended and you won't learn how to maintain or support it.

Have a go and let us know where you get stuck and what output/errors you have so we can help.

Robin

Hi rbatte1,

I have set up the SSH keys for passwordless authentication. Also i tried transferring the file from remote machine to a local machine without writing a unix bash script wich is working fine. I'm able to transfer the file from remote machine to th local machine.
Here initially i moved to the directory where my datafile is residing using

and later on did the below code to get the remote file on to local directory

sftp> get file1.txt newFile.txt

Using unix script , i saw many similar cases where they have used

but i wanted to use

.
So how to use this and what's the difference between these two?

My bash script is

#!/bin/bash
HOST='178.20.30.106'
USER='root'
PASSWD='abc'
sftp $USER@$HOST <<EOF
lcd /home/user/receivedFiles/
cd  /home/user/datafiles/
get file1.txt mynewfile.txt
EOF

When i run the above code i'm able to transfer the file from remote to loacl but the problem is nowhere i'm using the password(PASSWD) . So when i run the above script it asks me for the password to enter throught the commandline prompt. So how to avoid this ?
How can i pass PASSWD value so that the human intervension will not be there in transferring the file.

Thanks,
Shree

The expect tool is not standard unix. It's freely available, but it#'s a whole language in itself. With SFTP & passwordless keys, you won't need it though.

If you have passwordless authentication, you don't need this bit:-

USER='root'
PASSWD='abc'

I presume that you are putting in the real value of user@server and can do this on the command line to get connected.

Can you show us what output do you get manually and then by running your script? Nothing leaps out as a problem at the moment, but if it's prompting for your credentials, then your haven't got passwordless authentication working, which you will need to do first.

Feel free to over-type anything you deem sensitive.

Robin

I have updated/edited my reply where i have mentioned the actual issue.

I have updated/edited my reply where I have suggested the issue is with an incomplete setup of SSH key exchange to allow passwordless login. Did you make your client public key trusted on the server account that you want to connect to? What is the server OS by the way?

Can you show us the output from both a manual sftp with the user name you want to connect to and the output from your script.

Thanks,
Robin

Try manual sftp and post us the entire log masking the sensitive content

Hi,

I'm using Ubuntu 10.04 LTS.

I checked the set up of passwordless authentication in this way:

.When i excecuted the above command it din't ask me for any password. So i thought it might be passwordless authentication is set up alreday.Am i verifying the passwordless authentication in a right way?

Where can i find the logs related to sftp or ssh. I searched bfor /var/log/secure but dint find it.

Also executing the sftp caommands manually to transfer the file from remote to local not giving any error but the thing is asking for the password as passwordless authentication is not properly set.

When i write the bash script where in i used sftp commands to transfer the file. Here also i'm not getting any error and file is being transferred sucessfully but asking for the password due to authentication.

So how can i set the passwordless authentication and verify it.
One more query is if i go for passwordless authentication does it affect other things as multiple users are using the same ubuntu machine and alos hadoop is installed on this machine. So enabling passwordless authentication will affect other things or it is only applicable for my work?
I'm bit confused here.

Thanks,
Shree

---------- Post updated 06-10-14 at 01:30 AM ---------- Previous update was 06-09-14 at 11:46 PM ----------

Hi,

I got the steps to create passwordless login to a remote machine from local machine.
Below are the steps:

localserver# mkdir ~/.ssh
localserver# cd ~/.ssh
localserver# ssh-keygen -t rsalocalserver# ssh user@<remoteserver_ip>
Password: 
localserver# ssh user@<remoteserver_ip> mkdir -p .ssh
localserver# cat .ssh/id_rsa.pub | ssh user@<remoteserver_ip> 'cat >> .ssh/authorized_keys'
localserver# ssh user@<remoteserver_ip> "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
localserver# ssh user@<remoteserver_ip>

In the above steps, in which location do i need to create the ssh directory on local and remote machine.I already have ssh directory on this path /etc/ssh both on local and remote machines. So do i need to create ssh directory in the differen tlocation or do i need to use the ssh directory already existing?In case if i use the existiong ssh directory does it overwrite the existing contents with new contents? Please clarify the above queries.

Thanks,Shree

If you want to run that using your ID, create '.ssh' folder in your home directory
else, create in the respective application / functional ID's home directory

Hi Srini, I dint get "If you want to run that using your ID, create '.ssh' folder in your home directory" . Here ID in the sense?I'm accessing ubuntu using putty.

I mean user (username)

One more query is can we have multiple ssh directories one created under root user in the path /etc/ssh and other created using user user in the path /home/user/ssh ? Do they conflict each other?

In my case i have ssh folder created under /etc and under ssh i have the following files:

So after login to user useranme can i excecute the $ssh-keygen -t rsa command inside /etc/ssh folder? So doing this will overwrite the existing ssh_host_rsa_key.pub file or it will make a new public key entry at the end of the file?

So, the public key needs to be accepted on the server by the user you are connecting to. You seem to have the instructions to do this. Can you sign on without a password as that user?:-

ssh user@host

If it prompts for a password, do you know it? If so, put it in then have a look on the remote server at the permissions for the .ssh directory in that users home directory and the files within it. It must be owned by the account you've logged on as and it's best to keep them all as RW only to that owner, with no other access. The .ssh directory itself will need to have RWX though.

Note that the /etc/ssh is for the overall service, not your login certificates. Using ssh-keygen will (by default) work on or create the .ssh directory in your home directory.

Robin

Hi,

I tried generating public and private keys on local machine. Also copied the id_rsa.pub to the remote machine and renamed to authorized_keys.

Also set the permissions to the .ssh and authorized_keys as below:
chmod 640 authorized_keys
chmod 700 .ssh

But still when i do :
$ssh remoteserver@<ip>, it is asking for the password. The passwordless login is not yet enabled.Not getting where it's going wrong.

Thanks

---------- Post updated at 04:06 AM ---------- Previous update was at 03:47 AM ----------

Hi,

I'm able to transfer the file from remote machine to loac server with passwordless login.

Thank you.

Where did you store authorized_keys? Was it in your home directory on the remote server or in the .ssh subdirectory of the user you want to connect as?

I think you saying that this works without a password:-

ssh user@ip

.... but this requires a password:-

sftp user@ip

using the same user & ip. That would be odd. Is this correct?

Robin