2009-07-14 90 views
7

几周前,我为我们的操作团队编写了一个SNMP中继器。他们有一些愚蠢的设备,只能将陷阱发送到单个IP,并且我们有一个监听系统在多个IP上监听可用性。该代码的死简单,主要有:如何解析Perl中的原始SNMP陷阱?

while (recv($packet)) { 
    foreach $target (@targets) { 
    send($target, $packet); 
    } 
} 

它的工作,基本上,但现在明显的短来,它不包括发端IP是包括信息的问题(显然,第一类设备varbind和一些新的类不)。

我想这样做是我的代码更改为类似这样:

while ($server->recv($packet)) { 
    my $obj = decompile($packet) 
    if (!$obj->{varbind}{snmpTrapAddress}) { 
    $obj->{varbind}{snmpTrapAddress} = inet_ntoa($server->peeraddr()); 
    } 
    $packet = compile($obj); 
    foreach $target (@targets) { 
    send($target, $packet); 
    } 
} 

换句话说,如果我的发件人不包括snmpTrapAddress,添加它。问题是,我为Perl查看的每个SNMP软件包似乎非常专注于接收陷阱和执行获取的基础架构。

所以:是否有一个简单的Perl模块可以让我说“这是一个代表snmp陷阱的数据块,将它解码成我可以轻松操作的东西,然后将其重新编译回blob,我可以抛出网络”?

如果你给出的答案是“使用SNMP虚拟”,你能提供这样的例子吗?我可能只是盲目的,但从perldoc SNMP的输出,我不明白如何以这种方式使用它。

编辑:

环视了一下说:“SNMP编码”是真的ASN.1 BER(基本编码规则)后的事实证明。基于此,我正在转换:: BER。我仍然欢迎SNMP的任何简单的细分/编辑/重建提示。

+0

我对“SNMP”一无所知,但是`Net-SNMP`有一个`Net :: SNMP :: Message`类。 – 2009-07-14 15:26:16

回答

8

我从来没有找到一个完美的解决方案。 Net :: SNMP :: Message(Net::SNMP的一部分)可能允许这样做,但似乎没有公开定义的接口,并且Net :: SNMP接口都不是特别相关的。 NSNMP与我所寻找的精神最为接近,但它很脆弱,并且不适合我开箱即用的数据包,如果我要支持脆弱的代码,它将成为我自己的脆弱代码。

Mon::SNMP也接近我正在寻找的东西,但它也被打破了框。它似乎被放弃了,2001年的最后一个版本和2002年的开发者最后一次CPAN发布。我当时没有意识到它,但现在我认为这是因为接口改变到了Convert :: BER它使用的模块。

Mon :: SNMP让我指向Convert::BER。几千次读取了Convert :: BER POD,Mon :: SNMP源和RFC 1157(特别是4.1.6,“陷阱-PDU”),我想出了这个代码作为概念证明我想要的。这只是概念证明(因为我会在代码后详细说明),所以它可能并不完美,但我认为它可能为将来在此领域工作的Perl人员提供有用的参考,因此它是:

#!/usr/bin/perl 

use Convert::BER; 
use Convert::BER qw(/^(\$|BER_)/); 

my $ber = Convert::BER->new(); 

# OID I want to add to the trap if not already present 
my $snmpTrapAddress = '1.3.6.1.6.3.18.1.3'; 

# this would be from the incoming socket in production 
my $source_ip = '10.137.54.253'; 

# convert the octets into chars to match SNMP standard for IPs 
my $source_ip_str = join('', map { chr($_); } split(/\./, $source_ip)); 

# Read the binary trap data from STDIN or ARGV. Normally this would 
# come from the UDP receiver 
my $d = join('', <>); 

# Stuff my trap data into $ber 
$ber->buffer($d); 

print STDERR "Original packet:\n"; 
$ber->dump(); 

# Just decode the first two fields so we can tell what version we're dealing with 
$ber->decode(
       SEQUENCE => [ 
        INTEGER => \$version, 
        STRING => \$community, 
        BER => \$rest_of_trap, 
       ], 
) || die "Couldn't decode packet: ".$ber->error()."\n"; 

if ($version == 0) { 
    #print STDERR "This is a version 1 trap, proceeding\n"; 

    # decode the PDU up to but not including the VARBINDS 
    $rest_of_trap->decode(
    [ SEQUENCE => BER_CONTEXT | BER_CONSTRUCTOR | 0x04 ] => 
     [ 
     OBJECT_ID => \$enterprise_oid, 
     [ STRING => BER_APPLICATION | 0x00 ] => \$agentaddr, 
     INTEGER => \$generic, 
     INTEGER => \$specific, 
     [ INTEGER => BER_APPLICATION | 0x03 ] => \$timeticks, 
     SEQUENCE => [ BER => \$varbind_ber, ], 
     ], 
) || die "Couldn't decode packet: ".$extra->error()."\n";; 

    # now decode the actual VARBINDS (just the OIDs really, to decode the values 
    # We'd have to go to the MIBs, which I neither want nor need to do 
    my($r, $t_oid, $t_val, %varbinds); 
    while ($r = $varbind_ber->decode(
    SEQUENCE => [ 
     OBJECT_ID => \$t_oid, 
     ANY  => \$t_val, 
    ],)) 
    { 
    if (!$r) { 
     die "Couldn't decode SEQUENCE: ".$extra->error()."\n"; 
    } 
    $varbinds{$t_oid} = $t_val; 
    } 

    if ($varbinds{$snmpTrapAddress} || $varbinds{"$snmpTrapAddress.0"}) { 
    # the original trap already had the data, just print it back out 
    print $d; 
    } else { 
    # snmpTrapAddress isn't present, create a new object and rebuild the packet 
    my $new_trap = new Convert::BER; 
    $new_trap->encode(
     SEQUENCE => [ 
     INTEGER => $version, 
     STRING => $community, 
     [ SEQUENCE => BER_CONTEXT | BER_CONSTRUCTOR | 0x04 ] => 
      [ 
      OBJECT_ID => $enterprise_oid, 
      [ STRING => BER_APPLICATION | 0x00 ] => $agentaddr, 
      INTEGER => $generic, 
      INTEGER => $specific, 
      [ INTEGER => BER_APPLICATION | 0x03 ] => $timeticks, 
      SEQUENCE => [ 
       BER => $varbind_ber, 
       # this next oid/val is the only mod we should be making 
       SEQUENCE => [ 
       OBJECT_ID => "$snmpTrapAddress.0", 
       [ STRING => BER_APPLICATION | 0x00 ] => $source_ip_str, 
       ], 
      ], 
      ], 
     ], 
    ); 
    print STDERR "New packet:\n"; 
    $new_trap->dump(); 
    print $new_trap->buffer; 
    } 
} else { 
    print STDERR "I don't know how to decode non-v1 packets yet\n"; 
    # send back the original packet 
    print $d; 
} 

所以,就是这样。这是踢球者。我听到他们的话说他们没有得到陷阱中原始发件人的IP。在研究这个例子时,我发现,至少在他们给我的例子中,原始IP位于陷阱中的agent-addr字段中。在向他们展示这些信息并在工具的API中显示他们使用的这些信息后,他们就试图在他们的结尾进行更改。上面的代码是在他们问我实际需要在数据包中使用的东西的时候给出的,但现在上面的代码仍然没有经过严格测试的概念代码证明。希望有一天能帮助别人。

2

你试过NSNMP

+0

啊!我以为我有,但它看起来像我真正偶然发现的是NSNMP :: Simple,这不是我想要的。 NSNMP看起来像是一个可能的解决方案。戳一下吧... – jj33 2009-07-15 13:10:27

2

绝对检出SNMP_Session。

http://code.google.com/p/snmp-session/

请务必遵循的链接,其中有几个例子旧的发行站点。

我基本上走过了Mon :: SNMP,Convert :: BER,TCP/IP Illustrated等等的相同路径。SNMP_Session是我唯一能够工作的东西。通过工作,我的意思是接受UDP端口162上的SNMP陷阱,并将其解码为字符串等效记录,而不用重新发明几个轮子。我只使用接收陷阱功能,但我认为它可以做你想要的。

这是谷歌代码,但不是CPAN,所以它有点难找。