From: Noah <ad...@en...> - 2007-11-14 20:02:03
|
Hi there, I have multi-line output that I am hoping to capture repetitive output and hoping to dump the collected matches to a data file. Is there an example of how to use perl expect module to do this? Let me know if I need more details to make things make more sense. Cheers, Noah |
From: Ken I. <fn...@ua...> - 2007-11-14 20:14:51
|
On Wed, Nov 14, 2007 at 02:01:28PM -0600, Noah wrote: > Hi there, > > I have multi-line output that I am hoping to capture repetitive output > and hoping to dump the collected matches to a data file. > > Is there an example of how to use perl expect module to do this? >From this description there's no need for expect; just pipe or otherwise save the output into a file. > Let me know if I need more details to make things make more sense. I don't know; it sounds simple enough as it is. ;-) Expect is useful if you need to send commands to a process to get it to send anything, more particularly if there's a need for automated interaction with the process. > > Cheers, > > Noah Good luck! Ken -- Ken Irving, fn...@ua... |
From: Bradford R. <bt...@po...> - 2007-11-14 20:31:28
|
On Nov 14, 2007 3:14 PM, Ken Irving <fn...@ua...> wrote: > On Wed, Nov 14, 2007 at 02:01:28PM -0600, Noah wrote: > > Hi there, > > > > I have multi-line output that I am hoping to capture repetitive output > > and hoping to dump the collected matches to a data file. > > > > Is there an example of how to use perl expect module to do this? > > From this description there's no need for expect; just pipe or otherwise > save the output into a file. I'm assuming the output is coming from a remote connection to something, not a command run at the shell... I'm pretty sure expect can do that, but I would need more details. Can you give some sample output and an example of what you consider a "match" that you want to save? You'll probably end up calling expect over and over again to get each match, so that means you may also need to look for something that tells you when to stop looking. > > > Let me know if I need more details to make things make more sense. > > I don't know; it sounds simple enough as it is. ;-) > > Expect is useful if you need to send commands to a process to get it > to send anything, more particularly if there's a need for automated > interaction with the process. > > > > > Cheers, > > > > Noah > > Good luck! > > Ken > -- > Ken Irving, fn...@ua... > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |
From: Noah <ad...@en...> - 2007-11-14 20:41:06
|
Ken Irving wrote: > On Wed, Nov 14, 2007 at 02:01:28PM -0600, Noah wrote: >> Hi there, >> >> I have multi-line output that I am hoping to capture repetitive output >> and hoping to dump the collected matches to a data file. >> >> Is there an example of how to use perl expect module to do this? > >>From this description there's no need for expect; just pipe or otherwise > save the output into a file. no its an interactive with a CLI expect is required. plus I am not capturing all the output only matched regexp. cheers, Noah |
From: Noah <ad...@en...> - 2007-11-14 20:46:31
|
> > I'm assuming the output is coming from a remote connection to something, > not a command run at the shell... I'm pretty sure expect can do that, > but I would need more details. Can you give some sample output and an > example of what you consider a "match" that you want to save? You'll > probably end up calling expect over and over again to get each match, so > that means you may also need to look for something that tells you when > to stop looking. > Hi, okay I am attempting to grab the interface and Unit number like t1-1/1/0.0 for each logical interface to t1-1/1/0.9, the Input rate, Output rate, and the remote IP address out of the following output: sample output - yup the results from a JUNOS CLI command. Physical interface: t1-1/1/0, Enabled, Physical link is Down Interface index: 141, SNMP ifIndex: 38 Link-level type: Frame-Relay, MTU: 1504, Clocking: Internal, Speed: T1, Loopback: None, FCS: 16, Framing: ESF Device flags : Present Running Down Interface flags: Hardware-Down Link-Layer-Down Point-To-Point SNMP-Traps Internal: 0x4000 Link flags : Keepalives DTE ANSI LMI settings: n391dte 6, n392dte 3, n393dte 4, t391dte 10 seconds LMI: Input: 0 (never), Output: 2310 (00:00:08 ago) DTE statistics: Enquiries sent : 1930 Full enquiries sent : 380 Enquiry responses received : 0 Full enquiry responses received : 0 DCE statistics: Enquiries received : 0 Full enquiries received : 0 Enquiry responses sent : 0 Full enquiry responses sent : 0 Common statistics: Unknown messages received : 0 Asynchronous updates received : 0 Out-of-sequence packets received : 0 Keepalive responses timedout : 1 CoS queues : 4 supported, 4 maximum usable queues Last flapped : 2007-10-19 20:11:51 UTC (3w5d 00:27 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) DS1 alarms : LOF, LOS DS1 defects : LOF, LOS Logical interface t1-1/1/0.1 (Index 72) (SNMP ifIndex 5946) Description: globbers1 Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 10.10.10/24, Local: 10.10.10.1, Broadcast: 10.10.10.255 Addresses, Flags: Dest-route-down Is-Preferred Destination: 192.168.1.0/30, Local: 192.168.1.1, Broadcast: 192.168.1.3 DLCI 1 Flags: Down, DCE-Unconfigured Total down time: 06:22:08 sec, Last down: 06:22:08 ago Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.2 (Index 73) (SNMP ifIndex 5947) Description: globbers2 Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.4/30, Local: 192.168.1.5, Broadcast: 192.168.1.7 DLCI 2 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.3 (Index 74) (SNMP ifIndex 5948) Description: globbers3 Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.8/30, Local: 192.168.1.9, Broadcast: 192.168.1.11 DLCI 3 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.4 (Index 75) (SNMP ifIndex 5949) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.12/30, Local: 192.168.1.13, Broadcast: 192.168.1.15 DLCI 4 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.5 (Index 76) (SNMP ifIndex 5950) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.16/30, Local: 192.168.1.17, Broadcast: 192.168.1.19 DLCI 5 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.6 (Index 77) (SNMP ifIndex 5951) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.20/30, Local: 192.168.1.21, Broadcast: 192.168.1.23 DLCI 6 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.7 (Index 78) (SNMP ifIndex 5952) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.24/30, Local: 192.168.1.25, Broadcast: 192.168.1.27 DLCI 7 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.8 (Index 79) (SNMP ifIndex 5953) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.28/30, Local: 192.168.1.29, Broadcast: 192.168.1.31 DLCI 8 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 Logical interface t1-1/1/0.9 (Index 80) (SNMP ifIndex 5954) Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID Input packets : 0 Output packets: 0 Protocol inet, MTU: 1500 Flags: None Addresses, Flags: Dest-route-down Is-Preferred Is-Primary Destination: 192.168.1.32/30, Local: 192.168.1.33, Broadcast: 192.168.1.35 DLCI 9 Flags: Active Total down time: 0 sec, Last down: Never Input packets : 0 Output packets: 0 DLCI statistics: Active DLCI :8 Inactive DLCI :1 |
From: Bradford R. <bt...@po...> - 2007-11-14 20:59:35
|
On Nov 14, 2007 3:46 PM, Noah <ad...@en...> wrote: > > > > > I'm assuming the output is coming from a remote connection to something, > > not a command run at the shell... I'm pretty sure expect can do that, > > but I would need more details. Can you give some sample output and an > > example of what you consider a "match" that you want to save? You'll > > probably end up calling expect over and over again to get each match, so > > that means you may also need to look for something that tells you when > > to stop looking. > > > > > Hi, > > okay I am attempting to grab the interface and Unit number like > t1-1/1/0.0 for each logical interface to t1-1/1/0.9, the Input rate, > Output rate, and the remote IP address out of the following output: > > > > sample output - yup the results from a JUNOS CLI command. *Sounds like you really only need expect to do the remote connection/login part. If it were me, I would do the minimum with expect to just send the command and capture all of the output to an array (ie. one call to expect, using the prompt as the "match" string), and then parse the output the way you would normally parse an input stream, like with a foreach loop or something. * > > > Physical interface: t1-1/1/0, Enabled, Physical link is Down > Interface index: 141, SNMP ifIndex: 38 > Link-level type: Frame-Relay, MTU: 1504, Clocking: Internal, Speed: T1, > Loopback: None, FCS: 16, Framing: ESF > Device flags : Present Running Down > Interface flags: Hardware-Down Link-Layer-Down Point-To-Point > SNMP-Traps Internal: 0x4000 > Link flags : Keepalives DTE > ANSI LMI settings: n391dte 6, n392dte 3, n393dte 4, t391dte 10 seconds > LMI: Input: 0 (never), Output: 2310 (00:00:08 ago) > DTE statistics: > Enquiries sent : 1930 > Full enquiries sent : 380 > Enquiry responses received : 0 > Full enquiry responses received : 0 > DCE statistics: > Enquiries received : 0 > Full enquiries received : 0 > Enquiry responses sent : 0 > Full enquiry responses sent : 0 > Common statistics: > Unknown messages received : 0 > Asynchronous updates received : 0 > Out-of-sequence packets received : 0 > Keepalive responses timedout : 1 > CoS queues : 4 supported, 4 maximum usable queues > Last flapped : 2007-10-19 20:11:51 UTC (3w5d 00:27 ago) > Input rate : 0 bps (0 pps) > Output rate : 0 bps (0 pps) > DS1 alarms : LOF, LOS > DS1 defects : LOF, LOS > > Logical interface t1-1/1/0.1 (Index 72) (SNMP ifIndex 5946) > Description: globbers1 > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 10.10.10/24, Local: 10.10.10.1, Broadcast: > 10.10.10.255 > Addresses, Flags: Dest-route-down Is-Preferred > Destination: 192.168.1.0/30, Local: 192.168.1.1, Broadcast: > 192.168.1.3 > DLCI 1 > Flags: Down, DCE-Unconfigured > Total down time: 06:22:08 sec, Last down: 06:22:08 ago > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.2 (Index 73) (SNMP ifIndex 5947) > Description: globbers2 > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.4/30, Local: 192.168.1.5, Broadcast: > 192.168.1.7 > DLCI 2 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.3 (Index 74) (SNMP ifIndex 5948) > Description: globbers3 > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.8/30, Local: 192.168.1.9, Broadcast: > 192.168.1.11 > DLCI 3 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.4 (Index 75) (SNMP ifIndex 5949) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.12/30, Local: 192.168.1.13, Broadcast: > 192.168.1.15 > DLCI 4 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.5 (Index 76) (SNMP ifIndex 5950) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.16/30, Local: 192.168.1.17, Broadcast: > 192.168.1.19 > DLCI 5 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.6 (Index 77) (SNMP ifIndex 5951) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.20/30, Local: 192.168.1.21, Broadcast: > 192.168.1.23 > DLCI 6 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.7 (Index 78) (SNMP ifIndex 5952) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.24/30, Local: 192.168.1.25, Broadcast: > 192.168.1.27 > DLCI 7 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.8 (Index 79) (SNMP ifIndex 5953) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.28/30, Local: 192.168.1.29, Broadcast: > 192.168.1.31 > DLCI 8 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > > Logical interface t1-1/1/0.9 (Index 80) (SNMP ifIndex 5954) > Flags: Device-Down Point-To-Point SNMP-Traps Encapsulation: FR-NLPID > Input packets : 0 > Output packets: 0 > Protocol inet, MTU: 1500 > Flags: None > Addresses, Flags: Dest-route-down Is-Preferred Is-Primary > Destination: 192.168.1.32/30, Local: 192.168.1.33, Broadcast: > 192.168.1.35 > DLCI 9 > Flags: Active > Total down time: 0 sec, Last down: Never > Input packets : 0 > Output packets: 0 > DLCI statistics: > Active DLCI :8 Inactive DLCI :1 > > > > |
From: Noah <ad...@en...> - 2007-11-14 21:40:02
|
Bradford Ritchie wrote: > > > On Nov 14, 2007 3:46 PM, Noah <ad...@en... > <mailto:ad...@en...>> wrote: > > > > > > I'm assuming the output is coming from a remote connection to > something, > > not a command run at the shell... I'm pretty sure expect can do > that, > > but I would need more details. Can you give some sample output > and an > > example of what you consider a "match" that you want to save? You'll > > probably end up calling expect over and over again to get each > match, so > > that means you may also need to look for something that tells you > when > > to stop looking. > > > > > Hi, > > okay I am attempting to grab the interface and Unit number like > t1-1/1/0.0 for each logical interface to t1-1/1/0.9, the Input rate, > Output rate, and the remote IP address out of the following output: > > > > sample output - yup the results from a JUNOS CLI command. > > > *Sounds like you really only need expect to do the remote > connection/login part. If it were me, I would do the minimum with > expect to just send the command and capture all of the output to an > array (ie. one call to expect, using the prompt as the "match" string), > and then parse the output the way you would normally parse an input > stream, like with a foreach loop or something. > is there a way to capture the output with perl-expect into an array? Cheers, noah |
From: Ken I. <fn...@ua...> - 2007-11-14 21:57:28
|
On Wed, Nov 14, 2007 at 03:39:45PM -0600, Noah wrote: > > Bradford Ritchie wrote: >> On Nov 14, 2007 3:46 PM, Noah <ad...@en... >> <mailto:ad...@en...>> wrote: >> > >> > I'm assuming the output is coming from a remote connection to something, >> > not a command run at the shell... I'm pretty sure expect can do that, >> > but I would need more details. Can you give some sample output and an >> > example of what you consider a "match" that you want to save? You'll >> > probably end up calling expect over and over again to get each match, so >> > that means you may also need to look for something that tells you when >> > to stop looking. >> > >> Hi, >> okay I am attempting to grab the interface and Unit number like >> t1-1/1/0.0 for each logical interface to t1-1/1/0.9, the Input rate, >> Output rate, and the remote IP address out of the following output: >> sample output - yup the results from a JUNOS CLI command. >> *Sounds like you really only need expect to do the remote connection/login >> part. If it were me, I would do the minimum with expect to just send the >> command and capture all of the output to an array (ie. one call to expect, >> using the prompt as the "match" string), and then parse the output the way >> you would normally parse an input stream, like with a foreach loop or >> something. > > is there a way to capture the output with perl-expect into an array? One way would be to receive a string and split it aftwards, e.g., $foo->expect($timeout, 'regex', $some_terminating_pattern); my @results = split /\n/, $foo->match(); ... Ken -- Ken Irving, fn...@ua... |
From: Noah <ad...@en...> - 2007-11-14 23:59:19
|
Matt, I am not doing something correct here. I run my script and get the following: "Can't call method "put" on an undefined value at ./maintenance.check.pl line 96." Cheers, Noah Matt Zagrabelny wrote: > On Wed, 2007-11-14 at 15:39 -0600, Noah wrote: > > [...] > >> is there a way to capture the output with perl-expect into an array? > > Here are some snippets of what I have done in the past, though I do > things a little differently now, you will get the idea... hopefully. ;) > > Subroutine to send a command to Cisco devices and capture the output in > the scalar reference $receive_buffer. > > sub send_and_receive { > my $configs = shift; > my $send_string = shift; > my $receive_buffer = shift; > my $command = shift; > my $wait_for_found = (@_) ? shift : undef; > my $more_output = (@_) ? shift : '--More--'; > my $continue_command = (@_) ? shift : " "; > my $wait_for = (@_) ? shift : '>$'; > > # send whatever it is we are to send > $command->put($send_string); > > # now wait for the "wait_for" meanwhile saving > # all the output into receive_buffer > my $done = undef; > until ($done) { > my ($prematch, $match) = (undef, undef); > ($prematch, $match) = > $command->waitfor(Match => '/'.$more_output.'/i', > Match => '/'.$wait_for.'/i', > Match => '/\\[X\\] Exit.*:/ism', > Timeout => $configs->{management_timeout}); > > if ($receive_buffer ne undef) { > $$receive_buffer .= $prematch.$match; > } > > if ($match =~ /$more_output/i) { > $command->put($continue_command); > } elsif ($match =~ /$wait_for/) { > $$wait_for_found = 1 if ($wait_for_found); > $done = 1; > } elsif ($match =~ /\[X\] Exit.*:/ism) { > $$wait_for_found = 1 if ($wait_for_found); > $done = 1; > } > } > } > > $text_io = ''; > &send_and_receive($configs, > "show interface status\n", > \$text_io, > $connection, > \$found_prompt); > > my @lines = split /\n/, $text_io; > for (@lines) { > $_ = &trim($_); > } > > sub trim { > my $s = shift; > $s =~ s/^\s*//; > $s =~ s/\s*$//; > return $s; > } > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss |
From: Matt Z. <mzagrabe@d.umn.edu> - 2007-11-15 14:50:19
|
On Wed, 2007-11-14 at 17:58 -0600, Noah wrote: > Matt, >=20 > I am not doing something correct here. No, I am not doing something correct. I mistakenly sent you code snippets that made use of Net::Telnet. Here is a little more complete set of code, note that I may have missed copying something from my source files. Nevertheless, you will get the idea. my $found_prompt =3D undef; my $text_io =3D ''; my $exp =3D &log_in_to_infrastructure($configs, $infrastructure, \$text_io, \$found_prompt, $command); # process text_io here if you want. if ($found_prompt) { $found_prompt =3D undef; $text_io =3D ''; &send_and_receive($configs, "show interfaces status\n", \$text_io, $exp, \$found_prompt); # process text_io here } sub send_and_receive { my $configs =3D shift; my $send_string =3D shift; my $receive_buffer =3D shift; my $expect_object =3D shift; my $wait_for_found =3D shift; my $more_output =3D (@_) ? shift : '--More--'; my $continue_command =3D (@_) ? shift : ' '; my $wait_for =3D (@_) ? shift : '>$'; # set up logging the output to the buffer if ($receive_buffer ne undef) { $expect_object->log_file(sub { $$receive_buffer .=3D shift;}); } =20 # send the string $expect_object->send($send_string); # dont stop logging until we have seen the $wait_for $expect_object->expect($configs->{'management_timeout'}, [ qr/$more_output/i, sub {my $self =3D shift; $self->send($continue_command); exp_continue;}], # 1924 specific prompt [ qr/\[X\] Exit.*:/ism, sub { $$wait_for_found =3D 1; undef;}], # user defined prompt [ qr/$wait_for/i, sub { $$wait_for_found =3D 1; undef;}]); } sub log_in_to_infrastructure { my $configs =3D shift; my $infrastructure =3D shift; my $receive_buffer =3D shift; my $wait_for_found =3D shift; my $command =3D (@_) ? shift : 'telnet'; my $use_menu_1924 =3D (@_) ? shift : undef; my $wait_for =3D (@_) ? shift : '>$'; my $username =3D (@_) ? shift : $configs->{'management_user'}; my $password =3D (@_) ? shift : $configs->{'management_password'}; if ($command eq 'ssh') { $command =3D '/usr/bin/ssh -l '.$username.' '; } else { $command =3D '/usr/bin/telnet '; } my $exp =3D new Expect; $exp->slave->stty(qw(raw -echo)); my $exp =3D Expect->spawn("$command $infrastructure") or die "Cannot spawn $command: $!\n"; $exp->log_stdout(0); if ($receive_buffer ne undef) { $exp->log_file(sub { $$receive_buffer .=3D shift;}); } # note retval is currently unused my $retval =3D $exp->expect($configs->{'management_timeout'}, [ qr/user(name)?: /i, sub { my $self =3D shift; $self->send("$username\n"); exp_continue;}], [ qr/password:\s*/i, sub { my $self =3D shift; $self->send("$password\n"); exp_continue;}], [ qr/Catalyst 1900 Management Console.*Enter Selection: /ism, sub { my $self =3D shift; $self->send($use_menu_1924 ? 'm' : 'k'); exp_continue;}], [ qr/\[X\] Exit Management Console.*Enter Selection:/ism, sub { $$wait_for_found =3D 1; undef;}], # if first time ssh'ing to a infrastructure, then accept # the public key [ qr/\(yes\/no\)\?/i, sub { my $self =3D shift; $self->send("yes\n"); exp_continue;}], [ qr/$wait_for/i, sub { $$wait_for_found =3D 1; undef;}]); return $exp; } > I run my script and get the following: > "Can't call method "put" on an undefined value at ./maintenance.check.pl=20 > line 96." >=20 > Cheers, >=20 > Noah >=20 >=20 > Matt Zagrabelny wrote: > > On Wed, 2007-11-14 at 15:39 -0600, Noah wrote: > >=20 > > [...] > >=20 > >> is there a way to capture the output with perl-expect into an array? > >=20 > > Here are some snippets of what I have done in the past, though I do > > things a little differently now, you will get the idea... hopefully. ;) > >=20 > > Subroutine to send a command to Cisco devices and capture the output in > > the scalar reference $receive_buffer. > >=20 > > sub send_and_receive { > > my $configs =3D shift; > > my $send_string =3D shift; > > my $receive_buffer =3D shift; > > my $command =3D shift; > > my $wait_for_found =3D (@_) ? shift : undef; > > my $more_output =3D (@_) ? shift : '--More--'; > > my $continue_command =3D (@_) ? shift : " ";=20 > > my $wait_for =3D (@_) ? shift : '>$'; > > =20 > > # send whatever it is we are to send > > $command->put($send_string); > >=20 > > # now wait for the "wait_for" meanwhile saving > > # all the output into receive_buffer > > my $done =3D undef; > > until ($done) { > > my ($prematch, $match) =3D (undef, undef); > > ($prematch, $match) =3D > > $command->waitfor(Match =3D> '/'.$more_output.'/i', > > Match =3D> '/'.$wait_for.'/i', > > Match =3D> '/\\[X\\] Exit.*:/ism', > > Timeout =3D> $configs->{management_timeout}); > >=20 > > if ($receive_buffer ne undef) { > > $$receive_buffer .=3D $prematch.$match; > > } =20 > >=20 > > if ($match =3D~ /$more_output/i) { > > $command->put($continue_command); > > } elsif ($match =3D~ /$wait_for/) { > > $$wait_for_found =3D 1 if ($wait_for_found); > > $done =3D 1;=20 > > } elsif ($match =3D~ /\[X\] Exit.*:/ism) { > > $$wait_for_found =3D 1 if ($wait_for_found); > > $done =3D 1;=20 > > } =20 > > } > > } > >=20 > > $text_io =3D ''; > > &send_and_receive($configs, > > "show interface status\n", > > \$text_io, > > $connection, > > \$found_prompt); > >=20 > > my @lines =3D split /\n/, $text_io; > > for (@lines) { > > $_ =3D &trim($_); > > } > >=20 > > sub trim { > > my $s =3D shift; > > $s =3D~ s/^\s*//; > > $s =3D~ s/\s*$//; > > return $s; > > } =20 > >=20 [...] --=20 Matt Zagrabelny - mzagrabe@d.umn.edu - (218) 726 8844 University of Minnesota Duluth Information Technology Systems & Services PGP key 1024D/84E22DA2 2005-11-07 Fingerprint: 78F9 18B3 EF58 56F5 FC85 C5CA 53E7 887F 84E2 2DA2 He is not a fool who gives up what he cannot keep to gain what he cannot lose. -Jim Elliot |
From: Matt Z. <mzagrabe@d.umn.edu> - 2007-11-14 22:15:21
|
On Wed, 2007-11-14 at 15:39 -0600, Noah wrote: [...] > is there a way to capture the output with perl-expect into an array? Here are some snippets of what I have done in the past, though I do things a little differently now, you will get the idea... hopefully. ;) Subroutine to send a command to Cisco devices and capture the output in the scalar reference $receive_buffer. sub send_and_receive { my $configs =3D shift; my $send_string =3D shift; my $receive_buffer =3D shift; my $command =3D shift; my $wait_for_found =3D (@_) ? shift : undef; my $more_output =3D (@_) ? shift : '--More--'; my $continue_command =3D (@_) ? shift : " ";=20 my $wait_for =3D (@_) ? shift : '>$'; =20 # send whatever it is we are to send $command->put($send_string); # now wait for the "wait_for" meanwhile saving # all the output into receive_buffer my $done =3D undef; until ($done) { my ($prematch, $match) =3D (undef, undef); ($prematch, $match) =3D $command->waitfor(Match =3D> '/'.$more_output.'/i', Match =3D> '/'.$wait_for.'/i', Match =3D> '/\\[X\\] Exit.*:/ism', Timeout =3D> $configs->{management_timeout}); if ($receive_buffer ne undef) { $$receive_buffer .=3D $prematch.$match; } =20 if ($match =3D~ /$more_output/i) { $command->put($continue_command); } elsif ($match =3D~ /$wait_for/) { $$wait_for_found =3D 1 if ($wait_for_found); $done =3D 1;=20 } elsif ($match =3D~ /\[X\] Exit.*:/ism) { $$wait_for_found =3D 1 if ($wait_for_found); $done =3D 1;=20 } =20 } } $text_io =3D ''; &send_and_receive($configs, "show interface status\n", \$text_io, $connection, \$found_prompt); my @lines =3D split /\n/, $text_io; for (@lines) { $_ =3D &trim($_); } sub trim { my $s =3D shift; $s =3D~ s/^\s*//; $s =3D~ s/\s*$//; return $s; } =20 --=20 Matt Zagrabelny - mzagrabe@d.umn.edu - (218) 726 8844 University of Minnesota Duluth Information Technology Systems & Services PGP key 1024D/84E22DA2 2005-11-07 Fingerprint: 78F9 18B3 EF58 56F5 FC85 C5CA 53E7 887F 84E2 2DA2 He is not a fool who gives up what he cannot keep to gain what he cannot lose. -Jim Elliot |
From: Noah <ad...@en...> - 2007-11-14 22:31:53
|
thanks Matt. this is exactly what I am looking fer. cheers, Noah Matt Zagrabelny wrote: > On Wed, 2007-11-14 at 15:39 -0600, Noah wrote: > > [...] > >> is there a way to capture the output with perl-expect into an array? > > Here are some snippets of what I have done in the past, though I do > things a little differently now, you will get the idea... hopefully. ;) > > Subroutine to send a command to Cisco devices and capture the output in > the scalar reference $receive_buffer. > > sub send_and_receive { > my $configs = shift; > my $send_string = shift; > my $receive_buffer = shift; > my $command = shift; > my $wait_for_found = (@_) ? shift : undef; > my $more_output = (@_) ? shift : '--More--'; > my $continue_command = (@_) ? shift : " "; > my $wait_for = (@_) ? shift : '>$'; > > # send whatever it is we are to send > $command->put($send_string); > > # now wait for the "wait_for" meanwhile saving > # all the output into receive_buffer > my $done = undef; > until ($done) { > my ($prematch, $match) = (undef, undef); > ($prematch, $match) = > $command->waitfor(Match => '/'.$more_output.'/i', > Match => '/'.$wait_for.'/i', > Match => '/\\[X\\] Exit.*:/ism', > Timeout => $configs->{management_timeout}); > > if ($receive_buffer ne undef) { > $$receive_buffer .= $prematch.$match; > } > > if ($match =~ /$more_output/i) { > $command->put($continue_command); > } elsif ($match =~ /$wait_for/) { > $$wait_for_found = 1 if ($wait_for_found); > $done = 1; > } elsif ($match =~ /\[X\] Exit.*:/ism) { > $$wait_for_found = 1 if ($wait_for_found); > $done = 1; > } > } > } > > $text_io = ''; > &send_and_receive($configs, > "show interface status\n", > \$text_io, > $connection, > \$found_prompt); > > my @lines = split /\n/, $text_io; > for (@lines) { > $_ = &trim($_); > } > > sub trim { > my $s = shift; > $s =~ s/^\s*//; > $s =~ s/\s*$//; > return $s; > } > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss |