首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

抛砖引玉再求perl语句解决方法

2012-02-29 
抛砖引玉再求perl语句perl处理数据SNMPv2-SMI::mib-2.17.4.3.1.2.0.22.211.92.133.39 INTEGER: 14SNMPv2

抛砖引玉再求perl语句
perl处理数据
SNMPv2-SMI::mib-2.17.4.3.1.2.0.22.211.92.133.39 = INTEGER: 14
SNMPv2-SMI::mib-2.17.4.3.1.2.0.38.158.50.22.230 = INTEGER: 4
SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.24.38.91 = Hex-STRING: 00 16 D3 18 26 5B  
SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.92.133.39 = Hex-STRING: 00 16 D3 5C 85 27  
SNMPv2-SMI::mib-2.17.4.3.1.1.0.38.158.50.22.230 = Hex-STRING: 00 26 9E 32 16 E6

通过代码得到
14#00 16 D3 5C 85 27
4#00 26 9E 32 16 E6 

参考代码
open F, "your_file";
my (%a,%b);
while (my $line = <F>) {
  chomp($line);
  if ($line =~ /(?:\d+?\.){7}([\d.]+).*integer:\s+(\d+)/i ) {
  $a{$1} = $2;
  }
  if( $line =~ /(?:\d+?\.){7}([\d.]+).*Hex-STRING:\s+?((?:[\d\w\s]{3}){5}[\d\w]{2})/i ) {
  $b{$1} = $2;
  }
}
for my $c (keys %a) {
  if (defined($b{$c}) ) {
  print "$a{$c}#$b{$c}\n";
  }
}
close F;


现在我想更多串联
SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.24.38.91 = Hex-STRING: 00 16 D3 18 26 5B
SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.92.133.39 = Hex-STRING: 00 16 D3 5C 85 27
SNMPv2-SMI::mib-2.17.4.3.1.1.0.38.158.50.22.230 = Hex-STRING: 00 26 9E 32 16 E6
SNMPv2-SMI::mib-2.17.4.3.1.2.0.22.211.92.133.39 = INTEGER: 14
SNMPv2-SMI::mib-2.17.4.3.1.2.0.38.158.50.22.230 = INTEGER: 4
IF-MIB::ifIndex.7 = INTEGER: 14
IF-MIB::ifIndex.6 = INTEGER: 4
IF-MIB::ifIndex.5 = INTEGER: 3
IF-MIB::ifName.8 = STRING: Fa0/23
IF-MIB::ifName.7 = STRING: Fa0/24
IF-MIB::ifName.6 = STRING: Gi0/25
IF-MIB::ifName.5 = STRING: Gi0/26

通过黑体部分串联,其中ifIndex.7等同于ifName.7,最终得到数据

Fa0/23#00 16 D3 5C 85 27
Fa0/24#00 26 9E 32 16 E6 

在线等


 


[解决办法]

Perl code
use strict;use warnings;my ($key,$val);my $item;my @ahex=("SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.24.38.91 = Hex-STRING: 00 16 D3 18 26 5B",          "SNMPv2-SMI::mib-2.17.4.3.1.1.0.22.211.92.133.39 = Hex-STRING: 00 16 D3 5C 85 27",          "SNMPv2-SMI::mib-2.17.4.3.1.1.0.38.158.50.22.230 = Hex-STRING: 00 26 9E 32 16 E6");#构造hex数据的哈希表my %hhex;foreach $item (@ahex){    #以等号拆分字符串    ($key,$val)=split(/=/,$item);    #去除左右空白符    $key=~s/^\s+|\s+$//g;    $val=~s/^\s+|\s+$//g;    my @array=split(/\./,$key);    $key=join(".",@array[7..$#array]);    $val=(split(/\:/,$val))[1];    $val=~s/^\s+//;    $hhex{$key}=$val;}#打印%hhex#foreach $item (keys(%hhex))#{#    print("$item --> $hhex{$item}\n");#}#以下构造哈希表过程略....my @asmi=("SNMPv2-SMI::mib-2.17.4.3.1.2.0.22.211.92.133.39 = INTEGER: 14",                    "SNMPv2-SMI::mib-2.17.4.3.1.2.0.38.158.50.22.230 = INTEGER: 4");my %hsmi=(14=>"22.211.92.133.39",4=>"38.158.50.22.230");my @aifi=("IF-MIB::ifIndex.7 = INTEGER: 14",          "IF-MIB::ifIndex.6 = INTEGER: 4",          "IF-MIB::ifIndex.5 = INTEGER: 3");my %hifi=(7=>14,6=>4,5=>3);my @aifn=("IF-MIB::ifName.8 = STRING: Fa0/23",          "IF-MIB::ifName.7 = STRING: Fa0/24",          "IF-MIB::ifName.6 = STRING: Gi0/25",          "IF-MIB::ifName.5 = STRING: Gi0/26");my %hifn=(8 =>"Fa0/23",7 =>"Fa0/24",6 =>"Gi0/25",5 =>"Gi0/26");#获得接口名与hex数据的对应关系foreach $item (keys(%hifi)){    if(exists($hifn{$item}) && exists($hsmi{$hifi{$item}}) && exists($hhex{$hsmi{$hifi{$item}}}))    {        print("$hifn{$item}#$hhex{$hsmi{$hifi{$item}}}\n");    }} 


[解决办法]

Perl code
open F, "<your_file";my (%a,%b,%c,%d);while (my $line = <F>) {  chomp($line);  if ($line =~ /(?:\d+?\.){7}([\d.]+).*integer:\s+(\d+)/i ) {     $a{$1} = $2;  }  if( $line =~ /(?:\d+?\.){7}([\d.]+).*Hex-STRING:\s+?((?:[\d\w\s]{3}){5}[\d\w]{2})/i ) {    $b{$1} = $2;  }  if ($line =~ /ifIndex\.(\d+).*INTEGER:\s+?(\d+)/i) {      $c{$2} = $1;  }  if ($line =~ /ifName\.(\d+).*STRING:\s+?(.*)/i) {      $d{$1} = $2;  }}for my $c (keys %a) {  if (defined($b{$c}) && defined( $c{$a{$c}} && defined($d{$c{$a{$c}}})) ) {     print "$d{$c{$a{$c}}}#$b{$c}\n";  }}close F; 

热点排行