writing a pipe with a c telnet

Hi I have a problem writing a c program that makes a telnet connection and writes some command.
The shell command is something like this:
------------------------------------------------------------------
>
>telnet 141.111.231.132 3300
ENTER COMMAND: login "<--- I' wirte a command (ex login)"
RESP0; "<---Answer "
ENTER COMMAND: logout "<--- I' wirte a command (ex logout)"
RESP0; "<---Answer "
>
------------------------------------------------------------------

Now I need to write it in c. It's a program that does the telnet, catchs the answer and gives the command....

I have done:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <unistd.h>

#include <fcntl.h>

#define BUFFER 1024

#define READ 0
#define WRITE 1

popen2(const char *command, int *infp, int *outfp)
{
int p_stdin[2], p_stdout[2];
pid_t pid;

if \(pipe\(p_stdin\) != 0 || pipe\(p_stdout\) != 0\)
    return -1;

pid = fork\(\);

if \(pid &lt; 0\)
    return pid;
else if \(pid == 0\)
\{
    close\(p_stdin[WRITE]\);
    dup2\(p_stdin[READ], READ\);
    close\(p_stdout[READ]\);
    dup2\(p_stdout[WRITE], WRITE\);

   execl\("/bin/sh", "sh", "-c", command, NULL\);
    //execl\("command", command, NULL\);
    
    perror\("execl"\);
    exit\(1\);
\}

if \(infp == NULL\)
    close\(p_stdin[WRITE]\);
else
    *infp = p_stdin[WRITE];
// The way it was p_stdin[read] in this program is still open
if \(outfp == NULL\)
    close\(p_stdout[READ]\);
else
    *outfp = p_stdout[READ];
// as well as p_stdout[write], they're closed in the fork

close\(p_stdin[READ]\); // We only write to the forks input anyway
close\(p_stdout[WRITE]\); // and we only read from its output
return pid;

}
int main()
{
FILE *fp, *fp2;
FILE *shell;
int i, n, status;

char telnet_login[256];
char telnet_logout[256];

 n=sprintf\(telnet_path,"telnet %s %d\\n", "141.111.231.132", 3300\); 
 //n=sprintf\(telnet_path,"telnet.sh"\); 
 //printf\("%s \\n", telnet_path\);

 n=sprintf\(telnet_login,"LOGIN:%s:%s;", "aaa", "bbb"\);
// printf\("%s \\n", telnet_login\);

 n=sprintf\(telnet_logout,"%s;\\n", "LOGOUT"\);
 //printf\("%s \\n", telnet_logout\);

int infp, outfp;
//char buf[128];
char buf[BUFFER];
memset(buf,'\0',BUFFER);

if \(popen2\(telnet_path, &infp, &outfp\) &lt;= 0\)
\{
    printf\("Unable to exec sort\\n"\);
    exit\(1\);
\}

//write\(infp, telnet_login, strlen\(telnet_login\)\);
write\(infp, telnet_logout, strlen\(telnet_logout\)\);
//write\(infp, "exit\\n", strlen\("exit"\)\);
    
close\(infp\);

//*buf = '\\0';
sleep\(2\);

	read\(outfp, buf, BUFFER\);
printf\("buf = '%s'\\n", buf\);

return 0;

}

I'm not confident with execl and I'm not really shure I need to open a shell with execl. Maybe I can lunch the command directly....

Thanks for help!!!

Forking a telnet process just to read and write a network socket seems misdirected. You can open the socket straight from C and avoid the external processes altogether. Probably even simplifies your program (at the expense of having to figure out how to use sockets in C, but that's time well spent).

If you are connecting to a telnetd you will need to speak the telnet protocol.
Vanilla sockets aren't enough. Driving telnet, or any stdio buffered program (terminal emulated), via pipes is asking for pain.
Check out expect or read up on pty allocation, forkpty, etc....is my best advice.

The odd port number suggests to me that this is not strictly telnet protocol, but of course, we are left to speculate.

Understood, thus the 'if'. :slight_smile:

Yes you are right is not a straight telnet is more something about writing a c program that sends shell commands and retrive the answer.
It's really hard to sync the input and the output from shell...
I worked around with the code and I figurated out that my problem was with:
execl("/bin/sh", "sh", "-c", command, NULL);

now I'm able to execute execl without open a new shell.
This command works well:
execl("/usr/bin/telnet", command, ip_add, port, NULL);

where telnet it's just an alias on this solaris...
I still have problem on sync the write and reading part of the pipes...

Tks alot,
ff