473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Jul 22 '05 #1
2 4331
Hoegje wrote:
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.

Jul 22 '05 #2
Hoegje wrote:
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.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
1
by: elrik | last post by:
I want catch stdout and stderr of an child process and read them with the parent process. It's like popen4 but wihout shell commande. something like that : r, w = os.pipe() input=...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
0
by: spacelabstudio | last post by:
Hi, I'm observing some weird behavior and have written the following test program to demonstrate. This works under cygwin/WinXP but not Gentoo(kernel 2.6): huh.py...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
2
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
4
by: AK | last post by:
Hi everyone, Please check out this query that I have in : http://groups.google.com/group/progav/browse_thread/thread/29057ec587dd46b3 (its a bit long, thats why I am not typing it again;...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.