我寫了一個使用正則表達式並從命令輸出中打印所需字符串的函數。 該腳本按預期工作。但它不支持動態輸出。目前,我使用正則表達式「icmp」和「ok」並打印值。現在,type
,destination
和return code
可能會改變。命令完全沒有返回輸出的可能性很高。我如何處理這種情況?從命令輸出中打印字符串的正則表達式
sub check_summary{
my ($self) = @_;
my $type = 0;
my $return_type = 0;
my $ipsla = $self->{'ssh_obj'}->exec('show ip sla');
foreach my $line($ipsla) {
if ($line =~ m/(icmp)/) {
$type = $1;
}
if ($line =~ m/(OK)/) {
$return_type = $1;
}
}
INFO ($type,$return_type);
}
command Ouptut :
PSLAs Latest Operation Summary
Codes: * active,^inactive, ~ pending
ID Type Destination Stats Return Last
(ms) Code Run
-----------------------------------------------------------------------
*1 icmp 192.168.25.14 RTT=1 OK 1 second ago
雖然技術上「使用」正則表達式,但由於您使用固定子串作爲模式,因此您並不真正「使用」它們的功能。從這裏開始http://perldoc.perl.org/perlrequick.html獲取快速教程。 – mp3
其他文檔[perlrequick](http://perldoc.perl.org/perlrequick.html)[perlre](http://perldoc.perl.org/perlre.html)[perlreref](http:// perldoc.perl.org/perlreref.html)[perlretut](http://perldoc.perl.org/perlretut.html) – mp3
(1)什麼是'$ ipsla'? 「foreach」需要一個列表來迭代 - 是一個arrayref? (2)感興趣的線是否真的(並且總是)是命令輸出的最後一行? – zdim