pipe(), fork(), exec(), ... providing input to and capturing output of a child process

H

Hoegje

I am writing a C++ program, which should create a sub- process to
start a telnet session to another server. Then it should login to that
server (on the telnet login) and execute one or more command(s) on the
remote server. The C++ program provides all the input for this process
(username, password, servername, commands, ...) and should capture all
the output returned by the telnet session process inside some
variable. How can this be accomplished ? This is what I have so far,
but since I'm not very good at working with pipes and children, I need
the experts help on this one..

Thanks in advance.


#include <string>
#include <stdio.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>

using namespace std;

int main(){

string telnetString = "telnet";
char* arguments[2];
arguments[0] = "telnet";
arguments[1] = "myServerName";

string loginname = "muLogin\n";
string password = "myPassword\n";
//string command = "cd public_html";
string command = "ls -l > listing.txt\n";
string command2 = "cat index.html";
string strExit = "exit\n";


int fd1[2];
int fd2[2];
int pipetest;
int pid;

pipetest = pipe(fd1);
if(pipetest != 0) exit(1); // or ptest == -1

pipetest = pipe(fd2);
if(pipetest != 0) exit(1); // or ptest == -1

{
// Pipe OK


if( (pid = fork()) == 0)
{ // child


////
// Copy reading end of the pipe.
////

dup2(fd1[0], fileno(stdin));

////
// Copy writing end of the pipe.
////

dup2(fd2[1], fileno(stdout));

close(fd2[1]);
dup2(fd2[0], 1);

////
// Copy stderr too
////
// dup2(fd2[1], fileno(stderr));

////
// execute subprocess
// if it returns, there was an error
////
cerr << "Starting session" << endl;
execv( telnetString.c_str() , arguments );
}


int output;
char c;

////
// Do the telnet (= child) login stuff
// end execute the commands (just one for now)
////
cerr << "starting login" << endl;
write( fd1[1], loginname.c_str(),
loginname.size() );
cerr << "loginname" << endl;
write( fd1[1], password.c_str(),
password.size() );
cerr << "pass" << endl;
write( fd1[1], command.c_str(), command.size()
);
cerr << "command" << endl;
write( fd1[1], strExit.c_str(), strExit.size()
);
cerr << "exit1" << endl;
write( fd1[1], strExit.c_str(), strExit.size()
);
cerr << "exit2" << endl;

////
// Find some way to capture the output of the
telnet process
// in this C++ program, to parse it later on.
////
/* while( read(fd2[0], &c, 1) != 0 ){
cerr << c;
}
exit(0);
*/
}

return 0;
};
 
G

Grumble

Hoegje said:
I am writing a C++ program, which should create a sub- process to
start a telnet session to another server. Then it should login to that
server (on the telnet login) and execute one or more command(s) on the
remote server. The C++ program provides all the input for this process
(username, password, servername, commands, ...) and should capture all
the output returned by the telnet session process inside some
variable. How can this be accomplished ? This is what I have so far,
but since I'm not very good at working with pipes and children, I need
the experts help on this one..

I'm afraid your question is off-topic in this group. You might have
better luck in comp.unix.programmer or comp.unix.questions

You might want to look at the popen() function, if available,
although it might not be what you need.
 
G

Gianni Mariani

Hoegje said:
I am writing a C++ program, which should create a sub- process to
start a telnet session to another server. Then it should login to that
server (on the telnet login) and execute one or more command(s) on the
remote server. The C++ program provides all the input for this process
(username, password, servername, commands, ...) and should capture all
the output returned by the telnet session process inside some
variable. How can this be accomplished ? This is what I have so far,
but since I'm not very good at working with pipes and children, I need
the experts help on this one..

Try a better news-group. comp.lang.c++ discusses the "language".

As a hint:

You're far better off accessing the network functions directly. This
would even be better for portability reasons (using winsock).

Also, look at "chat" scripts.

And lastly, the best way to do this is using ssh where you can
authenticate without passwords. You may be able to find ssh libraries
that allow you to connect directly from an application.

However, wrapping telnet is probably more work than connecting directly
to the port. Look at CommonC++ or ACE.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top