using SCP in my C program

This is a discussion on using SCP in my C program within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, can anybody give me simple C program which uses SCP to copy file from server to client. Thank ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    using SCP in my C program

    Hi all,

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

    Thank You,
    Nitin.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    28
    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?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    28
    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 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);
    
    }
    Last edited by nitinmhetre; 12-19-2006 at 04:21 AM.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    Try man popen (in stdio.h) as well.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    Question

    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 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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21