我从来没有找到一个完美的解决方案。 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中显示他们使用的这些信息后,他们就试图在他们的结尾进行更改。上面的代码是在他们问我实际需要在数据包中使用的东西的时候给出的,但现在上面的代码仍然没有经过严格测试的概念代码证明。希望有一天能帮助别人。
我对“SNMP”一无所知,但是`Net-SNMP`有一个`Net :: SNMP :: Message`类。 – 2009-07-14 15:26:16