writing a pipe with a c telnet


 
Thread Tools Search this Thread
Top Forums Programming writing a pipe with a c telnet
# 1  
Old 04-04-2008
Network 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!!!
# 2  
Old 04-05-2008
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).
# 3  
Old 04-07-2008
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.
# 4  
Old 04-07-2008
The odd port number suggests to me that this is not strictly telnet protocol, but of course, we are left to speculate.
# 5  
Old 04-07-2008
Understood, thus the 'if'. Smilie
# 6  
Old 04-08-2008
Network Tk you but...

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. UNIX for Dummies Questions & Answers

Difference Between Krb5-telnet And Ekrb5-telnet

Hi, I want to know the difference between these two services. Both are under xinetd. Both are used for enabling and disabling Telnet service. So, can somebody please explain me the difference between the two ? Thanks in advance :) (0 Replies)
Discussion started by: kashifsd17
0 Replies

3. UNIX for Dummies Questions & Answers

pipe > output

I can use pipe output to a file. For example ./somescript.sh > output.txt But for example if the output from ./somescript.sh is slow. like if it prints one line every minute then output.txt is not updated every minute. Lines are written to output.txt in one go, hence have to wait for the whole... (2 Replies)
Discussion started by: kevincobain2000
2 Replies

4. UNIX for Dummies Questions & Answers

xargs vs. pipe

I have been using unix on and off for a number of years. I am not a sys admin. I use what I need. I have googled this, but I really can't figure out what is the difference between using xarg and just using a regular pipe? Why do I need to include xarg sometimes and how do I know when I need it? (2 Replies)
Discussion started by: guessingo
2 Replies

5. UNIX for Dummies Questions & Answers

Automatically login in the telnet from present telnet

Hi, I was writing one script which includes to switch to the another telnet automatically from the present telnet server. I was using rlogin but firstly it takes the same user name of the present telnet and secondly it is prompting for the password. But i want to switch to the another telnet... (2 Replies)
Discussion started by: Prateek
2 Replies

6. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

7. Shell Programming and Scripting

How can I use pipe

Hi, guys: I am working on my shell using c. How can I use pipe to implement the following? ls -l 1>> | grep hellp 1<< 2>> | less 2<< (the output of ls goes to grep, and the output of grep goes to less) Thanks Please use and tags when posting code, data or logs etc. to preserve... (1 Reply)
Discussion started by: tomlee
1 Replies

8. Shell Programming and Scripting

Webpage to Telnet via Perl and Expect: Telnet problem?

Somewhat long story: I have a simple Perl CGI script that uses Expect to Telnet to a device and grab some data, and then spits it back to Perl for display on the Webpage. This works for many devices I've tried, but one device just fails, it keeps rejecting the password on this device, only... (1 Reply)
Discussion started by: jondo
1 Replies

9. UNIX for Dummies Questions & Answers

broken pipe?

Hi there, I try to use: > find * | ls but it just gave me one level of ' ls '. and it said: > find * | ls dir1 dir2 f1 f2 f3 Broken Pipe > what is broken pipe? how can i fix it? thks Gusla (3 Replies)
Discussion started by: gusla
3 Replies

10. Programming

pipe help

i made a lot of processes. here is the code: main() { printf("\nEnter K="); scanf("%d",&k); printf("Enter L="); scanf("%d",&l); printf("\nFather id=%d\n",getpid()); x=0; makechild(); sleep(2); return 1; } int makechild() { for(q=1;q<=k;q++) { if(f=fork()) { ... (5 Replies)
Discussion started by: bb666
5 Replies
Login or Register to Ask a Question