From: Rui M. <rme...@te...> - 2007-05-08 13:45:09
|
Hi. I'm having a problem using Expect.pm I created a script to do automated ssh to several hosts. One thing I need to do is to enter several Cisco routers, execute the commands terminal length 0 and show running-config and save some of the output to a file. It's all working fine, and as the commands are executed the correct output is automatically printed in the screen (expect.pm default option). However, when the output of the command is very long (show running-config) the output is not correctly stored in my variable $stdout, it's always truncated in the same spot! Example: (These are parts of my code) #--------------------------------------------------------------------------- ------------------------ my $exp; my $prompt_default = "[\\\$%#>]\\s*(\\(enable\\)|)\\s*"; # A strange prompt, but it's working fine $exp = Expect->spawn("ssh ${username}\@${host}") or die "ERROR: Couldn't spawn ssh connection, $!\n"; $exp->log_file($opts{'l'}); $exp->restart_timeout_upon_receive(1); $exp->max_accum(0); # Executing Login - all is made correctly # (.) # Executing commands # (.) # Retrieving Output $exp->clear_accum(); $exp->expect($timeout, [ qr/$prompt_default/, sub { my $fh = shift; $stdout = $exp->before() . $exp->match() . $exp->after(); print $fh "\n"; #exp_continue; # Don't continue } ] ) or do{ erro("Invalid Prompt, " . $exp->exp_error() . "\n"); $exp->soft_close(); return; }; print "###############\n"; print "$stdout\n"; print "###############\n"; #--------------------------------------------------------------------------- ------------------------ What happens is that when I print $stdout the output I get is: ############### (1st part of output) ############### (2nd part of output) And if I save $stdout to a file, only 1st part is saved (2nd part gets truncated). Does anyone know why this is happening? I tried putting some sleeps but that didn't solve my problem. It almost seems that this works as some kind of buffer, that only when emptied can store more data! However, the qr/$prompt_default/ is working fine, as well as the automatic prints to the screen! Thanks in advance. |
From: Ken I. <fn...@ua...> - 2007-05-08 17:12:56
|
On Tue, May 08, 2007 at 02:45:00PM +0100, Rui Meireles wrote: > Hi. I?m having a problem using Expect.pm > > I created a script to do automated ssh to several hosts. One thing I > need to do is to enter several Cisco routers, execute the commands > terminal length 0 and show running-config and save some of the output > to a file. > > It?s all working fine, and as the commands are executed the correct > output is automatically printed in the screen (expect.pm default > option). However, when the output of the command is very long (show > running-config) the output is not correctly stored in my variable > $stdout, it?s always truncated in the same spot! > > Example: (These are parts of my code) > ... > $stdout = $exp->before() . $exp->match() . $exp->after(); > ... > > print ?###############\n?; > print ?$stdout\n?; > print ?###############\n?; > ... > > What happens is that when I print $stdout the output I get is: > > ############### > (1st part of output) > ############### > (2nd part of output) > > And if I save $stdout to a file, only 1st part is saved (2nd part gets > truncated). > > Does anyone know why this is happening? I tried putting some sleeps > but that didn?t solve my problem. It almost seems that this works > as some kind of buffer, that only when emptied can store more data! > However, the qr/$prompt_default/ is working fine, as well as the > automatic prints to the screen! If you can see the expected content in $stdout then this probably has nothing to do with Expect.pm but rather with your perl coding. You don't show how you're writing to a file. If you're open()ing and print()ing to a filehandle, Perl tries very hard to flush buffers when the script ends, even if you don't close() the filehandle, so the results you describe are not likely. My guess is that you're redirecting output to a file, so it's not even a perl problem but a shell or terminal issue. Maybe you can coerce the buffer to flush by issuing a ^D or messing with the terminal settings using stty, but the simplest fix is probably to do the write-to-file in the perl script and let Perl handle it. Good luck! Ken -- Ken Irving Water and Environmental Research Center University of Alaska, Fairbanks |
From: Rui M. <rme...@te...> - 2007-05-08 18:59:33
|
First of all, thanks for the quick answer. Quoting Ken Irving: "If you can see the expected content in $stdout ..." But I can't see the expected result in $stdout. I can see the expected result (all the way to the matched prompt) being printed to my screen, = as Expect prints all the outputs by default. But I only get "Part1" of the output in $stdout (and, of course, in my file). I'm using the standard Perl save-to-file mechanism: open(DEST, '>' . $filename); flock(DEST, 2); print DEST $stdout; close DEST; This only happens when the outputs are long (and it happens ALWAYS, and = in the same spot). Do you have any idea of what this could be? Thanks. -----Original Message----- From: exp...@li... [mailto:exp...@li...] On Behalf Of = Ken Irving Sent: ter=E7a-feira, 8 de Maio de 2007 18:13 To: exp...@li... Subject: Re: [Expectperl-discuss] Output from before() gets truncated = ifit's too long On Tue, May 08, 2007 at 02:45:00PM +0100, Rui Meireles wrote: > Hi. I?m having a problem using Expect.pm >=20 > I created a script to do automated ssh to several hosts. One thing I > need to do is to enter several Cisco routers, execute the commands > terminal length 0 and show running-config and save some of the output > to a file. > > It?s all working fine, and as the commands are executed the correct > output is automatically printed in the screen (expect.pm default > option). However, when the output of the command is very long (show > running-config) the output is not correctly stored in my variable > $stdout, it?s always truncated in the same spot! >=20 > Example: (These are parts of my code) > ... > $stdout =3D $exp->before() . $exp->match() . $exp->after(); > ... >=20 > print ?###############\n?; > print ?$stdout\n?; > print ?###############\n?; > ...=20 > > What happens is that when I print $stdout the output I get is: >=20 > ############### > (1st part of output) > ############### > (2nd part of output) >=20 > And if I save $stdout to a file, only 1st part is saved (2nd part gets > truncated). > > Does anyone know why this is happening? I tried putting some sleeps > but that didn?t solve my problem. It almost seems that this works > as some kind of buffer, that only when emptied can store more data! > However, the qr/$prompt_default/ is working fine, as well as the > automatic prints to the screen! If you can see the expected content in $stdout then this probably has nothing to do with Expect.pm but rather with your perl coding. You=20 don't show how you're writing to a file. If you're open()ing and print()ing to a filehandle, Perl tries very hard to flush buffers when the script ends, even if you don't close() the filehandle, so the results you describe are not likely. My guess is that you're redirecting output to a file, so it's not even=20 a perl problem but a shell or terminal issue. Maybe you can coerce the buffer to flush by issuing a ^D or messing with the terminal settings=20 using stty, but the simplest fix is probably to do the write-to-file in the perl script and let Perl handle it. Good luck! Ken --=20 Ken Irving Water and Environmental Research Center University of Alaska, Fairbanks -------------------------------------------------------------------------= This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Expectperl-discuss mailing list Exp...@li... https://lists.sourceforge.net/lists/listinfo/expectperl-discuss |
From: Roland G. <rgi...@cp...> - 2007-05-09 11:20:40
|
Have you tried debugging it by setting $Expect::Exp_Internal =3D 1; ?? This will tell you exactly what strings get matched etc. Hope this helps, Roland On 5/8/07, Rui Meireles <rme...@te...> wrote: > > First of all, thanks for the quick answer. > > Quoting Ken Irving: > "If you can see the expected content in $stdout ..." > > But I can't see the expected result in $stdout. I can see the expected > result (all the way to the matched prompt) being printed to my screen, as > Expect prints all the outputs by default. > But I only get "Part1" of the output in $stdout (and, of course, in my > file). > > I'm using the standard Perl save-to-file mechanism: > > open(DEST, '>' . $filename); > flock(DEST, 2); > print DEST $stdout; > close DEST; > > > This only happens when the outputs are long (and it happens ALWAYS, and i= n > the same spot). > Do you have any idea of what this could be? > Thanks. > > > -----Original Message----- > From: exp...@li... > [mailto:exp...@li...] On Behalf Of Ke= n > Irving > Sent: ter=E7a-feira, 8 de Maio de 2007 18:13 > To: exp...@li... > Subject: Re: [Expectperl-discuss] Output from before() gets truncated ifi= t's > too long > > On Tue, May 08, 2007 at 02:45:00PM +0100, Rui Meireles wrote: > > Hi. I?m having a problem using Expect.pm > > > > I created a script to do automated ssh to several hosts. One thing I > > need to do is to enter several Cisco routers, execute the commands > > terminal length 0 and show running-config and save some of the output > > to a file. > > > > It?s all working fine, and as the commands are executed the correct > > output is automatically printed in the screen (expect.pm default > > option). However, when the output of the command is very long (show > > running-config) the output is not correctly stored in my variable > > $stdout, it?s always truncated in the same spot! > > > > Example: (These are parts of my code) > > ... > > $stdout =3D $exp->before() . $exp->match() . $exp->after(); > > ... > > > > print ?###############\n?; > > print ?$stdout\n?; > > print ?###############\n?; > > ... > > > > What happens is that when I print $stdout the output I get is: > > > > ############### > > (1st part of output) > > ############### > > (2nd part of output) > > > > And if I save $stdout to a file, only 1st part is saved (2nd part gets > > truncated). > > > > Does anyone know why this is happening? I tried putting some sleeps > > but that didn?t solve my problem. It almost seems that this works > > as some kind of buffer, that only when emptied can store more data! > > However, the qr/$prompt_default/ is working fine, as well as the > > automatic prints to the screen! > > If you can see the expected content in $stdout then this probably has > nothing to do with Expect.pm but rather with your perl coding. You > don't show how you're writing to a file. If you're open()ing > and print()ing to a filehandle, Perl tries very hard to flush buffers > when the script ends, even if you don't close() the filehandle, so > the results you describe are not likely. > > My guess is that you're redirecting output to a file, so it's not even > a perl problem but a shell or terminal issue. Maybe you can coerce the > buffer to flush by issuing a ^D or messing with the terminal settings > using stty, but the simplest fix is probably to do the write-to-file in > the perl script and let Perl handle it. > > Good luck! > > Ken > > -- > Ken Irving > Water and Environmental Research Center > University of Alaska, Fairbanks > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |