The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 04-04-2008
fafo77 fafo77 is offline
Registered User
 

Join Date: Feb 2008
Posts: 5
Angry 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 < 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) <= 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!!!
Reply With Quote
Forum Sponsor