这是几个星期前,我写了我们的OPS组的SNMP relayer。他们只能陷阱发送到一个单一的IP一些愚蠢的设备,我们有多个IP地址的可用性监听监控系统。代码的死简单,并且实质上:

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

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

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

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,添加它。问题是,每一个SNMP包我看过对Perl似乎很主要侧重于接收陷阱和执行获得的基础设施。

所以:有一个简单的Perl模块,让我说“这里是表示SNMP陷阱数据的BLOB其解码到的东西,我可以很容易地操作,然后重新编译它放回一个blob我可以扔在网络上。 “?

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

编辑:

环视了一下说:“SNMP编码”是真的ASN.1 BER(基本编码规则)后的事实证明。在此基础上我在与转换一展身手:: BER。我仍然会欢迎任何容易击穿/编辑/重建提示SNMP。

有帮助吗?

解决方案

我从来没有发现一个完美地解决了这个。净:: SNMP ::消息(净:: SNMP )可能会允许这一点,但似乎并没有有公开定义的接口,并没有任何的Net :: SNMP接口似乎特别相关。 NSNMP 是最接近的东西我一直在寻找的精神,但它的脆性和我的包没开箱的,如果我要去支持脆弱的代码,这将是我自己脆弱的代码=)。

周一:: SNMP也接近了什么我一直在寻找,但它也被打破了开箱即用。这似乎被抛弃,在2001年的最后一个版本和开发商的最后CPAN发行于2002年。我并没有在当时意识到这一点,但我现在认为,这是因为在接口转换的改变破碎:: BER模块,它使用。

周一:: SNMP让我向指出转换:: BER 。几千读取转换的BER :: POD,孟:: 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是在陷阱代理地址字段。显示该工具的API在他们这个并在之后他们使用这种暴露他们去了,试图把他们的最终变化。我daving上述对他们问我的东西,我真正需要的数据包的渣土时的代码,但现在上述仍将概念代码非严格的测试证明。希望它可以帮助别人一天。

scroll top