VenkatKrishna
VenkatKrishna

Reputation: 127

File transfer using SCP and C or C++

Following is my code for transferring file from local machine to another machine using SCP everything is working fine but I when the program executing the "system(exec)" command it is asking for destination user password.

Is there any way to append(or add) password to exec char array with another '%s' in sprintf statement.

char exec[180];
sprintf(exec,"scp -o StrictHostKeyChecking=no %s/%s %s@%s:/home",current_path,filename,destination_user,dest_ip);

//printf("\n %s \n",exec);
if(system(exec)==0) 
printf("\nFile %s moved successfully\n",file);
else
printf("\nFile %s not moved successfully\n",file);

Upvotes: 0

Views: 6961

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129524

The correct solution here is to set up a passwordless login using RSA or DSA public/private key.

This site goes through the steps necessary: http://www.linuxproblem.org/art_9.html

In short, run ssh-keygen -t rsa, and use the default values, including an "empty" passphrase (otherwise, you'll be asked a passphrase instead). [Do this on YOUR machine]

Then use ssh b@B mkdir -p .ssh to create the .ssh directory of the target machine (b@B' corresponding to the remote user on remote machine).

Finally, copy the key generated in the first step to the relevant file on the remote machine. cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'.

Now you should be able to log into the remote machine without a password. (I use this both at home and at work for this purpose)

Upvotes: 4

Related Questions