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;
};
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;
};