From: David S. <Dav...@bt...> - 2002-11-20 22:29:03
|
Hello, I made a module that uses expect to telnet to a site and capture 5 minutes of streaming data at the site. The plan was to use threading to have multiple telnet sessions going at the same time to different hosts. The problem is that I see the expect session start the telnet, but it quicly decides there is not match for the -i regexp, sees EOF, and exits. Has anybody done this kind of thing or know what I need to do to get it to work? Does it have something to do with STDOUT needing to get past somewhere somehow? The gist of it is: package module use threads; use Expect; use strict; sub new $self = { ip => undef, stuff here } stuff here bless $self... } sub telnet { my $self = shift; lock($self); my $timeout = 300; if ($self->{timeout}) { $timeout = $self->{timeout}; } $Expect::Log_Stdout = 0; $Expect::Exp_Internal = 1 if $self->{debug} > 1; $Expect::Debug = 1 if $self->{debug} > 2; $Expect::Log_Stdout = 1 if $self->{debug}; $conn = Expect->spawn("/usr/bin/telnet $address"); sleep 1; my $spawn_ok; my $read = undef; $conn->expect($timeout, [ qr'Username: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$acs_login\r"); exp_continue; ], [ 'Password: $', sub { my $fh = shift; $conn->clear_accum(); $fh->send("$acs_passwd\r"); exp_continue; } ], [ '-re', '--->$', # Prompt never seen ], [ eof => sub { if ($spawn_ok) { $self->{log}->entry("Disconnected Login.",DIE); } else { $self->{log}->entry("ACS Login not seen",DIE); } } ], [ timeout => sub { $read = $conn->exp_before(); if ($self->{debug}) { print $fh $read if $self->{debug}; } &process_5e_data($self,\$read); } ] ); return; Program uses module: use module.pm use threads; foreach .. { $switch{$2}{connection} = module::new( { ip => $ip, more here } ); if ($type eq "5e") { my $thr = threads->new(\&BTI::Switch::get_5e_report,$switch{$2}{connection}); push(@site,$thr); } elsif ( $type eq "600e") { my $thr = threads->new(\&BTI::Switch::get_600e_report,$switch{$2}{connection}); push(@site,$thr); } } foreach my $thr (@site) { $thr->join(); } The debug run looks like: spawn id(7): beginning expect. Timeout: 300 seconds. Current time: Wed Nov 20 17:25:28 2002 spawn id(7): list of patterns: #1: -re `(?-xism:Username: $)' #2: -re `Password: $' #3: -re `--->$' #4: -eof `' spawn id(7): Does `' match: pattern #1: -re `(?-xism:Username: $)'? No. pattern #2: -re `Password: $'? No. pattern #3: -re `--->$'? No. pattern #4: -eof `'? No. Waiting for new data (300 seconds)... spawn id(7): new data. spawn id(7): EOF Calling EOF hook CODE(0x87b723c)... ALERT: ACS Login prompt not seen Closing spawn id(7). Expect::hard_close('Expect=GLOB(0x87a5bcc)') called at /usr/local/lib/perl5/site_perl/5.8.0/Expect.pm line 1575 Expect::DESTROY('Expect=GLOB(0x87a5bcc)') called at ./collector.pl line 128 eval {...} called at ./collector.pl line 128 spawn id(7) closed. Pid 3843 of spawn id(7) terminated, Status: 0x01 |