Hello Wise and Generous Monks,
I'm trying to write a Perl script that uses Net::SNMP to query RFC 2674 enabled Ethernet switches. One of the "Textual Conventions" defined in the MIB is a "PortList" that the MIB defines as:
PortList ::= TEXTUAL-CONVENTION STATUS current DESCRIPTION "Each octet within this value specifies a set of eight ports, with the first octet specifying ports 1 through 8, the second octet specifying ports 9 through 16, etc. Within each octet, the most significant bit represents the lowest numbered port, and the least significant bit represents the highest numbered port. Thus, each port of the bridge is represented by a single bit within the value of this object. If that bit has a value of '1' then that port is included in the set of ports; the port is not included if its bit has a value of '0'." SYNTAX OCTET STRING
What I would like to do is convert the hex string to an Perl array where the variable is either a one or a zero depending on the hex string. What I've written so far is:
#!/usr/bin/perl -w
use strict;
use diagnostics;
use Data::Dumper;
use Net::SNMP;
use Carp;
my $debug = 0;
my $ports = "1.3.6.1.2.1.17.7.1.4.2.1.5.0.1";
my ($session, $error) = Net::SNMP->session( version=> 2,
-hostname => "switch",
-community=> "public");
if (!$session) {
croak("Net::SNMP Session not created\n");
}
my $result = $session->get_request(-varbindlist => $ports]);
+
if (!$result) {
my $err = $session->error();
print "ERROR: $err \n";
croak("no results from get_bulk_result");
}
$session->close();
my $portstr = $result->{$ports};
print "The port string is: $portstr\n";
[download]
Which produces the following output: 0xfffbff0000000000000000
I know I need to use unpack to make the conversion, but I'm not sure how to go about it.
Thanks in advance for your generous help.
Notdeadyet
The ASN.1 OCTET STRING data type is encoded into a TLV triplet that begins with a Tag byte of 0x04. The OCTET STRING and BIT STRING data types are very similar. Thus, the two types are encoded in a similar manner except that, because the trailing byte of an OCTET STRING cannot have unused bits, no leading bytes must be added to the content. The following example, adapted from the CMC Encoded ASN.1 topic, shows how the name of a certificate template is encoded as a byte array.
复制30 17 ; SEQUENCE (17 Bytes)
| 06 09 ; OBJECT_ID (9 Bytes)
| | 2b 06 01 04 01 82 37 14 02 ; 1.3.6.1.4.1.311.20.2
| 04 0a ; OCTET_STRING (a Bytes)
| 1e 08 00 55 00 73 00 65 00 72 ; ...U.s.e.r
If the byte array contains fewer than 128 bytes, the Length field of the TLV triplet requires only one byte to specify the content length. If it is more than 127 bytes, bit 7 of the Length field is set to 1 and bits 6 through 0 specify the number of additional bytes used to identify the content length. This is shown in the following example where the high order bit of the second byte on the first line is set to 1 and the byte indicates that there is a trailing Length byte. The third byte therefore specifies that the content is 0x80 bytes long.
复制04 81 80 ; OCTET_STRING (80 Bytes)
38 10 60 e2 70 69 91 4a ; 8.`.pi.J
8b b5 22 57 2a 62 ef de ; .."W*b..
15 7d 59 d6 4e 20 9a 45 ; .}Y.N .E
2b e3 fd fc 68 ba af bf ; +...h...
9c 17 b0 8e 6d c4 29 1e ; ....m.).
e3 21 ac bb 5a 8a c9 67 ; .!..Z..g
0a d4 45 93 10 c0 26 eb ; ..E...&.
0a 83 c2 b1 40 87 36 f7 ; ....@.6.
a0 26 da b9 bb 46 73 88 ; .&...Fs.
7a 67 b9 e6 b3 6f ea 59 ; zg...o.Y
28 8a d3 92 72 f6 7b 89 ; (...r.{.
a0 d8 2d 9e 40 eb 1e bb ; ..-.@...
6e ae f0 5a ed 16 c9 e3 ; n..Z....
27 59 37 8f f3 4a 98 60 ; 'Y7..J.`
f8 fb a7 0a ee 1b 6e 91 ; ......n.
95 96 cf 0d 56 ac ab 35 ; ....V..5
This information is polled via SNMP.
Do an SNMP walk against the device using the ifTable OID, to see if the interface is reported there.
I'm trying to write a Perl script that uses Net::SNMP to query RFC 2674 enabled Ethernet switches. One of the "Textual Conventions" defined in the MIB is a "PortList" that the MIB defines as:
PortList ::= TEXTUAL-CONVENTION STATUS current DESCRIPTION "Each octet within this value specifies a set of eight ports, with the first octet specifying ports 1 through 8, the second octet specifying ports 9 through 16, etc. Within each octet, the most significant bit represents the lowest numbered port, and the least significant bit represents the highest numbered port. Thus, each port of the bridge is represented by a single bit within the value of this object. If that bit has a value of '1' then that port is included in the set of ports; the port is not included if its bit has a value of '0'." SYNTAX OCTET STRING
What I would like to do is convert the hex string to an Perl array where the variable is either a one or a zero depending on the hex string. What I've written so far is:
#!/usr/bin/perl -w
use strict;
use diagnostics;
use Data::Dumper;
use Net::SNMP;
use Carp;
my $debug = 0;
my $ports = "1.3.6.1.2.1.17.7.1.4.2.1.5.0.1";
my ($session, $error) = Net::SNMP->session( version=> 2,
-hostname => "switch",
-community=> "public");
if (!$session) {
croak("Net::SNMP Session not created\n");
}
my $result = $session->get_request(-varbindlist => $ports]);
+
if (!$result) {
my $err = $session->error();
print "ERROR: $err \n";
croak("no results from get_bulk_result");
}
$session->close();
my $portstr = $result->{$ports};
print "The port string is: $portstr\n";
[download]
Which produces the following output: 0xfffbff0000000000000000
I know I need to use unpack to make the conversion, but I'm not sure how to go about it.
Thanks in advance for your generous help.
Notdeadyet
The ASN.1 OCTET STRING data type is encoded into a TLV triplet that begins with a Tag byte of 0x04. The OCTET STRING and BIT STRING data types are very similar. Thus, the two types are encoded in a similar manner except that, because the trailing byte of an OCTET STRING cannot have unused bits, no leading bytes must be added to the content. The following example, adapted from the CMC Encoded ASN.1 topic, shows how the name of a certificate template is encoded as a byte array.
复制30 17 ; SEQUENCE (17 Bytes)
| 06 09 ; OBJECT_ID (9 Bytes)
| | 2b 06 01 04 01 82 37 14 02 ; 1.3.6.1.4.1.311.20.2
| 04 0a ; OCTET_STRING (a Bytes)
| 1e 08 00 55 00 73 00 65 00 72 ; ...U.s.e.r
If the byte array contains fewer than 128 bytes, the Length field of the TLV triplet requires only one byte to specify the content length. If it is more than 127 bytes, bit 7 of the Length field is set to 1 and bits 6 through 0 specify the number of additional bytes used to identify the content length. This is shown in the following example where the high order bit of the second byte on the first line is set to 1 and the byte indicates that there is a trailing Length byte. The third byte therefore specifies that the content is 0x80 bytes long.
复制04 81 80 ; OCTET_STRING (80 Bytes)
38 10 60 e2 70 69 91 4a ; 8.`.pi.J
8b b5 22 57 2a 62 ef de ; .."W*b..
15 7d 59 d6 4e 20 9a 45 ; .}Y.N .E
2b e3 fd fc 68 ba af bf ; +...h...
9c 17 b0 8e 6d c4 29 1e ; ....m.).
e3 21 ac bb 5a 8a c9 67 ; .!..Z..g
0a d4 45 93 10 c0 26 eb ; ..E...&.
0a 83 c2 b1 40 87 36 f7 ; ....@.6.
a0 26 da b9 bb 46 73 88 ; .&...Fs.
7a 67 b9 e6 b3 6f ea 59 ; zg...o.Y
28 8a d3 92 72 f6 7b 89 ; (...r.{.
a0 d8 2d 9e 40 eb 1e bb ; ..-.@...
6e ae f0 5a ed 16 c9 e3 ; n..Z....
27 59 37 8f f3 4a 98 60 ; 'Y7..J.`
f8 fb a7 0a ee 1b 6e 91 ; ......n.
95 96 cf 0d 56 ac ab 35 ; ....V..5
This information is polled via SNMP.
Do an SNMP walk against the device using the ifTable OID, to see if the interface is reported there.