PDA

View Full Version : using SCP in my C program



nitinmhetre
12-18-2006, 03:40 AM
Hi all,

can anybody give me simple C program which uses SCP to copy file from server to client.

Thank You,
Nitin.

Salem
12-18-2006, 06:26 AM
Do you know how to use scp from the command line?

It's really rather trivial
If you put on the command line
scp from to

Then in code, its
system("scp from to");

nitinmhetre
12-18-2006, 10:39 PM
I did that system(),but it is asking for password.how to set password in program.and i want to do scp for multiple files.Is that possible,shall I need to use fork() multiple times?

Salem
12-19-2006, 12:33 AM
Does it ask for a password when you run it from the command line?

Is there a command line option to specify the password (doesn't look like it)?

> shall I need to use fork() multiple times?
Probably just the once for each scp session

Create a two way pipe between your program and an scp session.
Listen to stdout of scp to look for password prompts, then respond on its stdin with the password.

Then hope that scp actually reads from stdin, and not directly from the controlling terminal.

nitinmhetre
12-19-2006, 05:13 AM
hi salem,
I am new to network programming,so i dont know how to get two way pipe between main program and scp session.I tried it with system(),but then you dont get control over the process created by system().
Is there any other way to do scp for multiple files with password set in the same program?

Here what i have tried.


#include<unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

void client(int,int);
void server(int,int);

int main(int argc, char **argv)
{
int pipe1[2],pipe2[2];
char c;
pid_t childpid;

pipe(pipe1);
pipe(pipe2);

if((childpid=fork())==0){
sleep(10);
close(pipe1[1]);
close(pipe2[0]);

server(pipe1[0],pipe2[1]);
exit(0);
}

/*parent */
close(pipe1[0]);
close(pipe2[1]);

client(pipe2[0],pipe1[1]);

waitpid(childpid,NULL,0);
exit(0);
}


void client(int readfd,int writefd)
{
//int len,n;
char buff[200]="scp tiny user@23.132.32.10:tiny\n";

//system("scp tiny user@23.132.32.10:tiny");
write(writefd,buff,strlen(buff));

}



void server(int readfd,int writefd)
{
int fd;
int n;
char buff[200]="password\n";

//if((n = read(readfd,buf,200)==0))
// ;
//buff[n]='\0';
write(writefd,buff,strlen(buff));

close(fd);

}

jim mcnamara
12-19-2006, 06:16 AM
Try man popen (in stdio.h) as well.

nitinmhetre
12-19-2006, 11:44 PM
I have tried this popen too,but it still asking for password.Please see my code and tell me what is going wrong.



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char command[100];
int cnt;
FILE *fp;
fp= popen("scp tiny user@12.28.22.20:tiny","w");
sleep(10);
//fflush(stdout);
//write(fp,"adi@x221055!!",strlen("adi@x221055!!"));
puts("212312!!"); //entering password
//fflush(stdout);
pclose(fp);
}