From: K-mart Cashier on 2 Mar 2010 13:20 Let's say that I have a C program with the following line of code.. FILE *fp = popen("telnet robotics.eecs.berkeley.edu", "w"); and when I get the login prompt, I have another line of code that enter my login name... (void)fprintf(fp, "%s\n", "some login name"); Here's the question. How would I wait for the login prompt to appear before (void)fprintf(fp, "%s\n", "some login name"); executes? Because right now, I have have sleep(5); (void)fprintf(fp, "%s\n", "some login name"); And from what I understand, popen() only tells me if the command was successful. It doesn't tell me when it has finished writing the entire output to the standard output.
From: John Gordon on 2 Mar 2010 14:28 In <101cd131-0fd0-4729-86e3-4fe059e05650(a)z10g2000prh.googlegroups.com> K-mart Cashier <cdalten(a)gmail.com> writes: > Let's say that I have a C program with the following line of code.. > FILE *fp = popen("telnet robotics.eecs.berkeley.edu", "w"); > and when I get the login prompt, I have another line of code that > enter my login name... > (void)fprintf(fp, "%s\n", "some login name"); > Here's the question. How would I wait for the login prompt to appear > before > (void)fprintf(fp, "%s\n", "some login name"); > executes? This may not answer your question, but... popen() is intended for use where you *only* care about the input or output of a command, but not both. If you're using it for something where you do actually care about the input and output, you should probably use some other IO mechanism such as pipes. In this specific case, it looks like the "expect" program would be perfect for your needs. From the expect homepage (expect.nist.gov): "Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff trivial. Expect is also useful for testing these same applications." -- John Gordon A is for Amy, who fell down the stairs gordon(a)panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"
From: Ersek, Laszlo on 2 Mar 2010 14:45 In article <101cd131-0fd0-4729-86e3-4fe059e05650(a)z10g2000prh.googlegroups.com>, K-mart Cashier <cdalten(a)gmail.com> writes: > Let's say that I have a C program with the following line of code.. > > FILE *fp = popen("telnet robotics.eecs.berkeley.edu", "w"); > > and when I get the login prompt, I have another line of code that > enter my login name... > > (void)fprintf(fp, "%s\n", "some login name"); > > > Here's the question. How would I wait for the login prompt to appear You would not. With popen(), you either write or read. http://www.opengroup.org/onlinepubs/007908775/xsh/popen.html If you want to both read and write, you have to create two pipes (or an AF_UNIX socket pair), and manually dup2(), fork() and exec() (or posix_spawn()); then use select(), pselect(), or poll() in the parent to find out whether to read or write (or perhaps both); or use aio. With the telnet client, the situation is worse, because the telnet client expects a teletype (ie. it expects that isatty([012]) all return true), so you'd have to open a pseudo-terminal pair, use its master side in the parent, and let telnet use the slave side. Such a pseudo-terminal pair is not binary transparent, the slave side has all the characteristics of a terminal (that's why telnet wants it). I suppose, for example, that if the slave side of the pseudo-terminal pair has the ISIG local mode flag set, and the parent process (your process) writes an INTR character to the master side, then the terminal driver will send a SIGINT to the telnet client, instead of transmitting a character. (Pseudo-terminal pairs are the basis of terminal emulators like xterm and ssh and telnet.) The opening of a pseudo-terminal pair was standardized as posix_openpt() and co., by SUSv3. http://www.opengroup.org/onlinepubs/000095399/functions/posix_openpt.html Are you sure you need this? Check out http://empty.sourceforge.net/ Cheers, lacos
From: K-mart Cashier on 2 Mar 2010 16:10 On Mar 2, 11:45 am, la...(a)ludens.elte.hu (Ersek, Laszlo) wrote: > In article <101cd131-0fd0-4729-86e3-4fe059e05...(a)z10g2000prh.googlegroups..com>, K-mart Cashier <cdal...(a)gmail.com> writes: > > > Let's say that I have a C program with the following line of code.. > > > FILE *fp = popen("telnet robotics.eecs.berkeley.edu", "w"); > > > and when I get the login prompt, I have another line of code that > > enter my login name... > > > (void)fprintf(fp, "%s\n", "some login name"); > > > Here's the question. How would I wait for the login prompt to appear > > You would not. With popen(), you either write or read. > > http://www.opengroup.org/onlinepubs/007908775/xsh/popen.html > > If you want to both read and write, you have to create two pipes (or an > AF_UNIX socket pair), and manually dup2(), fork() and exec() (or > posix_spawn()); then use select(), pselect(), or poll() in the parent to > find out whether to read or write (or perhaps both); or use aio. > > With the telnet client, the situation is worse, because the telnet > client expects a teletype (ie. it expects that isatty([012]) all return > true), so you'd have to open a pseudo-terminal pair, use its master side > in the parent, and let telnet use the slave side. Such a pseudo-terminal > pair is not binary transparent, the slave side has all the > characteristics of a terminal (that's why telnet wants it). I suppose, > for example, that if the slave side of the pseudo-terminal pair has the > ISIG local mode flag set, and the parent process (your process) writes > an INTR character to the master side, then the terminal driver will send > a SIGINT to the telnet client, instead of transmitting a character. > (Pseudo-terminal pairs are the basis of terminal emulators like xterm > and ssh and telnet.) > > The opening of a pseudo-terminal pair was standardized as posix_openpt() > and co., by SUSv3. > > http://www.opengroup.org/onlinepubs/000095399/functions/posix_openpt.... > > Are you sure you need this? Check out > > http://empty.sourceforge.net/ > I was just looking for an alternative to 'expect'.
From: Ersek, Laszlo on 2 Mar 2010 22:48 In article <c5736437-5b01-40bf-bf06-870b2c903b89(a)k2g2000pro.googlegroups.com>, K-mart Cashier <cdalten(a)gmail.com> writes: > On Mar 2, 11:45=A0am, la...(a)ludens.elte.hu (Ersek, Laszlo) wrote: >> http://empty.sourceforge.net/ >> > > I was just looking for an alternative to 'expect'. Then you found one :) Cheers, lacos
|
Pages: 1 Prev: Online SUS access Next: gethostbyname doesn't accept IPv6 addresses |