For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > Executing Unix commands on a remote host using C









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Executing Unix commands on a remote host using C
Bader

2005-09-28, 6:59 pm

How can I implement the following in C/C++:
1. Create a sub-process to start an SSH session to a remote host
2. Then using the created session within my program I need to
issue/send multiple commands to the remote host to run different
programs.

In particular I don't know how to send multiple commands using the same
SSH session (sub-process already created) in 1.

Thanks

Pascal Bourguignon

2005-09-28, 7:00 pm

"Bader" <baderA@gmail.com> writes:

> How can I implement the following in C/C++:
> 1. Create a sub-process


fork(2)

> to start


exec(2)

> an SSH session to a remote host


ssh(1)


> 2. Then using the created session within my program I need to
> issue/send multiple commands


popen(2)
pipe(2)
write(2)

> to the remote host to run different
> programs.
>
> In particular I don't know how to send multiple commands using the same
> SSH session (sub-process already created) in 1.


write(2)

--
__Pascal Bourguignon__ http://www.informatimago.com/
Måns Rullgård

2005-09-28, 7:00 pm

Pascal Bourguignon <spam@mouse-potato.com> writes:

> "Bader" <baderA@gmail.com> writes:
>
>
> fork(2)
>
>
> exec(2)
>
>
> ssh(1)
>
>
> popen(2)
> pipe(2)
> write(2)


popen(2) doesn't belong here. It does approximately fork(), exec(),
fdopen(), and returns a FILE*, which can be used for reading or
writing with the usual stdio functions. It does all of step 1, except
that it can't provide two-way communication, which the OP didn't say
whether he needed.

--
Måns Rullgård
mru@inprovide.com
Pascal Bourguignon

2005-09-28, 7:00 pm

Måns Rullgård <mru@inprovide.com> writes:

> Pascal Bourguignon <spam@mouse-potato.com> writes:
>
>
> popen(2) doesn't belong here.


Right, popen(2) doesn't exist. I meant popen(3).

--
__Pascal Bourguignon__ http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.
Måns Rullgård

2005-09-28, 7:00 pm

Pascal Bourguignon <spam@mouse-potato.com> writes:

> Måns Rullgård <mru@inprovide.com> writes:
>
>
> Right, popen(2) doesn't exist. I meant popen(3).


popen(3) doesn't belong with write(2) either, as I'm sure you know.

--
Måns Rullgård
mru@inprovide.com
Bader

2005-09-28, 7:00 pm

Thank you M=E5ns,

I am a little bit slow, could you just clarify what do you mean by the
numbers in the calls

fork(2)
exec(2)
and ssh(1)

because I am new to these calls

your help is highly appreciated

Erik Max Francis

2005-09-28, 7:00 pm

Bader wrote:

> I am a little bit slow, could you just clarify what do you mean by the
> numbers in the calls
>
> fork(2)
> exec(2)
> and ssh(1)
>
> because I am new to these calls
>
> your help is highly appreciated


It's the section of the man pages that the entry appears, e.g. man 2
fork or man 1 ssh.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I love Mickey Mouse more than any woman I've ever known.
-- Walt Disney
SM Ryan

2005-09-28, 7:00 pm

"Bader" <baderA@gmail.com> wrote:
# How can I implement the following in C/C++:

I would run expect and do the ssh session through expect.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Pascal Bourguignon

2005-09-29, 7:57 am

Måns Rullgård <mru@inprovide.com> writes:

> Pascal Bourguignon <spam@mouse-potato.com> writes:
>
>
> popen(3) doesn't belong with write(2) either, as I'm sure you know.


I don't know what you mean, but I know what I mean, and popen(3),
pipe(2) and write(2) DO BELONG to the anwer I guve to the OP.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.
Måns Rullgård

2005-09-29, 6:59 pm

Pascal Bourguignon <spam@mouse-potato.com> writes:

>
> I don't know what you mean, but I know what I mean, and popen(3),
> pipe(2) and write(2) DO BELONG to the anwer I guve to the OP.


How do you propose to use write(2) together with popen(3)? I would
have expected you to know better than mixing stdio with low-level
I/O. How you get pipe(2) into the mix is a complete mystery. Perhaps
you could illustrate with a small piece of code.

--
Måns Rullgård
mru@inprovide.com
Pascal Bourguignon

2005-09-29, 6:59 pm

Måns Rullgård <mru@inprovide.com> writes:

> Pascal Bourguignon <spam@mouse-potato.com> writes:
>
>
> How do you propose to use write(2) together with popen(3)?


write(fileno(popen("cat",O_WRONLY)),"What About this?",15);


> I would
> have expected you to know better than mixing stdio with low-level
> I/O.


I do whatever I want. You got a problem with that?


> How you get pipe(2) into the mix is a complete mystery. Perhaps
> you could illustrate with a small piece of code.


int fds[2];
pipe(fds);
if(0==fork()){
close(fds[1]);
dup2(fds[0],0);
execl("cat","cat",0);}
close(fds[0]);
write(fds[1],"What About that?",15);
close(fds[1]);


I would have exected you to know better how to mix unix syscalls to
get useful work done.

--
"Indentation! -- I will show you how to indent when I indent your skull!"
Måns Rullgård

2005-09-30, 6:59 pm

Pascal Bourguignon <spam@mouse-potato.com> writes:

> Måns Rullgård <mru@inprovide.com> writes:
>
>
> write(fileno(popen("cat",O_WRONLY)),"What About this?",15);


Just be sure not to use any stdio functions with that FILE* without
the usual precautions.

>
> I do whatever I want. You got a problem with that?


No, and you probably know how to do it safely. I wouldn't suggest it
to someone who doesn't know the basics.

>
> int fds[2];
> pipe(fds);
> if(0==fork()){
> close(fds[1]);
> dup2(fds[0],0);
> execl("cat","cat",0);}
> close(fds[0]);
> write(fds[1],"What About that?",15);
> close(fds[1]);
>
> I would have exected you to know better how to mix unix syscalls to
> get useful work done.


You're not using popen(3) there, which makes it perfectly sensible.

--
Måns Rullgård
mru@inprovide.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com