From: Roland G. <rgi...@cp...> - 2010-03-30 09:20:28
|
You can avoid all that hassle by doing the TCP connect directly in perl (directly with Socket or maybe with a little help from a nice CPAN module) and not in a spawned netcat. Expect doesn't need to spawn a program, it can also take a filehandle and work on that. Hope this helps, Roland Ken Irving schrieb: > Using Expect.pm 1.15, occasionally the spawned process fails for unknown > reasons. Up to now we just exit the program, but I'd like to give it > another try. I'm not clear on what exactly to retry, though. > > The code segment is currently: > > my $exp = Expect->new(); > $exp->raw_pty(1); > $exp->debug($debug); > $exp->log_file($nc_log) if $nc_log; > $exp->spawn("/bin/nc", $host, $port) > or die "Cannot spawn nc: $!\n"; > $exp->expect(10, '-re', 'CoraScript.*$') > or log_session('NO_CORA_CMD'); # log_session() invokes die() > > The process is spawned, but the subsequent expect() times out. > > I could perhaps do something like the following, i.e., just abandon the > Expect object (not tested): > > my $exp; > my $retries = 3; > while ( $retries ) { > $retries -= 1; > $exp = Expect->new(); > $exp->raw_pty(1); > $exp->debug($debug); > $exp->log_file($nc_log) if $nc_log; > $exp->spawn("/bin/nc", $host, $port) > or die "Cannot spawn nc: $!\n"; > break if $exp->expect(10, '-re', 'CoraScript.*$'); > $exp->hard_close(); > undef $exp; > } > log_session('NO_CORA_CMD') unless $exp; > ... > > But is there a cleaner or better way to approach this? > > Thanks for any hints or pointers, examples, etc. > > Ken > > |