How do to SCP from within my C++ program?

Discussion in 'C++' started by Saqib Ali, Feb 10, 2011.

  1. Saqib Ali

    Saqib Ali Guest

    Hi.

    I'm writing C++ code that is running on a Solaris system.

    From within my C++ program, I need to copy files from one machine to
    another using SFTP or SCP. I would like to learn how to do so
    programatically.

    There seems to be no standard C++ library (such as iostream, strings,
    etc) that would do this. I don't know why. Can anyone explain this to
    me? Without such a library, it seems my only option would be to run an
    operating system call. However if I did so, a user would need to type
    in a password during execution. That seems sub-optimal.

    I wish there was a function that looked like this:

    scp(sourceFile, targetMachine, targetUser, targetPassword, targetFile)

    What do other people do in this situation?

    Thanks much.
    - Saqib
    Saqib Ali, Feb 10, 2011
    #1
    1. Advertising

  2. Saqib Ali

    Jasen Betts Guest

    On 2011-02-10, Saqib Ali <> wrote:
    >
    > Hi.
    >
    > I'm writing C++ code that is running on a Solaris system.


    > From within my C++ program, I need to copy files from one machine to
    > another using SFTP or SCP. I would like to learn how to do so
    > programatically.


    every operating system is different, so there's no standard library
    for spawing external programs.

    > There seems to be no standard C++ library (such as iostream, strings,
    > etc) that would do this. I don't know why. Can anyone explain this to
    > me?


    ms windows doesn't have naything remotely like fork() or exec() or even
    a real argv[]. it's basically too hard to do that sort of thing in
    a standardisable way.

    > Without such a library, it seems my only option would be to run an
    > operating system call. However if I did so, a user would need to type
    > in a password during execution. That seems sub-optimal.


    ypu can use a public key instead of password with both scp and sftp (which
    are both essentially ssh rebranded)

    > I wish there was a function that looked like this:
    >
    > scp(sourceFile, targetMachine, targetUser, targetPassword, targetFile)
    >
    > What do other people do in this situation?


    Since this is a linux newsgroup, atleast for me....

    I use GVFS every day to edit remote files with a local editor
    it works fairly well (editor tends to crash occasionally in the
    directory browser, not sure why)

    AIUI programatically it's just ordinary local file operations on the correct
    GVFS path. gnome somehow fiugures out when it needs to ask me for a password
    or for access to the keychain.

    --
    ⚂⚃ 100% natural
    Jasen Betts, Feb 10, 2011
    #2
    1. Advertising

  3. Saqib Ali

    Paul Guest

    "Jasen Betts" <> wrote in message
    news:ij0io3$nqb$...
    > On 2011-02-10, Saqib Ali <> wrote:
    >>
    >> Hi.
    >>
    >> I'm writing C++ code that is running on a Solaris system.

    >
    >> From within my C++ program, I need to copy files from one machine to
    >> another using SFTP or SCP. I would like to learn how to do so
    >> programatically.

    >
    > every operating system is different, so there's no standard library
    > for spawing external programs.
    >
    >> There seems to be no standard C++ library (such as iostream, strings,
    >> etc) that would do this. I don't know why. Can anyone explain this to
    >> me?

    >
    > ms windows doesn't have naything remotely like fork() or exec() or even
    > a real argv[]. it's basically too hard to do that sort of thing in
    > a standardisable way.
    >

    CreateProcess()
    Paul, Feb 10, 2011
    #3
  4. Saqib Ali

    James Kanze Guest

    On Feb 10, 12:37 pm, "Paul" <> wrote:
    > "Jasen Betts" <> wrote in message


    > news:ij0io3$nqb$...


    > > On 2011-02-10, Saqib Ali <> wrote:


    > >> I'm writing C++ code that is running on a Solaris system.


    > >> From within my C++ program, I need to copy files from one machine to
    > >> another using SFTP or SCP. I would like to learn how to do so
    > >> programatically.


    > > every operating system is different, so there's no standard library
    > > for spawing external programs.


    > >> There seems to be no standard C++ library (such as iostream, strings,
    > >> etc) that would do this. I don't know why. Can anyone explain this to
    > >> me?


    > > ms windows doesn't have naything remotely like fork() or exec() or even
    > > a real argv[]. it's basically too hard to do that sort of thing in
    > > a standardisable way.


    > CreateProcess()


    Which isn't anything like fork() or exec(). Not that it would
    be too difficult to standardize something---systems differ a lot
    in how they handle IO as well, but C and C++ managed to
    standardize that.

    --
    James Kanze
    James Kanze, Feb 10, 2011
    #4
  5. Saqib Ali <> writes:
    > I'm writing C++ code that is running on a Solaris system.
    >
    > From within my C++ program, I need to copy files from one machine to
    > another using SFTP or SCP. I would like to learn how to do so
    > programatically.
    >
    > There seems to be no standard C++ library (such as iostream, strings,
    > etc) that would do this. I don't know why. Can anyone explain this to
    > me? Without such a library, it seems my only option would be to run an
    > operating system call. However if I did so, a user would need to type
    > in a password during execution. That seems sub-optimal.


    Use a key pair for authentication; invoke scp via system(); use
    ssh-agent to avoid repeatedly entering the passphrase that protects the
    private key. (Or have no passphrase protecting it, if that's acceptably
    secure for your environment.)

    --
    http://www.greenend.org.uk/rjk/
    Richard Kettlewell, Feb 10, 2011
    #5
  6. Saqib Ali

    Paul Guest

    "James Kanze" <> wrote in message
    news:...
    > On Feb 10, 12:37 pm, "Paul" <> wrote:
    >> "Jasen Betts" <> wrote in message

    >
    >> news:ij0io3$nqb$...

    >
    >> > On 2011-02-10, Saqib Ali <> wrote:

    >
    >> >> I'm writing C++ code that is running on a Solaris system.

    >
    >> >> From within my C++ program, I need to copy files from one machine to
    >> >> another using SFTP or SCP. I would like to learn how to do so
    >> >> programatically.

    >
    >> > every operating system is different, so there's no standard library
    >> > for spawing external programs.

    >
    >> >> There seems to be no standard C++ library (such as iostream, strings,
    >> >> etc) that would do this. I don't know why. Can anyone explain this to
    >> >> me?

    >
    >> > ms windows doesn't have naything remotely like fork() or exec() or even
    >> > a real argv[]. it's basically too hard to do that sort of thing in
    >> > a standardisable way.

    >
    >> CreateProcess()

    >
    > Which isn't anything like fork() or exec().


    Since you disputing the similarities what is the difference between
    createprocess and fork?

    <snip>
    Paul, Feb 10, 2011
    #6
  7. Saqib Ali

    Jasen Betts Guest

    On 2011-02-10, Paul <> wrote:
    >
    > "Jasen Betts" <> wrote in message
    > news:ij0io3$nqb$...
    >> On 2011-02-10, Saqib Ali <> wrote:
    >>>
    >>> Hi.
    >>>
    >>> I'm writing C++ code that is running on a Solaris system.

    >>
    >>> From within my C++ program, I need to copy files from one machine to
    >>> another using SFTP or SCP. I would like to learn how to do so
    >>> programatically.

    >>
    >> every operating system is different, so there's no standard library
    >> for spawing external programs.
    >>
    >>> There seems to be no standard C++ library (such as iostream, strings,
    >>> etc) that would do this. I don't know why. Can anyone explain this to
    >>> me?

    >>
    >> ms windows doesn't have naything remotely like fork() or exec() or even
    >> a real argv[]. it's basically too hard to do that sort of thing in
    >> a standardisable way.
    >>

    > CreateProcess()


    like I said nothing like fork or exec, no argv[] support.

    (posix allows passing an array if arbitrary NUL-terminated strings via argv
    windows doesn't, some things can be done by using quote marks but this
    is not a general solution, and different things interpret quote marks
    in the command-line differently)

    --
    ⚂⚃ 100% natural
    Jasen Betts, Feb 11, 2011
    #7
  8. Saqib Ali

    Jasen Betts Guest

    On 2011-02-10, Paul <> wrote:

    >>> > ms windows doesn't have naything remotely like fork() or exec() or even
    >>> > a real argv[]. it's basically too hard to do that sort of thing in
    >>> > a standardisable way.

    >>
    >>> CreateProcess()

    >>
    >> Which isn't anything like fork() or exec().

    >
    > Since you disputing the similarities what is the difference between
    > createprocess and fork?


    The most obvious differene is the number of parameters, fork takes none.

    fork creates as a child, a clone of the running process

    The clone interprets all the heritable handles and all the other state
    of the parent process (all the structures in memory)

    (it's not really a copy, the memory pages are just marked "shared" and
    "copy-on-write") so if the child doesn't do much before giving up the
    context (eg by calling exec() or or exit() ) there's not a large overhead
    associated with the fork call.

    Exec() does many of the lesser things createprocess() does, but the big
    thing, it oesn't do. it does not create a new process, it recycles
    the current one, this can be useful if you want to tweak the
    environment a bit before starting some other process, or if you want
    to renew the current context without explicitly undoing all the stuff
    you've done and without your invoker seeing you exit.

    --
    ⚂⚃ 100% natural
    Jasen Betts, Feb 11, 2011
    #8
  9. Saqib Ali

    Paul Guest

    "Jasen Betts" <> wrote in message
    news:ij36ta$e65$...
    > On 2011-02-10, Paul <> wrote:
    >
    >>>> > ms windows doesn't have naything remotely like fork() or exec() or
    >>>> > even
    >>>> > a real argv[]. it's basically too hard to do that sort of thing in
    >>>> > a standardisable way.
    >>>
    >>>> CreateProcess()
    >>>
    >>> Which isn't anything like fork() or exec().

    >>
    >> Since you disputing the similarities what is the difference between
    >> createprocess and fork?

    >
    > The most obvious differene is the number of parameters, fork takes none.
    >
    > fork creates as a child, a clone of the running process
    >
    > The clone interprets all the heritable handles and all the other state
    > of the parent process (all the structures in memory)
    >
    > (it's not really a copy, the memory pages are just marked "shared" and
    > "copy-on-write") so if the child doesn't do much before giving up the
    > context (eg by calling exec() or or exit() ) there's not a large overhead
    > associated with the fork call.
    >
    > Exec() does many of the lesser things createprocess() does, but the big
    > thing, it oesn't do. it does not create a new process, it recycles
    > the current one, this can be useful if you want to tweak the
    > environment a bit before starting some other process, or if you want
    > to renew the current context without explicitly undoing all the stuff
    > you've done and without your invoker seeing you exit.
    >
    > --
    > ⚂⚃ 100% natural


    Yes ok they are different but basically they all create a new process. There
    is still a similarity given that do you not agree. IDK if createprocess can
    create a child process of its parent or if there is soem other win fucntion
    that can do this.
    Paul, Feb 11, 2011
    #9
  10. On Wed, 09 Feb 2011 20:53:38 -0800, Saqib Ali wrote:

    > Hi.
    >
    > I'm writing C++ code that is running on a Solaris system.
    >
    > From within my C++ program, I need to copy files from one machine to
    > another using SFTP or SCP. I would like to learn how to do so
    > programatically.
    >
    > There seems to be no standard C++ library (such as iostream, strings,
    > etc) that would do this. I don't know why. Can anyone explain this to
    > me? Without such a library, it seems my only option would be to run an
    > operating system call. However if I did so, a user would need to type in
    > a password during execution. That seems sub-optimal.
    >
    > I wish there was a function that looked like this:
    >
    > scp(sourceFile, targetMachine, targetUser, targetPassword, targetFile)
    >
    > What do other people do in this situation?
    >
    > Thanks much.
    > - Saqib


    Use libssh or libssh2. Google will tell you more.
    James H. Markowitz, Feb 11, 2011
    #10
  11. Saqib Ali

    Nobody Guest

    On Fri, 11 Feb 2011 12:22:51 +0000, Paul wrote:

    >
    > Yes ok they are different but basically they all create a new process. There
    > is still a similarity given that do you not agree. IDK if createprocess can
    > create a child process of its parent or if there is soem other win fucntion
    > that can do this.


    The point is that the model is fundamentally different. Where Windows
    provides a single function, Unix has two separate functions which can be
    used independently. You can create a new process which continues to run
    the same program (from the same point, rather than from the beginning), or
    you can run a new program within the current process (retaining most of
    the process' entities, e.g. open file descriptors which aren't explicitly
    marked close-on-exec).

    Even comparing CreateProcess to a combined fork+exec, the Windows approach
    provides for a fixed set of options (change current directory, change
    environment, redirect std{in,out,err}), while the split model allows
    arbitrary changes to the child process prior to the exec. Also, exec()
    allows you to specify the exact argv[] which will be passed to the child's
    main(), whereas Windows passes a string and leaves it up to the program to
    parse this into arguments (using some pretty bizarre quoting rules, btw).

    The common functionality can seen in Python's subprocess module, which
    provides a reasonable trade-off between functionality and portability.
    Nobody, Feb 11, 2011
    #11
  12. Saqib Ali

    Paul Guest

    "Nobody" <> wrote in message
    news:p...
    > On Fri, 11 Feb 2011 12:22:51 +0000, Paul wrote:
    >
    >>
    >> Yes ok they are different but basically they all create a new process.
    >> There
    >> is still a similarity given that do you not agree. IDK if createprocess
    >> can
    >> create a child process of its parent or if there is soem other win
    >> fucntion
    >> that can do this.

    >
    > The point is that the model is fundamentally different. Where Windows
    > provides a single function, Unix has two separate functions which can be
    > used independently. You can create a new process which continues to run
    > the same program (from the same point, rather than from the beginning), or
    > you can run a new program within the current process (retaining most of
    > the process' entities, e.g. open file descriptors which aren't explicitly
    > marked close-on-exec).
    >
    > Even comparing CreateProcess to a combined fork+exec, the Windows approach
    > provides for a fixed set of options (change current directory, change
    > environment, redirect std{in,out,err}), while the split model allows
    > arbitrary changes to the child process prior to the exec. Also, exec()
    > allows you to specify the exact argv[] which will be passed to the child's
    > main(), whereas Windows passes a string and leaves it up to the program to
    > parse this into arguments (using some pretty bizarre quoting rules, btw).
    >
    > The common functionality can seen in Python's subprocess module, which
    > provides a reasonable trade-off between functionality and portability.
    >
    >

    Ok I'll take your word for it, I don't know enough about this to create an
    arguement, and I'm not pro-ms anyway. I thought Createprocess was a similar
    to fork and exec as it created a new process.

    TY for your explanation.
    Paul, Feb 11, 2011
    #12
  13. > There seems to be no standard C++ library (such as iostream, strings,
    > etc) that would do this. I don't know why. Can anyone explain this to
    > me? Without such a library, it seems my only option would be to run an
    > operating system call. However if I did so, a user would need to type
    > in a password during execution. That seems sub-optimal.
    >
    > I wish there was a function that looked like this:
    >
    > scp(sourceFile, targetMachine, targetUser, targetPassword, targetFile)
    >
    > What do other people do in this situation?


    system() to call scp?

    --
    @~@ Might, Courage, Vision, SINCERITY.
    / v \ Simplicity is Beauty! May the Force and Farce be with you!
    /( _ )\ (x86_64 Ubuntu 9.10) Linux 2.6.37
    ^ ^ 18:55:01 up 5 days 3:50 1 user load average: 0.00 0.04 0.05
    ä¸å€Ÿè²¸! ä¸è©é¨™! ä¸æ´äº¤! ä¸æ‰“交! ä¸æ‰“劫! ä¸è‡ªæ®º! è«‹è€ƒæ…®ç¶œæ´ (CSSA):
    http://www.swd.gov.hk/tc/index/site_pubsvc/page_socsecu/sub_addressesa
    Man-wai Chang, Feb 13, 2011
    #13
  14. Saqib Ali

    Adrian Guest

    On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    > system() to call scp?


    Man-wai idea would work. Although I would setup passwordless keys so no
    password is needed.

    Adrian

    #include <iostream>
    #include <cstdlib>
    #include <sstream>
    #include <string>

    void scp(const std::string &sourceFile, const std::string
    &targetMachine, const std::string &targetUser, const std::string
    &targetFile)
    {
    std::eek:stringstream strm;
    strm << "/usr/bin/scp " << sourceFile << ' ' << targetUser << '@' <<
    targetMachine << ':' << targetFile;
    system(strm.str().c_str());
    }

    int main(int argc, char *argv[])
    {
    scp("foo.txt", "box1", "userbar", ".");

    return 0;
    }
    Adrian, Feb 17, 2011
    #14
  15. Saqib Ali

    J G Miller Guest

    On Thursday, February 17th, 2011 at 11:25:53h -0700, Adrian wrote:

    > On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    >> system() to call scp?

    >
    > Man-wai idea would work.


    Yes it would, except one of the first things that is usually
    taught in a system programming course is, do not use
    system() because it is potentially insecure; instead use fork()
    followed by exec()
    J G Miller, Feb 17, 2011
    #15
  16. Saqib Ali

    Ian Collins Guest

    On 02/18/11 07:25 AM, Adrian wrote:
    > On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    >> system() to call scp?

    >
    > Man-wai idea would work.


    One downside of system() the caller doesn't know if the command
    succeeded or failed.

    --
    Ian Collins
    Ian Collins, Feb 17, 2011
    #16
  17. Saqib Ali

    Adrian Guest

    On Feb 17, 1:29 pm, J G Miller <mil...@yoyo_ORG> wrote:
    > On Thursday, February 17th, 2011 at 11:25:53h -0700, Adrian wrote:
    > > On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    > >> system() to call scp?

    >
    > > Man-wai idea would work.

    >
    > Yes it would, except one of the first things that is usually
    > taught in a system programming course is, do not use
    > system() because it is potentially insecure; instead use fork()
    > followed by exec()


    But I am posting from comp.lang.c++ and fork() doesn't exist ;-)

    Adrian
    Adrian, Feb 18, 2011
    #17
  18. Saqib Ali

    Adrian Guest

    On Feb 17, 1:52 pm, Ian Collins <> wrote:
    > On 02/18/11 07:25 AM, Adrian wrote:
    >
    > > On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    > >> system() to call scp?

    >
    > > Man-wai idea would work.

    >
    > One downside of system() the caller doesn't know if the command
    > succeeded or failed.


    Would not the scp return other the 0 back through system() to indicate
    something went wrong. Not perfect and other errors (ie missing scp)
    could also cause failures.

    Adrian
    Adrian, Feb 18, 2011
    #18
  19. Saqib Ali

    Ian Collins Guest

    On 02/18/11 05:00 PM, Adrian wrote:
    > On Feb 17, 1:52 pm, Ian Collins<> wrote:
    >> On 02/18/11 07:25 AM, Adrian wrote:
    >>
    >>> On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    >>>> system() to call scp?

    >>
    >>> Man-wai idea would work.

    >>
    >> One downside of system() the caller doesn't know if the command
    >> succeeded or failed.

    >
    > Would not the scp return other the 0 back through system() to indicate
    > something went wrong. Not perfect and other errors (ie missing scp)
    > could also cause failures.


    What the system() function returns is implementation defined. You'll
    have to check your system documentation.

    --
    Ian Collins
    Ian Collins, Feb 18, 2011
    #19
  20. Saqib Ali

    Ian Collins Guest

    On 02/18/11 04:58 PM, Adrian wrote:
    > On Feb 17, 1:29 pm, J G Miller<mil...@yoyo_ORG> wrote:
    >> On Thursday, February 17th, 2011 at 11:25:53h -0700, Adrian wrote:
    >>> On 02/13/2011 04:05 AM, Man-wai Chang wrote:
    >>>> system() to call scp?

    >>
    >>> Man-wai idea would work.

    >>
    >> Yes it would, except one of the first things that is usually
    >> taught in a system programming course is, do not use
    >> system() because it is potentially insecure; instead use fork()
    >> followed by exec()

    >
    > But I am posting from comp.lang.c++ and fork() doesn't exist ;-)


    But you cross posted to a Linux group!

    --
    Ian Collins
    Ian Collins, Feb 18, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xavier

    Classes SSH-SCP for Java

    Xavier, Oct 27, 2004, in forum: Java
    Replies:
    9
    Views:
    10,122
    Michael Borgwardt
    Nov 5, 2004
  2. mark | r

    scp crit 2

    mark | r, Dec 6, 2005, in forum: HTML
    Replies:
    7
    Views:
    633
    mark | r
    Dec 15, 2005
  3. Michael

    scp in python

    Michael, Sep 17, 2003, in forum: Python
    Replies:
    4
    Views:
    10,397
    David Mertz
    Sep 23, 2003
  4. krabhi
    Replies:
    1
    Views:
    9,250
    Marco Meschieri
    Aug 9, 2006
  5. Une Bévue
    Replies:
    0
    Views:
    363
    Une Bévue
    Jul 22, 2008
Loading...

Share This Page