UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2004-11-03 04:03 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

#include <stdio.h>
#include <expect.h>

// gcc -lexpect5.32 -ltcl8.3 -lm -o x test.c

int main(){

FILE *fp1;
int ec;
char buff[128];

exp_loguser = 1;
exp_timeout = 3600;



if (0 == (fp1 = exp_popen("gdb"))) {

        printf("exp_popen failed\n");
        exit(-1);
}

while(1){

        if (0 > exp_fexpectl(fp1,exp_glob,"(gdb)",0,exp_end)) exit(-1);
        fgets(buff, sizeof(buff), stdin);
        fprintf(fp1,"%s", buff);
}

return(0);
}

Offline

#2 2004-11-03 10:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

1. Well, I've never used libexpect, but taking a quick look at the
man pages and header files, try adding this:

exp_stty_init = "-echo";

2. I'm guessing probably because you have no "*.a" static version
of libexpect (or, one of the other libs you link against) installed...

Offline

#3 2004-11-04 03:00 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

exp_stty_init = "-echo";

Offline

#4 2004-11-04 03:09 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

Offline

#5 2004-11-04 10:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

Offline

#6 2004-11-08 08:37 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

Offline

#7 2004-11-08 09:19 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

Well, CTRL-D generally doesn't generate a signal...  Typically,
it merely simulates EOF...  Ie: if you're manually typing in input to
some command, you can make it believe it has reached end of
file by hitting CTRL-D...

As for the other problem, I have no idea what library openpty() is
supposed to be in, but you just need to find it (and, hope it has a
"*.a" static version), and then just link it in as well...

Oh, hell, I just decided to search through my various system
libs, and I found openpty() in "libutil"...  So, try throwing "-lutil" into
your command-line, or if that doesn't work, hunt around your
system for "libutil.a" (or similar), and pass that to gcc, as well...

Offline

#8 2004-11-09 02:37 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

Offline

#9 2004-11-09 02:57 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

All I did was a simple for loop through various libs, printing them
and passing them to "nm | grep", as well...  The "find" approach
is just as good, if not easier...

It sounds like the pty isn't getting initialized properly, or something...
Are "exp_ttycopy" and "exp_ttyinit" set to 1?  Maybe throw "sane"
into the "exp_stty_init" options, as well? *shrug*

Offline

#10 2004-11-09 08:57 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

fgets(buff, sizeof(buff), stdin);
fprintf(hnd,"%s", buff);

Offline

#11 2004-11-09 09:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

Offline

#12 2004-11-10 03:40 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

char ch;
struct termios old, new;

exp_loguser = 1;
exp_timeout = 3600;
//exp_stty_init = "-echo";
exp_ttyinit = 1;
exp_ttycopy = 1;

hnd = exp_popen(sh);

while(1){

        if (0 > exp_fexpectl(hnd, exp_glob, "$" , 0, exp_end)) exit(1);

        tcgetattr (0, &old);
        memcpy (&new, &old, sizeof (struct termios));
        new.c_lflag &= ~(ICANON | ECHO);
        tcsetattr (0, TCSANOW, &new);
        read (0, &ch, 1);
        write(fileno(hnd), &ch, 1);
        tcsetattr (0, TCSANOW, &old);
        fflush(hnd);
}

Offline

#13 2004-11-11 12:05 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

Yeah, the problem is you do exp_fexpectl() after every single char
of input, looking for a new shell prompt, which isn't going to come...
Why do you do that exp_fexpectl(), anyway??  Why not simply
just read as much data as it has to give you, and output it?  Or,
if you really want to use exp_fexpectl(), why not simply call it
without the search for "$", and with "exp_timeout" set to 0, so it
just reads as much as it can without blocking?  (Technically, you
should use select() on 0 (stdin) and the expect FD, and whenever
either one has data to read, read it and then send it to the other...)

And, there's no point in constantly switching back and forth between
canonical input mode and non...  Just set it to raw mode outside
the loop at the top, and restore outside at the bottom...

Offline

#14 2004-11-13 03:48 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

Offline

#15 2004-11-13 08:01 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Libexpect

exp_fexpectl(hnd, exp_end)

Offline

#16 2004-11-16 09:37 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Libexpect

Hi,

Well, what I can say ? YOU ARE THE GUY ROBSEACE!!!  :wink:

Thks a lottttttttttt!!! :)

Based in your code I understood better and make my code work correct!

Thks once again.

Regards,

Offline

#17 2017-11-14 11:38 AM

xdev
Member
Registered: 2017-11-14
Posts: 2

Re: Libexpect

Offline

Board footer

Powered by FluxBB