LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2008, 06:50 AM   #1
ravikumarv
LQ Newbie
 
Registered: Jun 2007
Posts: 27

Rep: Reputation: 15
Question feteching file from remote system using scp through a C programme.


Hi,

I need to fetch a file from remote system using scp through a C programme.(basically not to enter passwd manually)
As we need to capture the prasswd promting in the programme i using the pipe.

Here is the my sample progrmme.



bool getRemoteFile(char *ip,char *user,char *passwd,char *srcFile,char *dstFile,char *errBuf)
{

char *exe = "/usr/bin/scp";
int result=0,ret = 0, pid=0;
int write_stdin[2], read_stdout[2];
char response[512];
char inputString[1024];
char *argv[10] = {exe,inputString,dstFile,NULL};

snprintf(inputString, 1023, "%s@%s:%s", user,Ip, srcFile);

pipe(write_stdin);
pipe(read_stdout);

/* Fork the process */
pid = fork();

switch (pid) {
case 0: /* Child process */
close(write_stdin[1]);
close(read_stdout[0]);
/* Replace stdin and stdout with the new fds */
dup2(write_stdin[0], STDIN_FILENO);
dup2(read_stdout[1], STDERR_FILENO);
dup2(read_stdout[1], STDOUT_FILENO);
close(write_stdin[0]);
close(read_stdout[1]);
execvp(exe, argv);
_exit(0);
break;
case -1: /* fork() error */
Log("Fork() error for host:%s, cmd:scp, user:%s", Ip,user);
break;
default: /* Parent process */
/* Close unneeded fds */
close(write_stdin[0]);
close(read_stdout[1]);
struct pollfd waitfordata[1];
waitfordata[0].fd=read_stdout[0];
waitfordata[0].events=POLLIN;
sleep(1);
int status;
int waittime=1;
// result = talkToIDPDevice(waitfordata,read_stdout[0], write_stdin[1], idpdevice->password);
do {
int err = poll(waitfordata, 1, 60000);
if (err == -1){
strncpy(errBuf,"poll failed failed",strlen("poll failed failed"));
return false;
}else{
if(err == 0 && waittime > 5 ){

/* Connection Timed Out
killl the child process
*/
kill(pid,9);
strncpy(errBuf,"Connection Timed Out",strlen("Connection Timed Out"));
return false;
}else{
memset(response,0,512);
read(read_stdout[0],response,511); /* process the buffer*/
if(strstr(response,"(yes/no)") == 0 ){
write(write_stdin[1],"yes",3);
write(write_stdin[1],"\n",2);
continue;
}else if(strstr(response,"password:") == 0 ){
write(write_stdin[1],passwd,strlen(passwd));
write(write_stdin[1],"\n",2);
break;
}
}
}
waittime++;

}while(1);
/* *call wait pid */
close(write_stdin[1]);
close(read_stdout[0]);
}
}


Generally using above programme we can execute a command in child and capture the output that command in parent.
Similarly i tried using scp command so that passwd prompting will received in the parent , but passwd prompting is going to STDOUT.

Why scp output was not captured in the parent.

Please help me.

Regards,
Ravikumar
 
Old 02-13-2008, 08:18 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Why don't you use a library like http://nixbit.com/cat/system/networking/ssh-library/?
Or, why don't you setup the machines so no password is asked?
 
Old 02-14-2008, 01:08 AM   #3
ravikumarv
LQ Newbie
 
Registered: Jun 2007
Posts: 27

Original Poster
Rep: Reputation: 15
setting no passwd not serves my purpose.
May be SSH library will do my work.

Is not possible doing through fork + exec + pipe + dup system calls ?

Regards,
Ravikumar
 
Old 02-14-2008, 01:17 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think when he said 'no passwd' he meant use 'authorised keys' instead. Still secure, but cmd line passwd not reqd.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SCP: Copy file from remote linux server onto a windows machine? Tony Empire Linux - Newbie 21 09-22-2015 10:18 PM
scp: copy a file from local machine to remote machine seran Linux - Newbie 8 10-30-2007 12:23 PM
Remote File System ?? kenz Linux - Networking 1 05-13-2007 12:29 PM
scp/ssh tail(multiple file) remote copy tpreitano Linux - General 1 08-22-2005 02:17 PM
scp :copy file from one remote comp to another aniketh Linux - Networking 4 06-16-2004 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration