我目前正在编写一个脚本来自动化服务器的配置。我使用Expect.pm模块与服务器交互,但现在我面临一个不知道如何解决的问题。
我所要做的是将命令send到服务器,该命令将列出当前安装在服务器上的设备,然后检查该列表中是否有某些项。问题是这个列表是随机排列的,所以我不知道我首先应该期望什么。
我试图完成的是查找所有项,然后在所有项都匹配时退出expect调用。请记住,每个服务器都有自己的设备集,我必须匹配的设备数量可能会有所不同,因此解决方案必须解决一般情况。有什么方法可以用Expect.pm来完成这个任务吗?我已经试了一段时间了,但我似乎找不到一个好的解决办法.
提前感谢!
/Haso
编辑:我找到了一个解决方案
我编写了一个函数,它将为expect调用构造参数数组,然后进行调用。在本例中,引用$self是我自己定义的对象,但$self->{_expect}是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;
}发布于 2013-07-18 10:58:56
成套装置
这是个神奇的词。使用集合::标量。
如果是$expect_devices->is_subset($known_devices),你就有了“匹配”。
https://stackoverflow.com/questions/17720043
复制相似问题