我从来没有找到一个完美的解决方案。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,“The Trap-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 中使用它的位置之后,他们开始尝试最终做出改变。我正在对上面的代码进行测试,因为他们要求我提供一些我实际上需要在数据包中添加的东西,但是现在上面的代码仍然是未经严格测试的概念验证代码。希望有一天它可以帮助某人。