Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using SCP in a C Program

Status
Not open for further replies.

AimeeSoltys

Programmer
Oct 18, 2005
1
US
I am trying to write a C program that uses SCP to obtain a file from another machine to the current machine. Does anyone have suggestions on how to code this? This is what I have:

char CommandLine[4096];
...
sprintf( CommandLine, "scp user@OtherMachine:/../../file.gz ." );
system( CommandLine );

But of course, SCP is gonna ask for a password, so how do I have the C program give the password?
user@OtherMachine's password:
 
For C, if I'm not mistaken, you would probably need
to use a pseudo terminal to drive the interaction.
If stdio is not used in scp(I'm sure it is, or a
facsimile thereof) then you might be able to get
away with coding a coprocess.
In other words it's more trouble than it's worth.
Take a look at expect instead.
 
You can get scp to work without passwords by using key pairs. From my manual
Un-attended login
When batch scripts require secure access to remote hosts, i.e. no user to type in the password, an un-attended-login is required. This is achieved by copying the requesting user's authentication key from the source host to the target host into a file called .ssh/authorized_keys. E.g. to set up an un-attended login for 'user' on host2 when connecting from host1: -

user@host1> cd; mkdir .ssh
user@host1> ssh-keygen -t rsa -N '' -f .ssh/id_rsa
user@host1> scp .ssh/id_rsa.pub user@host2:user_host1_key #requires password
user@host1> ssh -l user host2 'mkdir .ssh; cat user_host1_key >> .ssh/authorized_keys' #requires password
user@host1> ssh -l user host2 'ls -la' #Does NOT require password



The ssh-keygen command generates the user's key for host1. Thus, when added to the authorized-keys file on host2 allows user on host1 to login into user account on host2 without entering interactive mode to enter the password, i.e. un-attended login.
This applies just as well for scp as for ssh and sftp.

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top