Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > expect.pm - stdout buffering issue

Reply
Thread Tools

expect.pm - stdout buffering issue

 
 
Andry
Guest
Posts: n/a
 
      09-23-2008
Hi all,
I have the following script:
************************************************** ****************
#!/usr/bin/perl -w
use Expect;

$timeout = 5;
$|=1;

$exp = new Expect();
$exp->raw_pty(1);

$exp->log_file("output.log", "w");

$exp->spawn("ssh -l username 10.17.39.29");
$exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
\n"); } ]);
$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
\n"); } ]);
sleep 2;
$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
\n"); } ]);
sleep 5;

$exp->log_file(undef);

$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
\n"); } ]);
************************************************** ****************

When I run the script, the console (stdout) shows:
1) ssh (login...)...
2) sleeping 2 seconds
3) "ls -l" output
4) sleeping 5 seconds
.... the script quits without displaying the output of the second
command "ls"
Even the log file does not contain the missing output.

Now if I put the following command at the end of the script:
$exp->soft_close();
Then the console shows the previously missing output ("ls") at the
very end of the script, but the log file does not include that yet.

It seems there is a general buffering problem with STDOUT.
How can I get things displayed and logged correctly and at the right
time?

Thanks,
Andrea
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-23-2008
Andry wrote:
> Hi all,
> I have the following script:
> ************************************************** ****************
> #!/usr/bin/perl -w
> use Expect;
>
> $timeout = 5;
> $|=1;
>
> $exp = new Expect();
> $exp->raw_pty(1);
>
> $exp->log_file("output.log", "w");
>
> $exp->spawn("ssh -l username 10.17.39.29");
> $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> \n"); } ]);
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> \n"); } ]);
> sleep 2;
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> \n"); } ]);
> sleep 5;
>
> $exp->log_file(undef);
>
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
> \n"); } ]);
> ************************************************** ****************
>
> When I run the script, the console (stdout) shows:
> 1) ssh (login...)...
> 2) sleeping 2 seconds
> 3) "ls -l" output
> 4) sleeping 5 seconds
> ... the script quits without displaying the output of the second
> command "ls"
> Even the log file does not contain the missing output.
>
> Now if I put the following command at the end of the script:
> $exp->soft_close();
> Then the console shows the previously missing output ("ls") at the
> very end of the script, but the log file does not include that yet.
>
> It seems there is a general buffering problem with STDOUT.
> How can I get things displayed and logged correctly and at the right
> time?


More likeley: your expect strings don't match and your script just times
out:

> $timeout = 5;

vs.
> 4) sleeping 5 seconds


There are two logging facilities:

1. $exp->exp_internal(1);
IIRC that will tell you what your script expects and what it actually
receives.

2. $exp->log_user(1)
Can't recall what that does

I usually set these tow if I have problems.

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-23-2008
Andry wrote:
> Hi all,
> I have the following script:
> ************************************************** ****************
> #!/usr/bin/perl -w
> use Expect;
>
> $timeout = 5;
> $|=1;
>
> $exp = new Expect();
> $exp->raw_pty(1);
>
> $exp->log_file("output.log", "w");
>
> $exp->spawn("ssh -l username 10.17.39.29");
> $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> \n"); } ]);
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> \n"); } ]);
> sleep 2;
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> \n"); } ]);


I just notice:

Maybe you want to send a \r rather than a \n?

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
Andry
Guest
Posts: n/a
 
      09-23-2008
On Sep 23, 1:00*pm, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> Andry wrote:
> > Hi all,
> > I have the following script:
> > ************************************************** ****************
> > #!/usr/bin/perl -w
> > use Expect;

>
> > $timeout = 5;
> > $|=1;

>
> > $exp = new Expect();
> > $exp->raw_pty(1);

>
> > $exp->log_file("output.log", "w");

>
> > $exp->spawn("ssh -l username 10.17.39.29");
> > $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> > \n"); } ]);
> > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> > \n"); } ]);
> > sleep 2;
> > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> > \n"); } ]);

>
> I just notice:
>
> Maybe you want to send a \r rather than a \n?
>
> Josef
> --
> These are my personal views and not those of Fujitsu Siemens Computers!
> Josef Möllers (Pinguinpfleger bei FSC)
> * * * * If failure had no penalty success would not be a prize (T.. *Pratchett)
> Company Details:http://www.fujitsu-siemens.com/imprint.html


Hi Josef,
The script is not timing out anywhere, even if I comment out
$timeout=5, the behavior is the same.
Even replacing \n with \r does not change anything.
I enabled the debug logging with exp_internal, but it doesn't look
very helpful in my case. Here is its output anyway (having the
soft_close command at the very end of the script):
************************************************** ******************
[root@fedora-c2 perl-scripts]# ./ssh.pl
Spawned 'ssh -l root 10.17.39.29'
spawn id(3)
Pid: 19863
Tty: /dev/pts/4
Expect::spawn('Expect=GLOB(0x9374dbc)','ssh -l root 10.17.39.29')
called at ./ssh.pl line 34
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 37
spawn id(3): list of patterns:
#1: -re `[Pp]assword'


spawn id(3): Does `'
match:
pattern #1: -re `[Pp]assword'? No.

root@10.17.39.29's password:
spawn id(3): Does `root@10.17.39.29\'s password: '
match:
pattern #1: -re `[Pp]assword'? YES!!
Before match string: `root@10.17.39.29\'s '
Match string: `password'
After match string: `: '
Matchlist: ()
Calling hook CODE(0x9377204)...
Sending 'atis4713\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x9374dbc)','atis4713\x {a}') called at ./
ssh.pl line 37
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x9395304)' ) called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 37
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 55
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `: '
match:
pattern #1: -re `svr-mila'? No.



spawn id(3): Does `: \n'
match:
pattern #1: -re `svr-mila'? No.

Last login: Tue Sep 23 14:20:30 2008 from 10.17.91.83
Welcome to the Milan TestLab server
If you're not part of the TestLab team, you're not welcome anymore...


spawn id(3): Does `: \nLast login: Tue Sep 23 14:20:30 2008 from
10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you\'re not
part of the TestLab team, you\'re not welcome anymore...\r\n\r\n'
match:
pattern #1: -re `svr-mila'? No.

[root@svr-mila-testlab2 ~]#
spawn id(3): Does `: \nLast login: Tue Sep 23 14:20:30 2008 from
10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you\'re not
part of the TestLab team, you\'re not welcome anymore...\r\n\r
\n[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `: \nLast login: Tue Sep 23 14:20:30 2008
from 10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you
\'re not part of the TestLab team, you\'re not welcome anymore...\r\n\r
\n[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x93772c4)...
Sending 'ls -l\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x9374dbc)','ls -l\x{a}') called at ./
ssh.pl line 53
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x93952f' ) called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 55
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 70
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.

ls -l
total 2360
-rw-r--r-- 1 root root 33980 Feb 8 2007 1
-rw------- 1 root root 1534 Feb 5 2007 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Feb 5 2007 Desktop
-rw-r--r-- 1 root root 38253 Feb 5 2007 install.log
-rw-r--r-- 1 root root 4475 Feb 5 2007 install.log.syslog
-rw-r--r-- 1 root root 2290308 Sep 23 14:21 sb-log.txt
-rw-r--r-- 1 root root 3034 Feb 6 2007 xorg.conf.new
drwxr-xr-x 2 root root 4096 Feb 7 2007 yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls -l\r\n\033[00mtotal 2360\r\n-rw-r--
r-- 1 root root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-rw------- 1
root root 1534 Feb 5 2007 \033[00manaconda-ks.cfg\033[00m\r
\ndrwxr-xr-x 2 root root 4096 Feb 5 2007 \033[01;34mDesktop
\033[00m\r\n-rw-r--r-- 1 root root 38253 Feb 5 2007
\033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root 4475 Feb 5
2007 \033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root root
2290308 Sep 23 14:21 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1 root
root 3034 Feb 6 2007 \033[00mxorg.conf.new\033[00m\r\ndrwxr-xr-x
2 root root 4096 Feb 7 2007 \033[0
1;34myum-keys\033[00m\r\n\033[m[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls -l\r\n\033[00mtotal 2360\r
\n-rw-r--r-- 1 root root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-
rw------- 1 root root 1534 Feb 5 2007 \033[00manaconda-ks.cfg
\033[00m\r\ndrwxr-xr-x 2 root root 4096 Feb 5 2007
\033[01;34mDesktop\033[00m\r\n-rw-r--r-- 1 root root 38253 Feb 5
2007 \033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root 4475
Feb 5 2007 \033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root
root 2290308 Sep 23 14:21 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1
root root 3034 Feb 6 2007 \033[00mxorg.conf.new\033[00m\r\ndrwxr-
xr-x 2 root root 4096 Feb 7 2007 \033[01;34myum-keys\033[00m\r\n
\033[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x937737...
Sending 'ls\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x9374dbc)','ls\x{a}') called at ./ssh.pl
line 68
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x93819e0)' ) called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x938198c)') called
at ./ssh.pl line 70
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x9375140)') called
at ./ssh.pl line 88
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.

ls
1 Desktop install.log.syslog xorg.conf.new
anaconda-ks.cfg install.log sb-log.txt yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog\033[00m
\033[00mxorg.conf.new\033[00m\r\n\033[00manaconda-ks.cfg\033[00m
\033[00minstall.log\033[00m \033[00msb-log.txt\033[00m
\033[01;34myum-keys\033[00m\r\n\033[m[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog
\033[00m \033[00mxorg.conf.new\033[00m\r\n\033[00manaconda-ks.cfg
\033[00m \033[00minstall.log\033[00m \033[00msb-log.txt
\033[00m \033[01;34myum-keys\033[00m\r\n\033[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x9377444)...
Sending 'exit\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x9374dbc)','exit\x{a}' ) called at ./
ssh.pl line 88
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x938240' ) called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0 x9375140)') called
at ./ssh.pl line 88
exit
logout
Connection to 10.17.39.29 closed.
[root@fedora-c2 perl-scripts]#
[root@fedora-c2 perl-scripts]#
************************************************** ******************

As I said in my first post, the operations of the scripts look out of
sync since the sleep commands are executed in the wrong order and the
last "ls" output is not shown until the very end of the script (only
if use soft_close command and the log_file does not caoture that
output anyway).
The commands should be executed in the following order:
ls -l
sleep 2
ls
sleep 5

Instead they are executed like that:
sleep 2
ls -l
sleep 5
ls (only visible if soft_close is present and not captured by log_file
anyway)

Any idea?
 
Reply With Quote
 
Andry
Guest
Posts: n/a
 
      09-23-2008
On 23 Set, 15:48, Glenn Jackman <(E-Mail Removed)> wrote:
> At 2008-09-23 06:35AM, "Andry" wrote:
>
>
>
> > *Hi all,
> > *I have the following script:
> > *************************************************** ****************
> > *#!/usr/bin/perl -w
> > *use Expect;

>
> > *$timeout = 5;
> > *$|=1;

>
> > *$exp = new Expect();
> > *$exp->raw_pty(1);

>
> > *$exp->log_file("output.log", "w");

>
> > *$exp->spawn("ssh -l username 10.17.39.29");
> > *$exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> > *\n"); } ]);
> > *$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> > *\n"); } ]);
> > *sleep 2;
> > *$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> > *\n"); } ]);
> > *sleep 5;

>
> In my experience (with expect rather than expect.pm), if you need to
> sleep, your match patterns are wrong. *In particular, if you're trying
> to match your shell prompt, you should anchor your regex.
>
> --
> Glenn Jackman
> * * Write a wise saying and your name will live forever. -- Anonymous


I'm not sure to understand what you mean, but the problem is not in my
match pattern ("prompt-string" was just a med up example...).
The expect function matches what I need (as you can see in the debug
generated by exp_internal) but I have a "timing" issue with execution
of sleep commands and also the output of send commands is not sent to
STDOUT when expected (see previous posts).
It looks like expect objects do not send their output to STDOUT
correctly (buffering issue?).

Andrea
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      09-23-2008
Andry wrote:

> output anyway).
> The commands should be executed in the following order:
> ls -l
> sleep 2
> ls
> sleep 5
>
> Instead they are executed like that:
> sleep 2
> ls -l
> sleep 5
> ls (only visible if soft_close is present and not captured by log_file
> anyway)
>
> Any idea?


It could very well be a buffering issue.
You could check by adding a "print STDERR "Got password prompt, sending
password\n" to the anonymous sub that's executed when "[Pp]assword" is
matched, and liekwise for the other subs.

Josef
BTW You *do* know, that you had the password in your output?

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      09-23-2008
Andry wrote:
> Hi all,
> I have the following script:
> ************************************************** ****************
> #!/usr/bin/perl -w
> use Expect;
>
> $timeout = 5;
> $|=1;
>
> $exp = new Expect();
> $exp->raw_pty(1);
>
> $exp->log_file("output.log", "w");
>
> $exp->spawn("ssh -l username 10.17.39.29");
> $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> \n"); } ]);
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> \n"); } ]);
> sleep 2;
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> \n"); } ]);
> sleep 5;
>
> $exp->log_file(undef);
>
> $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
> \n"); } ]);
> ************************************************** ****************
>
> When I run the script, the console (stdout) shows:
> 1) ssh (login...)...
> 2) sleeping 2 seconds
> 3) "ls -l" output
> 4) sleeping 5 seconds
> ... the script quits without displaying the output of the second
> command "ls"
> Even the log file does not contain the missing output.


On my way home, I have been thinking about this (with this miserable
weather one *has* to think about something else) ...

In a wider sense, it has nothing to do with buffering.

What happens is:

You (disguised as a Perl/Expect script) wait for the password prompt and
send the password. Then you wait for the shell prompt and send the ls
command. Then you sleep for 2 seconds. In the meantime, the remote "ls
-l" produces output, but there is no-one there to notice.
Then you wait for the next shell prompt. *Now* you have to sift through
ls' output to look for the prompt (btw. here Glenn's remark about
anchoring comes into play, if one of the file names would contain the
prompt as a substring!). While sifting though the output, it will be
echoed to the console.
When you find the prompt, you send the next "ls" command and the
previous section hits you again.

So, it's not classical buffering but rather a postponed reading/echoing
of output produced earlier (of course, pedantics would call this
buffering, too, ...)

What you are expecting is the equivalent of

expect(prompt)
send("ls -l\r")
expect(prompt) << Here you get "ls -l"'s output
sleep 2
send("ls\r")
expect(prompt) << Here you get "ls"'s output
sleep 5;

HTH,

Josef
--
Mails please to josef dot moellers
and I'm on gmx dot de.
 
Reply With Quote
 
Andry
Guest
Posts: n/a
 
      09-24-2008
On 23 Set, 18:01, Josef Moellers <(E-Mail Removed)> wrote:
> Andry wrote:
> > Hi all,
> > I have the following script:
> > ************************************************** ****************
> > #!/usr/bin/perl -w
> > use Expect;

>
> > $timeout = 5;
> > $|=1;

>
> > $exp = new Expect();
> > $exp->raw_pty(1);

>
> > $exp->log_file("output.log", "w");

>
> > $exp->spawn("ssh -l username 10.17.39.29");
> > $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> > \n"); } ]);
> > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> > \n"); } ]);
> > sleep 2;
> > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> > \n"); } ]);
> > sleep 5;

>
> > $exp->log_file(undef);

>
> > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
> > \n"); } ]);
> > ************************************************** ****************

>
> > When I run the script, the console (stdout) shows:
> > 1) ssh (login...)...
> > 2) sleeping 2 seconds
> > 3) "ls -l" output
> > 4) sleeping 5 seconds
> > ... the script quits without displaying the output of the second
> > command "ls"
> > Even the log file does not contain the missing output.

>
> On my way home, I have been thinking about this (with this miserable
> weather one *has* to think about something else) ...
>
> In a wider sense, it has nothing to do with buffering.
>
> What happens is:
>
> You (disguised as a Perl/Expect script) wait for the password prompt and
> send the password. Then you wait for the shell prompt and send the ls
> command. Then you sleep for 2 seconds. In the meantime, the remote "ls
> -l" produces output, but there is no-one there to notice.
> Then you wait for the next shell prompt. *Now* you have to sift through
> ls' output to look for the prompt (btw. here Glenn's remark about
> anchoring comes into play, if one of the file names would contain the
> prompt as a substring!). While sifting though the output, it will be
> echoed to the console.
> When you find the prompt, you send the next "ls" command and the
> previous section hits you again.
>
> So, it's not classical buffering but rather a postponed reading/echoing
> of output produced earlier (of course, pedantics would call this
> buffering, too, ...)
>
> What you are expecting is the equivalent of
>
> expect(prompt)
> send("ls -l\r")
> expect(prompt) *<< Here you get "ls -l"'s output
> sleep 2
> send("ls\r")
> expect(prompt) *<< Here you get "ls"'s output
> sleep 5;
>
> HTH,
>
> Josef
> --
> Mails please to josef dot moellers
> and I'm on gmx dot de.


Hi Josef,
I checked and the prompt string is not present in any line of the "ls"
output.
Actually the problem starts even before that: right after sending the
password, the output shows that the script executes the first sleep (2
seconds) even BEFORE sending the first "ls -l".
So the problem is not related to unexpected (or not timely) matching
of the prompt string.
I put a couple of "print" before and after each "send" command to
better understand the timing issue.
'print "\nSending...\n"' is put right before each 'send'.
'print "\n...Sent!\n"' is put right after each 'send' (before the
'sleep').

Here is the output I get (I typed comments to highlight when the
'sleep' occurs):
************************************************** ******
root@10.17.39.29's password:
Sending...

....Sent!

Last login: Wed Sep 24 09:57:48 2008 from 10.17.91.83
Welcome to the Milan TestLab server
If you're not part of the TestLab team, you're not welcome anymore...


Sending...

....Sent!
########## NOW IS SLEEPING TWO SECONDS ##########
[root@svr-mila-testlab2 ~]# ls -l
total 2364
-rw-r--r-- 1 root root 33980 Feb 8 2007 1
-rw------- 1 root root 1534 Feb 5 2007 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Feb 5 2007 Desktop
-rw-r--r-- 1 root root 38253 Feb 5 2007 install.log
-rw-r--r-- 1 root root 4475 Feb 5 2007 install.log.syslog
-rw-r--r-- 1 root root 2297093 Sep 24 10:01 sb-log.txt
-rw-r--r-- 1 root root 3034 Feb 6 2007 xorg.conf.new
drwxr-xr-x 2 root root 4096 Feb 7 2007 yum-keys
[root@svr-mila-testlab2 ~]#
Sending...

....Sent!
########## NOW IS SLEEPING FIVE SECONDS ##########
ls
1 Desktop install.log.syslog xorg.conf.new
anaconda-ks.cfg install.log sb-log.txt yum-keys
[root@svr-mila-testlab2 ~]#
Sending...

....Sent!
exit
logout
Connection to 10.17.39.29 closed.
************************************************** ******

Instead, what I expect should be something like this:
************************************************** ******
root@10.17.39.29's password:
Sending...
Last login: Wed Sep 24 09:57:48 2008 from 10.17.91.83
Welcome to the Milan TestLab server
If you're not part of the TestLab team, you're not welcome anymore...
[root@svr-mila-testlab2 ~]#
....Sent!

Sending...
ls -l
total 2364
-rw-r--r-- 1 root root 33980 Feb 8 2007 1
-rw------- 1 root root 1534 Feb 5 2007 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Feb 5 2007 Desktop
-rw-r--r-- 1 root root 38253 Feb 5 2007 install.log
-rw-r--r-- 1 root root 4475 Feb 5 2007 install.log.syslog
-rw-r--r-- 1 root root 2297093 Sep 24 10:01 sb-log.txt
-rw-r--r-- 1 root root 3034 Feb 6 2007 xorg.conf.new
drwxr-xr-x 2 root root 4096 Feb 7 2007 yum-keys
[root@svr-mila-testlab2 ~]#
....Sent!

########## NOW IS SLEEPING TWO SECONDS ##########

Sending...
ls
1 Desktop install.log.syslog xorg.conf.new
anaconda-ks.cfg install.log sb-log.txt yum-keys
[root@svr-mila-testlab2 ~]#
....Sent!
########## NOW IS SLEEPING FIVE SECONDS ##########

Sending...
exit
....Sent!

logout
Connection to 10.17.39.29 closed.
************************************************** ******

Maybe it's ok that "...Sent!" is displayed right after "Sending..."
because the output of the 'send' command takes longer to display and
it comes after, but the time of displaying "Sending..." is wrong
anyway and, above all, the time of 'sleep' is wrong.
Of course this is trivial example but I need to display output lines
and execute 'sleep' commands in the proper order to build a more
complex non-interactive script.

Any idea?

Thanks,
Andrea
 
Reply With Quote
 
Andry
Guest
Posts: n/a
 
      09-24-2008
On 24 Set, 10:25, Andry <(E-Mail Removed)> wrote:
> On 23 Set, 18:01, Josef Moellers <(E-Mail Removed)> wrote:
>
>
>
> > Andry wrote:
> > > Hi all,
> > > I have the following script:
> > > ************************************************** ****************
> > > #!/usr/bin/perl -w
> > > use Expect;

>
> > > $timeout = 5;
> > > $|=1;

>
> > > $exp = new Expect();
> > > $exp->raw_pty(1);

>
> > > $exp->log_file("output.log", "w");

>
> > > $exp->spawn("ssh -l username 10.17.39.29");
> > > $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> > > \n"); } ]);
> > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
> > > \n"); } ]);
> > > sleep 2;
> > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> > > \n"); } ]);
> > > sleep 5;

>
> > > $exp->log_file(undef);

>
> > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
> > > \n"); } ]);
> > > ************************************************** ****************

>
> > > When I run the script, the console (stdout) shows:
> > > 1) ssh (login...)...
> > > 2) sleeping 2 seconds
> > > 3) "ls -l" output
> > > 4) sleeping 5 seconds
> > > ... the script quits without displaying the output of the second
> > > command "ls"
> > > Even the log file does not contain the missing output.

>
> > On my way home, I have been thinking about this (with this miserable
> > weather one *has* to think about something else) ...

>
> > In a wider sense, it has nothing to do with buffering.

>
> > What happens is:

>
> > You (disguised as a Perl/Expect script) wait for the password prompt and
> > send the password. Then you wait for the shell prompt and send the ls
> > command. Then you sleep for 2 seconds. In the meantime, the remote "ls
> > -l" produces output, but there is no-one there to notice.
> > Then you wait for the next shell prompt. *Now* you have to sift through
> > ls' output to look for the prompt (btw. here Glenn's remark about
> > anchoring comes into play, if one of the file names would contain the
> > prompt as a substring!). While sifting though the output, it will be
> > echoed to the console.
> > When you find the prompt, you send the next "ls" command and the
> > previous section hits you again.

>
> > So, it's not classical buffering but rather a postponed reading/echoing
> > of output produced earlier (of course, pedantics would call this
> > buffering, too, ...)

>
> > What you are expecting is the equivalent of

>
> > expect(prompt)
> > send("ls -l\r")
> > expect(prompt) *<< Here you get "ls -l"'s output
> > sleep 2
> > send("ls\r")
> > expect(prompt) *<< Here you get "ls"'s output
> > sleep 5;

>
> > HTH,

>
> > Josef
> > --
> > Mails please to josef dot moellers
> > and I'm on gmx dot de.

>
> Hi Josef,
> I checked and the prompt string is not present in any line of the "ls"
> output.
> Actually the problem starts even before that: right after sending the
> password, the output shows that the script executes the first sleep (2
> seconds) even BEFORE sending the first "ls -l".
> So the problem is not related to unexpected (or not timely) matching
> of the prompt string.
> I put a couple of "print" before and after each "send" command to
> better understand the timing issue.
> 'print "\nSending...\n"' is put right before each 'send'.
> 'print "\n...Sent!\n"' is put right after each 'send' (before the
> 'sleep').
>
> Here is the output I get (I typed comments to highlight when the
> 'sleep' occurs):
> ************************************************** ******
> r...@10.17.39.29's password:
> Sending...
>
> ...Sent!
>
> Last login: Wed Sep 24 09:57:48 2008 from 10.17.91.83
> Welcome to the Milan TestLab server
> If you're not part of the TestLab team, you're not welcome anymore...
>
> Sending...
>
> ...Sent!
> ########## NOW IS SLEEPING TWO SECONDS ##########
> [root@svr-mila-testlab2 ~]# ls -l
> total 2364
> -rw-r--r-- 1 root root * 33980 Feb *8 *2007 1
> -rw------- 1 root root * *1534 Feb *5 *2007 anaconda-ks.cfg
> drwxr-xr-x 2 root root * *4096 Feb *5 *2007 Desktop
> -rw-r--r-- 1 root root * 38253 Feb *5 *2007 install.log
> -rw-r--r-- 1 root root * *4475 Feb *5 *2007 install.log.syslog
> -rw-r--r-- 1 root root 2297093 Sep 24 10:01 sb-log.txt
> -rw-r--r-- 1 root root * *3034 Feb *6 *2007 xorg.conf.new
> drwxr-xr-x 2 root root * *4096 Feb *7 *2007 yum-keys
> [root@svr-mila-testlab2 ~]#
> Sending...
>
> ...Sent!
> ########## NOW IS SLEEPING FIVE SECONDS ##########
> ls
> 1 * * * * * * * *Desktop * * *install.log.syslog *xorg.conf.new
> anaconda-ks.cfg *install.log *sb-log.txt * * * * *yum-keys
> [root@svr-mila-testlab2 ~]#
> Sending...
>
> ...Sent!
> exit
> logout
> Connection to 10.17.39.29 closed.
> ************************************************** ******
>
> Instead, what I expect should be something like this:
> ************************************************** ******
> r...@10.17.39.29's password:
> Sending...
> Last login: Wed Sep 24 09:57:48 2008 from 10.17.91.83
> Welcome to the Milan TestLab server
> If you're not part of the TestLab team, you're not welcome anymore...
> [root@svr-mila-testlab2 ~]#
> ...Sent!
>
> Sending...
> *ls -l
> total 2364
> -rw-r--r-- 1 root root * 33980 Feb *8 *2007 1
> -rw------- 1 root root * *1534 Feb *5 *2007 anaconda-ks.cfg
> drwxr-xr-x 2 root root * *4096 Feb *5 *2007 Desktop
> -rw-r--r-- 1 root root * 38253 Feb *5 *2007 install.log
> -rw-r--r-- 1 root root * *4475 Feb *5 *2007 install.log.syslog
> -rw-r--r-- 1 root root 2297093 Sep 24 10:01 sb-log.txt
> -rw-r--r-- 1 root root * *3034 Feb *6 *2007 xorg.conf.new
> drwxr-xr-x 2 root root * *4096 Feb *7 *2007 yum-keys
> [root@svr-mila-testlab2 ~]#
> ...Sent!
>
> ########## NOW IS SLEEPING TWO SECONDS ##########
>
> Sending...
> ls
> 1 * * * * * * * *Desktop * * *install.log.syslog *xorg.conf.new
> anaconda-ks.cfg *install.log *sb-log.txt * * * * *yum-keys
> [root@svr-mila-testlab2 ~]#
> ...Sent!
> ########## NOW IS SLEEPING FIVE SECONDS ##########
>
> Sending...
> exit
> ...Sent!
>
> logout
> Connection to 10.17.39.29 closed.
> ************************************************** ******
>
> Maybe it's ok that "...Sent!" is displayed right after "Sending..."
> because the output of the 'send' command takes longer to display and
> it comes after, but the time of displaying "Sending..." is wrong
> anyway and, above all, the time of 'sleep' is wrong.
> Of course this is trivial example but I need to display output lines
> and execute 'sleep' commands in the proper order to build a more
> complex non-interactive script.
>
> Any idea?
>
> Thanks,
> Andrea


I ran again the exp_internal tool for debug and found that somehow the
'sleep' commands are correctly executed but the output is displayed at
the wrong time, see below (I added comments to highlight 'sleep'
executions that are not logged by exp_internal):
************************************************** ************
Spawned 'ssh -l root 10.17.39.29'
spawn id(3)
Pid: 31550
Tty: /dev/pts/4
Expect::spawn('Expect=GLOB(0x981723','ssh -l root 10.17.39.29')
called
at ./ssh.pl line 34
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x9822520)') called
at
../ssh.pl line 37
spawn id(3): list of patterns:
#1: -re `[Pp]assword'


spawn id(3): Does `'
match:
pattern #1: -re `[Pp]assword'? No.

root@10.17.39.29's password:
spawn id(3): Does `root@10.17.39.29\'s password: '
match:
pattern #1: -re `[Pp]assword'? YES!!
Before match string: `root@10.17.39.29\'s '
Match string: `password'
After match string: `: '
Matchlist: ()
Calling hook CODE(0x9817274)...

Sending...
Sending 'XXXXXXXX\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x981723','XXXXXXXX\x {a}') called at ./
ssh.
pl line 37
main::__ANON__('Expect=GLOB(0x981723') called at /usr/lib/perl5/
site_p
erl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x98380d4)' ) called at /usr/
lib/p
erl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x9822520)') called
at
../ssh.pl line 37

....Sent!
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x98069ec)') called
at
../ssh.pl line 56
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `: '
match:
pattern #1: -re `svr-mila'? No.


spawn id(3): Does `: \n'
match:
pattern #1: -re `svr-mila'? No.



spawn id(3): Does `: \nLast login: Wed Sep 24 11:05:15 2008 from
10.17.91.83\r\r
\nWelcome to the Milan TestLab server\r\nIf you\'re not part of the
TestLab team
, you\'re not welcome anymore...\r\n\r\n'
match:
pattern #1: -re `svr-mila'? No.

Last login: Wed Sep 24 11:05:15 2008 from 10.17.91.83
Welcome to the Milan TestLab server
If you're not part of the TestLab team, you're not welcome anymore...


spawn id(3): Does `: \nLast login: Wed Sep 24 11:05:15 2008 from
10.17.91.83\r\r
\nWelcome to the Milan TestLab server\r\nIf you\'re not part of the
TestLab team
, you\'re not welcome anymore...\r\n\r\n[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `: \nLast login: Wed Sep 24 11:05:15 2008
from 10.17.91
..83\r\r\nWelcome to the Milan TestLab server\r\nIf you\'re not part of
the TestL
ab team, you\'re not welcome anymore...\r\n\r\n[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x981743c)...
Sending 'ls -l\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x981723','ls -l\x{a}') called at ./
ssh.pl
line 53
main::__ANON__('Expect=GLOB(0x981723') called at /usr/lib/perl5/
site_p
erl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x97a9bf4)' ) called at /usr/
lib/p
erl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x98069ec)') called
at
../ssh.pl line 56
[root@svr-mila-testlab2 ~]#
Sending...

....Sent!
########## NOW IT'S SLEEPING 2 SECONDS ##########
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x98382a') called
at
../ssh.pl line 72
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.

ls -l
total 2368
-rw-r--r-- 1 root root 33980 Feb 8 2007 1
-rw------- 1 root root 1534 Feb 5 2007 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Feb 5 2007 Desktop
-rw-r--r-- 1 root root 38253 Feb 5 2007 install.log
-rw-r--r-- 1 root root 4475 Feb 5 2007 install.log.syslog
-rw-r--r-- 1 root root 2299385 Sep 24 11:06 sb-log.txt
-rw-r--r-- 1 root root 3034 Feb 6 2007 xorg.conf.new
drwxr-xr-x 2 root root 4096 Feb 7 2007 yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls -l\r\n\033[00mtotal 2368\r\n-rw-r--
r-- 1 roo
t root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-rw------- 1 root
root 1534
Feb 5 2007 \033[00manaconda-ks.cfg\033[00m\r\ndrwxr-xr-x 2 root
root 4096 F
eb 5 2007 \033[01;34mDesktop\033[00m\r\n-rw-r--r-- 1 root root
38253 Feb 5
2007 \033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root 4475
Feb 5 2007
\033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root root 2299385
Sep 24 11:
06 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1 root root 3034 Feb
6 2007 \03
3[00mxorg.conf.new\033[00m\r\ndrwxr-xr-x 2 root root 4096 Feb 7
2007 \033[0
1;34myum-keys\033[00m\r\n\033[m[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls -l\r\n\033[00mtotal 2368\r
\n-rw-r--r-
- 1 root root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-rw------- 1
root root
1534 Feb 5 2007 \033[00manaconda-ks.cfg\033[00m\r\ndrwxr-xr-x 2
root root
4096 Feb 5 2007 \033[01;34mDesktop\033[00m\r\n-rw-r--r-- 1 root
root 38253
Feb 5 2007 \033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root
4475 Feb
5 2007 \033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root root
2299385 Sep
24 11:06 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1 root root 3034
Feb 6 2
007 \033[00mxorg.conf.new\033[00m\r\ndrwxr-xr-x 2 root root 4096
Feb 7 2007
\033[01;34myum-keys\033[00m\r\n\033[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x9814f54)...

Sending...
Sending 'ls\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x981723','ls\x{a}') called at ./ssh.pl
lin
e 69
main::__ANON__('Expect=GLOB(0x981723') called at /usr/lib/perl5/
site_p
erl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x98380e0)' ) called at /usr/
lib/p
erl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x98382a') called
at
../ssh.pl line 72

....Sent!
########## NOW IT'S SLEEPING FIVE SECONDS ###########
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x9575530)') called
at
../ssh.pl line 90
spawn id(3): list of patterns:
#1: -re `svr-mila'


spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.

ls
1 Desktop install.log.syslog xorg.conf.new
anaconda-ks.cfg install.log sb-log.txt yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog\033[00m
\033[00mxorg
..conf.new\033[00m\r\n\033[00manaconda-ks.cfg\033[00m
\033[00minstall.log\033[00
m \033[00msb-log.txt\033[00m \033[01;34myum-keys\033[00m\r\n
\033[m[roo
t@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog
\033[00m \033[
00mxorg.conf.new\033[00m\r\n\033[00manaconda-ks.cfg\033[00m
\033[00minstall.log
\033[00m \033[00msb-log.txt\033[00m \033[01;34myum-keys
\033[00m\r\n\03
3[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x98150f...

Sending...
Sending 'exit\n' to spawn id(3)
Expect:rint('Expect=GLOB(0x981723','exit\x{a}' ) called at ./
ssh.pl l
ine 90
main::__ANON__('Expect=GLOB(0x981723') called at /usr/lib/perl5/
site_p
erl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x9838074)' ) called at /usr/
lib/p
erl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x981723',5,'ARRAY(0 x9575530)') called
at
../ssh.pl line 90

....Sent!
exit
logout
Connection to 10.17.39.29 closed.
************************************************** ********

Eventhough I could ignore the display to console, I still have the
problem that the log_file does not capture the last part of the script
(from the second "ls" on). So, I still think there is a real issue
with STDOUT buffering.
Any way out?

Thanks,
Andrea
 
Reply With Quote
 
Andry
Guest
Posts: n/a
 
      09-24-2008
On Sep 24, 1:50*pm, Glenn Jackman <(E-Mail Removed)> wrote:
> The paradigm behind Expect is this: *send something and then expect to
> see the result. *What you are thinking is that the act of sending
> somehow immediately produces outout. *send and expect commands really
> occur in pairs.
>
> If I send you an email, I would expect to wait for your reply.
> NOT: I send you an email and magically your reply instantly appears.
>
> You should send the ls command, then expect the prompt. *In the output
> that contains the prompt you will find the result of ls.
>
> At 2008-09-24 04:25AM, "Andry" wrote:
>
>
>
> > > Andry wrote:
> > > > Hi all,
> > > > I have the following script:
> > > > ************************************************** ****************
> > > > #!/usr/bin/perl -w
> > > > use Expect;

>
> > > > $timeout = 5;
> > > > $|=1;

>
> > > > $exp = new Expect();
> > > > $exp->raw_pty(1);

>
> > > > $exp->log_file("output.log", "w");

>
> > > > $exp->spawn("ssh -l username 10.17.39.29");
> > > > $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
> > > > \n"); } ]);
> > > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls-l
> > > > \n"); } ]);
> > > > sleep 2;
> > > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
> > > > \n"); } ]);
> > > > sleep 5;

>
> I would write the above as:
>
> * * my @match_prompt = ('-re', 'prompt_pattern$');
>
> * * $exp->spawn("ssh -l username 10.17.39.29");
> * * $exp->expect($timeout, @match_prompt);
>
> * * $exp->send("password\r");
> * * $exp->expect($timeout, @match_prompt) or
> * * * * die "couldn't see prompt after 'send password'\n";
>
> * * $exp->send("ls -l\r");
> * * $exp->expect($timeout, @match_prompt) or
> * * * * die "couldn't see prompt after 'send ls -l'\n";
> * * # parse $match_patterns for 'ls -l' output
>
> * * $exp->send("ls\r");
> * * $exp->expect($timeout, @match_prompt) or
> * * * * die "couldn't see prompt after 'send ls'\n";
> * * # parse $match_patterns for 'ls' output
>
> * * $exp->send("exit\r");
> * * $exp->expect($timeout, 'eof');
>
> > > > $exp->log_file(undef);

>
> > > > $exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("exit
> > > > \n"); } ]);

>
> --
> Glenn Jackman
> * * Write a wise saying and your name will live forever. -- Anonymous


Hi Glenn,
Sorry but you did not catch the point.
Doing this:
> $exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password\n"); } ]);

It's exactly the same of doing this:
> $exp->expect($timeout, '-re', "[Pp]assword");
> $exp->send("password\r");

There is no difference (even '-re' is taken by default in the first
method) execpt that the first method compresses the syntax.
You can try and check it out yourself.

Here the problem is different. STDOUT is not being managed by
Expect.pm as expected.

Now, I found that if I put $exp->log_file(undef) at the very end of
the script (after $exp->soft_close) I can catch the rest of the script
that was being missed out previously. Though the desired result would
be to catch "ls -l" and "ls" but not the rest (exit ssh...). If I put
$exp->log_file(undef) after the last 'send ls' but before $exp-
>soft_close() then the output of the last "ls" is missed out.


So, I still see inconsistent behavior with STDOUT for 'sleep' commands
as well as log_file operations.

Any idea?

Thanks,
Andrea


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: On the default buffering of stdout and the future of CLC Kaz Kylheku C Programming 1 02-23-2009 10:51 PM
Re: On the default buffering of stdout and the future of CLC Chris McDonald C Programming 5 02-22-2009 08:32 PM
Non-buffering stdout Cecil Westerhof Python 0 03-25-2008 04:29 AM
disable stdout buffering ? Mathias Herrmann C Programming 7 10-04-2005 05:23 PM
IBM Java Process.exec() ... mad buffering of stdout dh Java 2 02-03-2004 09:47 AM



Advertisments