[lug] reference/tutorial for connecting to port 23
Alan Robertson
alanr at suse.com
Wed Jul 26 12:50:26 MDT 2000
matthew.w.mcillece at lmco.com wrote:
>
> Does anyone know where I might find an explanation of how to automatically
> pass the output of a distinct process to (and through) a connection such as
> telnet? I've always expected telnet to be tied to the keyboard that
> initiated the connection, but now need to pass characters generated by
> another process through without user intervention, if possible. I've been
> reading an IRIX Network Programming Guide but the discussion is not what I
> can use. I've also grepped through all the Linux HOWTO's, but haven't found
> anything quite on topic.
Here's some GPLed code I wrote a week or so ago to do that:
/*
* Start a process with its stdin and stdout redirected to pipes
* so the parent process can talk to it.
*/
int
StartProcess(const char * cmd, int * readfd, int * writefd)
{
pid_t pid;
int wrpipe[2]; /* The pipe the parent process writes to */
/* (which the child process reads from) */
int rdpipe[2]; /* The pipe the parent process reads from */
/* (which the child process writes to) */
if (pipe(wrpipe) < 0) {
perror("cannot create pipe\n");
return(-1);
}
if (pipe(rdpipe) < 0) {
perror("cannot create pipe\n");
close(wrpipe[0]);
close(wrpipe[1]);
return(-1);
}
switch(pid=fork()) {
case -1: perror("cannot StartProcess cmd");
close(rdpipe[0]);
close(wrpipe[1]);
close(wrpipe[0]);
close(rdpipe[1]);
return(-1);
case 0: /* We are the child */
/* Redirect stdin */
close(0);
dup2(wrpipe[0], 0);
close(wrpipe[0]);
close(wrpipe[1]);
/* Redirect stdout */
close(1);
dup2(rdpipe[1], 1);
close(rdpipe[0]);
close(rdpipe[1]);
execlp("/bin/sh", "sh", "-c", cmd, NULL);
perror("cannot exec shell!");
exit(1);
default: /* We are the parent */
*readfd = rdpipe[0];
close(rdpipe[1]);
*writefd = wrpipe[1];
close(wrpipe[0]);
return(pid);
}
/*NOTREACHED*/
return(-1);
}
Hope this helps... [I specifically use it with telnet...]
-- Alan Robertson
alanr at suse.com
More information about the LUG
mailing list