perl - Match multiple patterns in unknow order with Expect.pm -
i working on script automate configuration of servers. use expect.pm
module interact server facing problem don't know how solve.
what trying send
command server list devices installed on server, , check if items in list. problems list in random order can't know item should expect first.
what i'm trying accomplish find items , exit expect
call when items has been matched. keep in mind every server has own set of devices , number of devices have match can vary, solution has solve general case. there way accomplish expect.pm
? have tried time , can't seem find solution...
thanks in advance!
/haso
edit: have found solution
i have writen function construct array of arguments expect
call , make call. reference $self
in case own defined object $self->{_expect}
expect object.
sub match_all { $self = shift; $timeout = shift; @patterns = @_; $pattern_count = @patterns; $match_count = 0; #function called when pattern matched sub match{ $exp = shift; $mc_ptr = shift; $pc_ptr = shift; $$mc_ptr++; if($$mc_ptr != $$pc_ptr) { #set accumelator before , after string, #effectivly cutting away matched substring. $before = $exp->before(); $after = $exp->after(); $exp->set_accum($before.$after); exp_continue_timeout; } } #build array of patterns expect call @exp_patterns; foreach $pattern (@patterns) { push @exp_patterns, [$pattern, \&match, \$match_count, \$pattern_count]; } #set notransfer true in order manipulate #the accumelator on own during function $self->{_expect}->notransfer(1); $self->{_expect}->expect($timeout, @exp_patterns); $self->{_expect}->notransfer(0); return $match_count == $pattern_count; }
set of devices
that's magic word. use set::scalar.
if $expect_devices->is_subset($known_devices)
, have "match".
Comments
Post a Comment