The UNIX and Linux Forums  

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


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can I use pipe command ? bintaleb UNIX for Dummies Questions & Answers 11 04-16-2007 12:16 PM
Problem with pipe into sed 98_1LE Shell Programming and Scripting 2 08-13-2005 01:40 PM
pipe command zomboo UNIX for Dummies Questions & Answers 1 10-31-2004 02:04 PM
PIpe Spy jodders High Level Programming 11 02-18-2004 06:44 AM
pipe help bb666 High Level Programming 5 02-26-2002 12:07 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #2 (permalink)  
Old 04-05-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,249
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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).
Reply With Quote
  #3 (permalink)  
Old 04-07-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 211
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #4 (permalink)  
Old 04-07-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,249
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The odd port number suggests to me that this is not strictly telnet protocol, but of course, we are left to speculate.
Reply With Quote
  #5 (permalink)  
Old 04-07-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 211
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Understood, thus the 'if'.
Reply With Quote
  #6 (permalink)  
Old 04-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Angry 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
Reply With Quote
Google UNIX.COM
Reply

Tags
solaris

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process find grep find mtime find null character in a unix file from ip can we get machine name +unix glance unix grep multiple lines grep or grep recursive how to redirect console logs in unix inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 12:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101