0

How to read characters printed on a process's own control terminal ? Or is this even possible under Linux ?

My program needs to launch an SSH client and start a password authenticated session (C/C++ programming language), so injecting the password into the SSH client's control terminal is required. Since the SSH client and my program share the same control terminal, password injection can be achieved through system call (man tty_ioctl):

ioctl(STDIN_FILENO, TIOCSTI, password)

However the injecting must be at the right time, if password injected too early, the injected data will be ignored, so my program has to wait for the password prompt "foobar's password:" before injecting password. Currently my solution is to wait a few seconds before the injection, which is very unreliable.

So what is right time to inject the password into the control terminal ?

I don't want to create a new pseudo-terminal, too many terminal related things have to be handled.

user416983
  • 974
  • 3
  • 18
  • 28
  • Have a look at the [Expect](http://expect.nist.gov/) program. You can also embed it in your C program by using [libexpect(3)](http://docs.activestate.com/activetcl/8.4/expect/libexpect.3.html). – Cristian Ciupitu Sep 27 '13 at 13:51
  • @ChrisWue, while that question is indeed similar with this one, it or its answers don't deal with the case when the application uses the terminal *directly* instead of the usual `stdin` and `stdout`. – Cristian Ciupitu Sep 27 '13 at 13:57
  • @ChrisWue I checked the source code of openssh, the password prompt is printed on it's control terminal (/dev/tty), so popen() definitely won't work. I have no experience on libexpect, not sure libexpect would handle various terminal related things for me, for example handle certain signals. – user416983 Sep 29 '13 at 03:29
  • I'm considering using ptrace() to implement this feature, with ptrace() I can intercept all the system calls of my program's child process, so in theory it's doable, although lots of work needs to be done. – user416983 Sep 29 '13 at 03:40
  • @user416983: Wouldn't it be easier to include libssh in your program rather than hacking around? – ChrisWue Sep 29 '13 at 04:24

0 Answers0