From: Prashar <him...@ya...> - 2009-02-16 07:59:55
|
Hi, Snippet of code pasted below gets time-out as it couldnt match the string "[spr9628-c] MP>". Could some one please temme where actually I goofed up . $exp->expect($timeout, [ 'timeout' => sub{print "\nERROR: Timed out waiting for telnet to close.\n"; }], [ 'MP login: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$username\n"); exp_continue; } ], [ 'MP password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn telnet.\n"; } } ], [ '-re', '\s*MP Host Name:(.*)\r', sub { my $fh = shift; $tmp = ($fh->matchlist)[0]; ## $tmp = spr9628-c $tmp =~ s/\s+//g; $tmp =~ s/^\s+//g; exp_continue; } ], '-re', '\[$tmp\]\sMP>\s', ## Should exit expect from here as per logic ); ========================== Output: MP Host Name: spr9628-c Revision E.03.15 ************************************************************************* MP ACCESS IS NOT SECURE Default MP users are currently configured and remote access is enabled. Modify default users passwords or delete default users (see UC command) OR Disable all types of remote access (see SA command) ************************************************************************* MP MAIN MENU: CO: Console VFP: Virtual Front Panel CM: Command Menu CL: Console Log SL: Show Event Logs HE: Main Help Menu X: Exit Connection [spr9628-c] MP> ERROR: Timed out waiting for telnet to close. Thanks in advance -H -- View this message in context: http://www.nabble.com/Expect-perl-tp22032953p22032953.html Sent from the Perl - Expectperl-Discuss mailing list archive at Nabble.com. |
From: Bryan B. <br...@bu...> - 2009-02-16 14:52:28
|
> } > ], > '-re', '\[$tmp\]\sMP>\s', ## Should exit expect from here > as > per logic > > ); > If your using single quotes $tmp wont be expanded. Try: "\[$tmp\]\sMP>\s" and see if that works for you. Bryan http://sourceforge.net/projects/rover |
From: Austin S. <te...@of...> - 2009-02-16 17:22:09
|
On Mon, 16 Feb 2009 09:37:01 -0500 (EST) "Bryan Bueter" <br...@bu...> wrote: > > } > > ], > > '-re', '\[$tmp\]\sMP>\s', ## Should exit expect > > from here as'\[ > > per logic > > > > ); > > > > If your using single quotes $tmp wont be expanded. Try: > "\[$tmp\]\sMP>\s" and see if that works for you. I would try something like '\[' . $tmp . '\]\sMP>\s' If you use double quotes the regex engine will see [$tmp] instead of \[$tmp\] Austin |
From: Prashar <him...@ya...> - 2009-02-17 05:46:14
|
Thanks everyone for your kind help. I made following change to the code: [ '\s*MP Host Name:(.*)', sub { my $fh = shift; $tmp = ($fh->matchlist)[0]; $tmp =~ s/\s+//g; $tmp =~ s/^\s+//g; exp_continue; } ], "\[$tmp\] MP\> ", But I think $tmp will never have the value assigned to it in a #5 pattern match routine as the list of patterns are prepared at the beginning before spawning for match. hence we see the $tmp as null ( which was defined globally). exp_internal(1) ============== Starting EXPECT pattern matching... at /opt/perl_64/lib/site_perl/5.8.8/Expect.pm line 559 Expect::expect('Expect=GLOB(0x60000000006dc260)', 5, 'ARRAY(0x60000000006dc710)', 'ARRAY(0x60000000006dc7f0)', 'ARRAY(0x60000000006dc8c0)', 'ARRAY(0x60000000006dc980)', 'ARRAY(0x60000000006dca50)', '[] MP> ') called at ./expect_orig.pl line 88 spawn id(3): list of patterns: #2: -re `MP login: $' #3: -re `MP password: $' #4: -eof `' #5: -re `\\s*MP Host Name:(.*)' #6: -ex `[] MP> ' spawn id(3): Does `' match: pattern #2: -re `MP login: $'? No. pattern #3: -re `MP password: $'? No. pattern #4: -eof `'? No. pattern #5: -re `\\s*MP Host Name:(.*)'? No. pattern #6: -ex `[] MP> '? No. Prashar wrote: > > Hi, > > Snippet of code pasted below gets time-out as it couldnt match the string > "[spr9628-c] MP>". Could some one please temme where actually I goofed up > . > > $exp->expect($timeout, > > [ 'timeout' => sub{print "\nERROR: Timed out waiting for > telnet to close.\n"; > }], > [ > 'MP login: $', > sub { > $spawn_ok = 1; > my $fh = shift; > $fh->send("$username\n"); > exp_continue; > } > ], > [ > 'MP password: $', > sub { > my $fh = shift; > print $fh "$password\n"; > exp_continue; > } > ], > [ > eof => > sub { > if ($spawn_ok) { > die "ERROR: premature EOF in login.\n"; > } else { > die "ERROR: could not spawn telnet.\n"; > } > } > ], > [ > '-re', '\s*MP Host Name:(.*)\r', > sub { > my $fh = shift; > $tmp = ($fh->matchlist)[0]; ## $tmp = spr9628-c > $tmp =~ s/\s+//g; > $tmp =~ s/^\s+//g; > exp_continue; > } > ], > '-re', '\[$tmp\]\sMP>\s', ## Should exit expect from here > as per logic > > ); > > > > ========================== > > Output: > > MP Host Name: spr9628-c > > Revision E.03.15 > > > ************************************************************************* > MP ACCESS IS NOT SECURE > Default MP users are currently configured and remote access is enabled. > Modify default users passwords or delete default users (see UC command) > OR > Disable all types of remote access (see SA command) > ************************************************************************* > > > MP MAIN MENU: > > CO: Console > VFP: Virtual Front Panel > CM: Command Menu > CL: Console Log > SL: Show Event Logs > HE: Main Help Menu > X: Exit Connection > > [spr9628-c] MP> > ERROR: Timed out waiting for telnet to close. > > > Thanks in advance > -H > > > -- View this message in context: http://www.nabble.com/Expect-perl-tp22032953p22051159.html Sent from the Perl - Expectperl-Discuss mailing list archive at Nabble.com. |
From: Roland G. <rgi...@cp...> - 2009-02-17 10:20:37
|
Prashar wrote: > [ > '\s*MP Host Name:(.*)', > sub { > my $fh = shift; > $tmp = ($fh->matchlist)[0]; > $tmp =~ s/\s+//g; > $tmp =~ s/^\s+//g; > exp_continue; > } > ], > "\[$tmp\] MP\> ", > > But I think $tmp will never have the value assigned to it in a #5 pattern > match routine as the list of patterns are prepared at the beginning before > spawning for match. hence we see the $tmp as null ( which was defined > globally). Right, this cannot work, you try to change the match pattern after it was already passed to the expect() call. For this to work, you must exit the first expect() call and enter another expect(), this time with the captured prompt string. Hope this helps, Roland |