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

S

Saqib Ali

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
 
J

Jasen Betts

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.
 
P

Paul

Jasen Betts said:
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()
 
J

James Kanze

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.
 
R

Richard Kettlewell

Saqib Ali said:
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.)
 
P

Paul

James Kanze said:
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>
 
J

Jasen Betts

Jasen Betts said:
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)
 
J

Jasen Betts

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.
 
P

Paul

Jasen Betts said:
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.

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.
 
J

James H. Markowitz

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.
 
N

Nobody

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.
 
P

Paul

Nobody said:
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.
 
M

Man-wai Chang

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
 
A

Adrian

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

J G Miller

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()
 
A

Adrian

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
 
A

Adrian

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
 
I

Ian Collins

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top