我目前正在使用腳本來自動配置服務器。我使用Expect.pm
模塊與服務器交互,但現在我面臨一個我不知道如何解決的問題。使用Expect.pm在未知訂單中匹配多個模式
我想要做的是send
一個命令到服務器,它將列出當前安裝在服務器上的設備,然後檢查某些項目是否在該列表中。問題在於該列表是隨機排列的,所以我不知道我應該首先期望什麼項目。
我試圖完成的是找到所有項目,然後在所有項目匹配時退出expect
調用。請記住,每臺服務器都有自己的一套設備,我必須匹配的設備數量可能會有所不同,因此解決方案必須解決一般情況。有沒有辦法做到這一點Expect.pm
?我已經嘗試了一段時間,我似乎無法找到一個好的解決方案...
在此先感謝!
/Haso
編輯:我已經找到了解決辦法
我已經writen,將構建的參數數組爲expect
呼叫,然後撥打電話的功能。在這種情況下,參考文獻$self
是我自己定義的對象,但$self->{_expect}
是期望的對象。
sub match_all {
my $self = shift;
my $timeout = shift;
my @patterns = @_;
my $pattern_count = @patterns;
my $match_count = 0;
#Function that is called when a pattern is matched
sub match{
my $exp = shift;
my $mc_ptr = shift;
my $pc_ptr = shift;
$$mc_ptr++;
if($$mc_ptr != $$pc_ptr) {
#Set the accumelator to the before and after string,
#effectivly cutting away the matched substring.
my $before = $exp->before();
my $after = $exp->after();
$exp->set_accum($before.$after);
exp_continue_timeout;
}
}
#Build the array of patterns for the expect call
my @exp_patterns;
foreach my $pattern (@patterns) {
push @exp_patterns, [$pattern, \&match, \$match_count, \$pattern_count];
}
#Set notransfer to True in order to manipulate
#the accumelator on my own during this function
$self->{_expect}->notransfer(1);
$self->{_expect}->expect($timeout, @exp_patterns);
$self->{_expect}->notransfer(0);
return $match_count == $pattern_count;
}
嗨,謝謝你的回答。你提出的解決方案的確是解決這個問題的方法,儘管這不是真正需要的。問題是,爲了使用這個解決方案,我必須解析列表才能創建集合。另外,如果所有項目都在集合中,我必須在測試之前等待完全超時。 – Hasofanten