Running a Program From Within a Program

I am trying to write a C or C++ program to control a telnet session. For the purpose of this question we could simplify the issue by assuming that I am relaying questions and answers to a user at the console. That's a simplification, but close enough.

I started out by opening a socket to the telnet port 23. I soon discovered that the handshake is complicated and there is no good documentation on what I have to do to satisfy the thing. Please don't tell me to read the telnet RFC because I did and it is the furthest thing imaginable from instructions on the negotiation.

Next I checked into the possibility that cURL could handle it, but there is nothing approaching actual instructions on how to write such a program and when I type cURL telnet://xxx.xxx.xxx.xxx to see if at least I can do it interactively, it fails to relay things to and from the screen properly and freezes a lot.

Next, I checked into using popen, but I soon became convinced that I would need two pipes and simply don't know how to do it. I can control programs I write with a pair of pipes, but I am trying to control the telnet app that comes with Linux and this would seem to involve stdin and stdout. There is undoubtedly a way to run an interactive program from within C/C++ by taking over stdin and stdout, but I sure don't know how.

Next up was IPWorks by nsoftware, and they even have a Telnet class, but it is utterly undocumented except for a class reference, which would be very hard to write an app from.

I would be grateful for any help I could get with this. I have been writing code and Googling for a day. Thanks in advance.

Brandon
The standard way to have a Unix program launch another program in the manner of a shell is:

1. Create 3 pipes: one for stdin, one for stdout, one for stderr
2. Fork
3. Parent process closes the read end of the stdin pipe and the write ends of stdout/stderr
4. Child process closes fds 0, 1, and 2 (stdin, stdout, stderr)
5. Child process uses dup2() to duplicate the read end of stdin on fd 0
6. Child process uses dup2() to duplicate the write end of stdout on fd 1
7. Child process uses dup2() to duplicate the write end of stderr on fd 2
8. Child closes both ends of the 3 sets of pipe fd it got
9. If your process doesn't faithfully use the close-on-exec FD flag (who does?), then instead of doing step 8, just close all FDs > 2.
10. Child uses one of the exec() functions to run the program you want to launch

Yes, this may be my only option. I haven't ever done it, but it doesn't look impossible.
wouldn't you be better off using expect?

just a suggestion.
Maybe perl? I have used this library before

http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

Topic archived. No new replies allowed.