Hi all,
can anybody give me simple C program which uses SCP to copy file from server to client.
Thank You,
Nitin.
Hi all,
can anybody give me simple C program which uses SCP to copy file from server to client.
Thank You,
Nitin.
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");
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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?
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Code:#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 [email protected]:tiny\n"; //system("scp tiny [email protected]: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); }
Last edited by nitinmhetre; 12-19-2006 at 05:21 AM.
Try man popen (in stdio.h) as well.
I have tried this popen too,but it still asking for password.Please see my code and tell me what is going wrong.
Code:#include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ char command[100]; int cnt; FILE *fp; fp= popen("scp tiny [email protected]:tiny","w"); sleep(10); //fflush(stdout); //write(fp,"adi@x221055!!",strlen("adi@x221055!!")); puts("212312!!"); //entering password //fflush(stdout); pclose(fp); }