Unix Programming - Control stdin/stdout of an other program.. How can I do that ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2007 > Control stdin/stdout of an other program.. How can I do that ?





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 Control stdin/stdout of an other program.. How can I do that ?
Benoit Lefebvre

2007-09-14, 7:22 pm

Hi,
I know that ssh keys exists.. I know about ssh_agent and all these
things..
But I'm limited in my environment and I won't be able to change
these things to put an ssh key everywhere.

What I want to do is connect to a list of servers via ssh (OpenSSH)
and only get prompted for the password once (assuming the password is
the same for all the servers).

I would like my program to start ssh, get the "password:" prompt and
then issue the password. Once this is done, leave the session to me.
Like if I could send keys to a system() call.
Actually, this thing will be used to issue commands to many servers
at the same time to do basic administration.. like creating a user on
many systems.

I've seen in the past some softwares reading/writing to the passwd
command.. for example P-Synch from MTechIT.. the problem is that I
haven't seen the source code and I'm having problems finding what I am
looking for with the stdin/stdout/read/write keywords..

Anyone can refer me to an Open Source application that do soemthing
similar or a basic script example.

PS: I'm planning on developping in c/c++ on an AIX 5L environment

Thanks,
--Benoit Lefebvre

Maxim Yegorushkin

2007-09-14, 7:22 pm

On Sep 14, 7:31 pm, Benoit Lefebvre <benoit.lefeb...@gmail.com> wrote:

[]

> I would like my program to start ssh, get the "password:" prompt and
> then issue the password. Once this is done, leave the session to me.
> Like if I could send keys to a system() call.
> Actually, this thing will be used to issue commands to many servers
> at the same time to do basic administration.. like creating a user on
> many systems.
>
> I've seen in the past some softwares reading/writing to the passwd
> command.. for example P-Synch from MTechIT.. the problem is that I
> haven't seen the source code and I'm having problems finding what I am
> looking for with the stdin/stdout/read/write keywords..
>
> Anyone can refer me to an Open Source application that do soemthing
> similar or a basic script example.


Have you seen this: http://en.wikipedia.org/wiki/Expect

Gordon Burditt

2007-09-15, 1:38 am

> I know that ssh keys exists.. I know about ssh_agent and all these
>things..
> But I'm limited in my environment and I won't be able to change
>these things to put an ssh key everywhere.
>
> What I want to do is connect to a list of servers via ssh (OpenSSH)
>and only get prompted for the password once (assuming the password is
>the same for all the servers).


How long are you prepared to wait for the password prompt *if there
isn't one*? (if, for example, you're using a RSA or DSA key to log in
on this host). Or can you be sure there always will be one? Or that
you will always know in advance whether there is one? Also, are you
sure the ssh password is requested on stdin, as opposed to /dev/tty?


> I would like my program to start ssh, get the "password:" prompt and
>then issue the password. Once this is done, leave the session to me.


If you are controlling both stdin and stdout of another process, beware
of deadlock. This especially goes in the "leave the session to me" part.
Suppose, for example, that you shove a dump command down the pipe, and
the remote host runs it. If that dump command produces too much output
and you don't read it, but are shoving another command down following it,
you could end up with both processes waiting on write.

Ways to avoid deadlock include (a) use separate processes to handle stdin
and stdout, (b) use non-blocking I/O and select() or poll(), or (c) use
a pty instead of a pipe.

>Like if I could send keys to a system() call.
> Actually, this thing will be used to issue commands to many servers
>at the same time to do basic administration.. like creating a user on
>many systems.
>
> I've seen in the past some softwares reading/writing to the passwd
>command.. for example P-Synch from MTechIT.. the problem is that I
>haven't seen the source code and I'm having problems finding what I am
>looking for with the stdin/stdout/read/write keywords..


Look for the program "expect".

> Anyone can refer me to an Open Source application that do soemthing
>similar or a basic script example.

Pascal Bourguignon

2007-09-24, 7:31 am

Benoit Lefebvre <benoit.lefebvre@gmail.com> writes:

> Hi,
> I know that ssh keys exists.. I know about ssh_agent and all these
> things..
> But I'm limited in my environment and I won't be able to change
> these things to put an ssh key everywhere.
>
> What I want to do is connect to a list of servers via ssh (OpenSSH)
> and only get prompted for the password once (assuming the password is
> the same for all the servers).
>
> I would like my program to start ssh, get the "password:" prompt and
> then issue the password. Once this is done, leave the session to me.
> Like if I could send keys to a system() call.
> Actually, this thing will be used to issue commands to many servers
> at the same time to do basic administration.. like creating a user on
> many systems.
>
> I've seen in the past some softwares reading/writing to the passwd
> command.. for example P-Synch from MTechIT.. the problem is that I
> haven't seen the source code and I'm having problems finding what I am
> looking for with the stdin/stdout/read/write keywords..
>
> Anyone can refer me to an Open Source application that do soemthing
> similar or a basic script example.
>
> PS: I'm planning on developping in c/c++ on an AIX 5L environment


I think ssh-agent is designed to do what you want.
http://linuxmafia.com/pub/os2/stahl.../ssh-agent.html

--
__Pascal Bourguignon__ http://www.informatimago.com/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com