From: <db...@CT...> - 2003-02-28 19:48:27
|
If you want to capture the output of our spawned process into an array, there's a couple of ways to do it. Here's the way I prefer as it resembles the Tcl/Expect structure of accomplishing the same thing. There's other ways to do the matching; this is just one way. This is an example of spawning an "ls -la" command from the cmd line, and dumping 'stdout' to an array, then printing the array later. You could obviously do whatever you wanted to the array after you have the output. Substitute 'ls -la' for whatever and this should work for you: #!/usr/local/bin/perl use Expect; #------------------------------------------------- # turn off logging to console. If you want to see # stdout then just set this value back to '1'. #------------------------------------------------- $Expect::Log_Stdout=0; #---------------- # spawn a process #---------------- $session = Expect->spawn("/usr/local/bin/ls -la"); #------------------------ # expect our input lines: #------------------------ $session->expect(10, #--------------------------------------------- # if we match a line of text (STDOUT) from the # spawned process, push it to an array and continue #--------------------------------------------- '-re', "([^\r\n]+)[\r\n]+", sub { push(@output, ($session->matchlist)[0]); exp_continue; }, ); #----------------------------------------------------- # At this point, all output form spawned command is in # @output array. Let's print out the contents #----------------------------------------------------- foreach $row (@output) { print "**$row**\n"; } Hope this helps, David Basham db...@ct... "Maraglia, Dominicx" <dom...@in...> To: "'exp...@li...'" Sent by: <exp...@li...> exp...@li...urc cc: eforge.net Subject: [Expectperl-discuss] Capturing process output with Expect.pm 02/28/2003 12:13 PM Hello, I am having difficutlies while using the expect module to "capture" the output of a program called from within my perl script. Briefly, all I would like to do is: 1) call an interactive (shell) program from within my perl script 2) pass the program keystrokes 3) capture the text of certain responses from the shell program, preferablly in an array, as the shell program will return several lines of output which need to be analyzed. 1 and 2 are not problem at all, but I can not figure out number 3. Here is an example of what I am doing: ($cfg = Expect->spawn("cfg")) || die "Couldn't spawn relcfg, $!"; unless ($cfg->expect(10,"Enter selection: ")) { die "Execution of relcfg failed, ".$rel_cfg>exp_error()."\n"; } print $cfg "2\r"; unless ($cfg->expect(10,"Enter selection: ")) { die "Execution of relcfg failed, ".$cfg>exp_error()."\n"; } print $cfg "1\r"; unless ($cfg->expect(10,"Enter selection: ")) { die "Execution of relcfg failed, ".$cfg>exp_error()."\n"; } print $cfg "1\r"; unless ($cfg->expect(10,"Use CHAP for discovery [y/N/q]? ")) { die "Execution of relcfg failed, ".$cfg>exp_error()."\n"; } $cfg->send("N\r"); After sending the "N\r" the shell program will search and return information to the terminal and then prompt the user to quit the program; I can easily send commands to exit the program, but how do I capture the information returned by the program? I have tried the following constructs, and various permuatation, to no avail. $cfg->expect(0); $targets = $cfg->before(); @targets = $rel_cfg->before(); Thanks, Dominic ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf _______________________________________________ Expectperl-discuss mailing list Exp...@li... https://lists.sourceforge.net/lists/listinfo/expectperl-discuss |