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 |