SNMP_util.pm untested

filter: Statementstmt 0.0% 0 / 626 Branchbran 0.0% 0 / 358 Conditioncond 0.0% 0 / 84 Subroutinesub 0.0% 0 / 47 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 1154 CC 180 SCAR 64.6
CC
180
Cov
0%
snmpset
1332.0
snmpopen
1122.0
snmpwalk_flg
1122.0
CRAP 640.9
1 ### - *- mode: Perl -*-
2 ######################################################################
3 ### SNMP_util -- SNMP utilities using SNMP_Session.pm and BER.pm
4 ######################################################################
5 ### Copyright (c) 1998-2010, Mike Mitchell.
6 ###
7 ### This program is free software; you can redistribute it under the
8 ### "Artistic License 2.0" included in this distribution
9 ### (file "Artistic").
10 ######################################################################
11 ### Created by: Mike Mitchell <Mike.Mitchell@sas.com>
12 ###
13 ### Contributions and fixes by:
14 ###
15 ### Tobias Oetiker <tobi@oetiker.ch>: Basic layout
16 ### Simon Leinen <simon@switch.ch>: SNMP_session.pm/BER.pm
17 ### Jeff Allen <jeff.allen@acm.org>: length() of undefined value
18 ### Johannes Demel <demel@zid.tuwien.ac.at>: MIB file parse problem
19 ### Simon Leinen <simon@switch.ch>: more OIDs from Interface MIB
20 ### Jacques Supcik <supcik@ip-plus.net>: Specify local IP, port
21 ### Tobias Oetiker <tobi@oetiker.ch>: HASH as first OID to set SNMP options
22 ### Simon Leinen <simon@switch.ch>: 'undefined port' bug
23 ### Daniel McDonald <dmcdonald@digicontech.com>: request for getbulk support
24 ### Laurent Girod <it.fb@net2000.ch>: code for snmpwalkhash
25 ### Ian Duplisse <i.duplisse@cablelabs.com>: MIB parsing suggestions
26 ### Jakob Ilves <jakob.ilves@oracle.com>: return_array_refs for snmpwalk()
27 ### Valerio Bontempi <v.bontempi@inwind.it>: IPv6 support
28 ### Lorenzo Colitti <lorenzo@colitti.com>: IPv6 support
29 ### Joerg Kummer <JOERG.KUMMER@Roche.COM>: TimeTicks support in snmpset()
30 ### Christopher J. Tengi <tengi@CS.Princeton.EDU>: Gauge32 support in snmpset()
31 ### Nicolai Petri <nicolai@catpipe.net>: hashref passing for snmpwalkhash()
32 ### <jaccobs@online.nl>: parse NOTIFICATION-TYPE in MIB
33 ### Dan Thorson <Dan.Thorson@seagate.com>: handle quotes in MIB comments better
34 ######################################################################
35  
36 package SNMP_util;
37  
38 require 5.004;
39  
40 use strict;
41 use vars qw(@ISA @EXPORT $VERSION);
42 use Exporter;
43 use Carp;
44  
45 use BER "1.02";
46 use SNMP_Session "1.00";
47 use Socket;
48  
49 $VERSION = '1.15';
50  
51 @ISA = qw(Exporter);
52  
53 @EXPORT = qw(snmpget snmpgetnext snmpwalk snmpset snmptrap snmpgetbulk snmpmaptable snmpmaptable4 snmpwalkhash snmpmapOID snmpMIB_to_OID snmpLoad_OID_Cache snmpQueue_MIB_File);
54  
55 # The OID numbers from RFC1213 (MIB-II) and RFC1315 (Frame Relay)
56 # are pre-loaded below.
57 %SNMP_util::OIDS =
58 (
59 'iso' => '1',
60 'org' => '1.3',
61 'dod' => '1.3.6',
62 'internet' => '1.3.6.1',
63 'directory' => '1.3.6.1.1',
64 'mgmt' => '1.3.6.1.2',
65 'mib-2' => '1.3.6.1.2.1',
66 'system' => '1.3.6.1.2.1.1',
67 'sysDescr' => '1.3.6.1.2.1.1.1.0',
68 'sysObjectID' => '1.3.6.1.2.1.1.2.0',
69 'sysUpTime' => '1.3.6.1.2.1.1.3.0',
70 'sysUptime' => '1.3.6.1.2.1.1.3.0',
71 'sysContact' => '1.3.6.1.2.1.1.4.0',
72 'sysName' => '1.3.6.1.2.1.1.5.0',
73 'sysLocation' => '1.3.6.1.2.1.1.6.0',
74 'sysServices' => '1.3.6.1.2.1.1.7.0',
75 'interfaces' => '1.3.6.1.2.1.2',
76 'ifNumber' => '1.3.6.1.2.1.2.1.0',
77 'ifTable' => '1.3.6.1.2.1.2.2',
78 'ifEntry' => '1.3.6.1.2.1.2.2.1',
79 'ifIndex' => '1.3.6.1.2.1.2.2.1.1',
80 'ifInOctets' => '1.3.6.1.2.1.2.2.1.10',
81 'ifInUcastPkts' => '1.3.6.1.2.1.2.2.1.11',
82 'ifInNUcastPkts' => '1.3.6.1.2.1.2.2.1.12',
83 'ifInDiscards' => '1.3.6.1.2.1.2.2.1.13',
84 'ifInErrors' => '1.3.6.1.2.1.2.2.1.14',
85 'ifInUnknownProtos' => '1.3.6.1.2.1.2.2.1.15',
86 'ifOutOctets' => '1.3.6.1.2.1.2.2.1.16',
87 'ifOutUcastPkts' => '1.3.6.1.2.1.2.2.1.17',
88 'ifOutNUcastPkts' => '1.3.6.1.2.1.2.2.1.18',
89 'ifOutDiscards' => '1.3.6.1.2.1.2.2.1.19',
90 'ifDescr' => '1.3.6.1.2.1.2.2.1.2',
91 'ifOutErrors' => '1.3.6.1.2.1.2.2.1.20',
92 'ifOutQLen' => '1.3.6.1.2.1.2.2.1.21',
93 'ifSpecific' => '1.3.6.1.2.1.2.2.1.22',
94 'ifType' => '1.3.6.1.2.1.2.2.1.3',
95 'ifMtu' => '1.3.6.1.2.1.2.2.1.4',
96 'ifSpeed' => '1.3.6.1.2.1.2.2.1.5',
97 'ifPhysAddress' => '1.3.6.1.2.1.2.2.1.6',
98 'ifAdminHack' => '1.3.6.1.2.1.2.2.1.7',
99 'ifAdminStatus' => '1.3.6.1.2.1.2.2.1.7',
100 'ifOperHack' => '1.3.6.1.2.1.2.2.1.8',
101 'ifOperStatus' => '1.3.6.1.2.1.2.2.1.8',
102 'ifLastChange' => '1.3.6.1.2.1.2.2.1.9',
103 'at' => '1.3.6.1.2.1.3',
104 'atTable' => '1.3.6.1.2.1.3.1',
105 'atEntry' => '1.3.6.1.2.1.3.1.1',
106 'atIfIndex' => '1.3.6.1.2.1.3.1.1.1',
107 'atPhysAddress' => '1.3.6.1.2.1.3.1.1.2',
108 'atNetAddress' => '1.3.6.1.2.1.3.1.1.3',
109 'ip' => '1.3.6.1.2.1.4',
110 'ipForwarding' => '1.3.6.1.2.1.4.1',
111 'ipOutRequests' => '1.3.6.1.2.1.4.10',
112 'ipOutDiscards' => '1.3.6.1.2.1.4.11',
113 'ipOutNoRoutes' => '1.3.6.1.2.1.4.12',
114 'ipReasmTimeout' => '1.3.6.1.2.1.4.13',
115 'ipReasmReqds' => '1.3.6.1.2.1.4.14',
116 'ipReasmOKs' => '1.3.6.1.2.1.4.15',
117 'ipReasmFails' => '1.3.6.1.2.1.4.16',
118 'ipFragOKs' => '1.3.6.1.2.1.4.17',
119 'ipFragFails' => '1.3.6.1.2.1.4.18',
120 'ipFragCreates' => '1.3.6.1.2.1.4.19',
121 'ipDefaultTTL' => '1.3.6.1.2.1.4.2',
122 'ipAddrTable' => '1.3.6.1.2.1.4.20',
123 'ipAddrEntry' => '1.3.6.1.2.1.4.20.1',
124 'ipAdEntAddr' => '1.3.6.1.2.1.4.20.1.1',
125 'ipAdEntIfIndex' => '1.3.6.1.2.1.4.20.1.2',
126 'ipAdEntNetMask' => '1.3.6.1.2.1.4.20.1.3',
127 'ipAdEntBcastAddr' => '1.3.6.1.2.1.4.20.1.4',
128 'ipAdEntReasmMaxSize' => '1.3.6.1.2.1.4.20.1.5',
129 'ipRouteTable' => '1.3.6.1.2.1.4.21',
130 'ipRouteEntry' => '1.3.6.1.2.1.4.21.1',
131 'ipRouteDest' => '1.3.6.1.2.1.4.21.1.1',
132 'ipRouteAge' => '1.3.6.1.2.1.4.21.1.10',
133 'ipRouteMask' => '1.3.6.1.2.1.4.21.1.11',
134 'ipRouteMetric5' => '1.3.6.1.2.1.4.21.1.12',
135 'ipRouteInfo' => '1.3.6.1.2.1.4.21.1.13',
136 'ipRouteIfIndex' => '1.3.6.1.2.1.4.21.1.2',
137 'ipRouteMetric1' => '1.3.6.1.2.1.4.21.1.3',
138 'ipRouteMetric2' => '1.3.6.1.2.1.4.21.1.4',
139 'ipRouteMetric3' => '1.3.6.1.2.1.4.21.1.5',
140 'ipRouteMetric4' => '1.3.6.1.2.1.4.21.1.6',
141 'ipRouteNextHop' => '1.3.6.1.2.1.4.21.1.7',
142 'ipRouteType' => '1.3.6.1.2.1.4.21.1.8',
143 'ipRouteProto' => '1.3.6.1.2.1.4.21.1.9',
144 'ipNetToMediaTable' => '1.3.6.1.2.1.4.22',
145 'ipNetToMediaEntry' => '1.3.6.1.2.1.4.22.1',
146 'ipNetToMediaIfIndex' => '1.3.6.1.2.1.4.22.1.1',
147 'ipNetToMediaPhysAddress' => '1.3.6.1.2.1.4.22.1.2',
148 'ipNetToMediaNetAddress' => '1.3.6.1.2.1.4.22.1.3',
149 'ipNetToMediaType' => '1.3.6.1.2.1.4.22.1.4',
150 'ipRoutingDiscards' => '1.3.6.1.2.1.4.23',
151 'ipInReceives' => '1.3.6.1.2.1.4.3',
152 'ipInHdrErrors' => '1.3.6.1.2.1.4.4',
153 'ipInAddrErrors' => '1.3.6.1.2.1.4.5',
154 'ipForwDatagrams' => '1.3.6.1.2.1.4.6',
155 'ipInUnknownProtos' => '1.3.6.1.2.1.4.7',
156 'ipInDiscards' => '1.3.6.1.2.1.4.8',
157 'ipInDelivers' => '1.3.6.1.2.1.4.9',
158 'icmp' => '1.3.6.1.2.1.5',
159 'icmpInMsgs' => '1.3.6.1.2.1.5.1',
160 'icmpInTimestamps' => '1.3.6.1.2.1.5.10',
161 'icmpInTimestampReps' => '1.3.6.1.2.1.5.11',
162 'icmpInAddrMasks' => '1.3.6.1.2.1.5.12',
163 'icmpInAddrMaskReps' => '1.3.6.1.2.1.5.13',
164 'icmpOutMsgs' => '1.3.6.1.2.1.5.14',
165 'icmpOutErrors' => '1.3.6.1.2.1.5.15',
166 'icmpOutDestUnreachs' => '1.3.6.1.2.1.5.16',
167 'icmpOutTimeExcds' => '1.3.6.1.2.1.5.17',
168 'icmpOutParmProbs' => '1.3.6.1.2.1.5.18',
169 'icmpOutSrcQuenchs' => '1.3.6.1.2.1.5.19',
170 'icmpInErrors' => '1.3.6.1.2.1.5.2',
171 'icmpOutRedirects' => '1.3.6.1.2.1.5.20',
172 'icmpOutEchos' => '1.3.6.1.2.1.5.21',
173 'icmpOutEchoReps' => '1.3.6.1.2.1.5.22',
174 'icmpOutTimestamps' => '1.3.6.1.2.1.5.23',
175 'icmpOutTimestampReps' => '1.3.6.1.2.1.5.24',
176 'icmpOutAddrMasks' => '1.3.6.1.2.1.5.25',
177 'icmpOutAddrMaskReps' => '1.3.6.1.2.1.5.26',
178 'icmpInDestUnreachs' => '1.3.6.1.2.1.5.3',
179 'icmpInTimeExcds' => '1.3.6.1.2.1.5.4',
180 'icmpInParmProbs' => '1.3.6.1.2.1.5.5',
181 'icmpInSrcQuenchs' => '1.3.6.1.2.1.5.6',
182 'icmpInRedirects' => '1.3.6.1.2.1.5.7',
183 'icmpInEchos' => '1.3.6.1.2.1.5.8',
184 'icmpInEchoReps' => '1.3.6.1.2.1.5.9',
185 'tcp' => '1.3.6.1.2.1.6',
186 'tcpRtoAlgorithm' => '1.3.6.1.2.1.6.1',
187 'tcpInSegs' => '1.3.6.1.2.1.6.10',
188 'tcpOutSegs' => '1.3.6.1.2.1.6.11',
189 'tcpRetransSegs' => '1.3.6.1.2.1.6.12',
190 'tcpConnTable' => '1.3.6.1.2.1.6.13',
191 'tcpConnEntry' => '1.3.6.1.2.1.6.13.1',
192 'tcpConnState' => '1.3.6.1.2.1.6.13.1.1',
193 'tcpConnLocalAddress' => '1.3.6.1.2.1.6.13.1.2',
194 'tcpConnLocalPort' => '1.3.6.1.2.1.6.13.1.3',
195 'tcpConnRemAddress' => '1.3.6.1.2.1.6.13.1.4',
196 'tcpConnRemPort' => '1.3.6.1.2.1.6.13.1.5',
197 'tcpInErrs' => '1.3.6.1.2.1.6.14',
198 'tcpOutRsts' => '1.3.6.1.2.1.6.15',
199 'tcpRtoMin' => '1.3.6.1.2.1.6.2',
200 'tcpRtoMax' => '1.3.6.1.2.1.6.3',
201 'tcpMaxConn' => '1.3.6.1.2.1.6.4',
202 'tcpActiveOpens' => '1.3.6.1.2.1.6.5',
203 'tcpPassiveOpens' => '1.3.6.1.2.1.6.6',
204 'tcpAttemptFails' => '1.3.6.1.2.1.6.7',
205 'tcpEstabResets' => '1.3.6.1.2.1.6.8',
206 'tcpCurrEstab' => '1.3.6.1.2.1.6.9',
207 'udp' => '1.3.6.1.2.1.7',
208 'udpInDatagrams' => '1.3.6.1.2.1.7.1',
209 'udpNoPorts' => '1.3.6.1.2.1.7.2',
210 'udpInErrors' => '1.3.6.1.2.1.7.3',
211 'udpOutDatagrams' => '1.3.6.1.2.1.7.4',
212 'udpTable' => '1.3.6.1.2.1.7.5',
213 'udpEntry' => '1.3.6.1.2.1.7.5.1',
214 'udpLocalAddress' => '1.3.6.1.2.1.7.5.1.1',
215 'udpLocalPort' => '1.3.6.1.2.1.7.5.1.2',
216 'egp' => '1.3.6.1.2.1.8',
217 'egpInMsgs' => '1.3.6.1.2.1.8.1',
218 'egpInErrors' => '1.3.6.1.2.1.8.2',
219 'egpOutMsgs' => '1.3.6.1.2.1.8.3',
220 'egpOutErrors' => '1.3.6.1.2.1.8.4',
221 'egpNeighTable' => '1.3.6.1.2.1.8.5',
222 'egpNeighEntry' => '1.3.6.1.2.1.8.5.1',
223 'egpNeighState' => '1.3.6.1.2.1.8.5.1.1',
224 'egpNeighStateUps' => '1.3.6.1.2.1.8.5.1.10',
225 'egpNeighStateDowns' => '1.3.6.1.2.1.8.5.1.11',
226 'egpNeighIntervalHello' => '1.3.6.1.2.1.8.5.1.12',
227 'egpNeighIntervalPoll' => '1.3.6.1.2.1.8.5.1.13',
228 'egpNeighMode' => '1.3.6.1.2.1.8.5.1.14',
229 'egpNeighEventTrigger' => '1.3.6.1.2.1.8.5.1.15',
230 'egpNeighAddr' => '1.3.6.1.2.1.8.5.1.2',
231 'egpNeighAs' => '1.3.6.1.2.1.8.5.1.3',
232 'egpNeighInMsgs' => '1.3.6.1.2.1.8.5.1.4',
233 'egpNeighInErrs' => '1.3.6.1.2.1.8.5.1.5',
234 'egpNeighOutMsgs' => '1.3.6.1.2.1.8.5.1.6',
235 'egpNeighOutErrs' => '1.3.6.1.2.1.8.5.1.7',
236 'egpNeighInErrMsgs' => '1.3.6.1.2.1.8.5.1.8',
237 'egpNeighOutErrMsgs' => '1.3.6.1.2.1.8.5.1.9',
238 'egpAs' => '1.3.6.1.2.1.8.6',
239 'transmission' => '1.3.6.1.2.1.10',
240 'frame-relay' => '1.3.6.1.2.1.10.32',
241 'frDlcmiTable' => '1.3.6.1.2.1.10.32.1',
242 'frDlcmiEntry' => '1.3.6.1.2.1.10.32.1.1',
243 'frDlcmiIfIndex' => '1.3.6.1.2.1.10.32.1.1.1',
244 'frDlcmiState' => '1.3.6.1.2.1.10.32.1.1.2',
245 'frDlcmiAddress' => '1.3.6.1.2.1.10.32.1.1.3',
246 'frDlcmiAddressLen' => '1.3.6.1.2.1.10.32.1.1.4',
247 'frDlcmiPollingInterval' => '1.3.6.1.2.1.10.32.1.1.5',
248 'frDlcmiFullEnquiryInterval' => '1.3.6.1.2.1.10.32.1.1.6',
249 'frDlcmiErrorThreshold' => '1.3.6.1.2.1.10.32.1.1.7',
250 'frDlcmiMonitoredEvents' => '1.3.6.1.2.1.10.32.1.1.8',
251 'frDlcmiMaxSupportedVCs' => '1.3.6.1.2.1.10.32.1.1.9',
252 'frDlcmiMulticast' => '1.3.6.1.2.1.10.32.1.1.10',
253 'frCircuitTable' => '1.3.6.1.2.1.10.32.2',
254 'frCircuitEntry' => '1.3.6.1.2.1.10.32.2.1',
255 'frCircuitIfIndex' => '1.3.6.1.2.1.10.32.2.1.1',
256 'frCircuitDlci' => '1.3.6.1.2.1.10.32.2.1.2',
257 'frCircuitState' => '1.3.6.1.2.1.10.32.2.1.3',
258 'frCircuitReceivedFECNs' => '1.3.6.1.2.1.10.32.2.1.4',
259 'frCircuitReceivedBECNs' => '1.3.6.1.2.1.10.32.2.1.5',
260 'frCircuitSentFrames' => '1.3.6.1.2.1.10.32.2.1.6',
261 'frCircuitSentOctets' => '1.3.6.1.2.1.10.32.2.1.7',
262 'frOutOctets' => '1.3.6.1.2.1.10.32.2.1.7',
263 'frCircuitReceivedFrames' => '1.3.6.1.2.1.10.32.2.1.8',
264 'frCircuitReceivedOctets' => '1.3.6.1.2.1.10.32.2.1.9',
265 'frInOctets' => '1.3.6.1.2.1.10.32.2.1.9',
266 'frCircuitCreationTime' => '1.3.6.1.2.1.10.32.2.1.10',
267 'frCircuitLastTimeChange' => '1.3.6.1.2.1.10.32.2.1.11',
268 'frCircuitCommittedBurst' => '1.3.6.1.2.1.10.32.2.1.12',
269 'frCircuitExcessBurst' => '1.3.6.1.2.1.10.32.2.1.13',
270 'frCircuitThroughput' => '1.3.6.1.2.1.10.32.2.1.14',
271 'frErrTable' => '1.3.6.1.2.1.10.32.3',
272 'frErrEntry' => '1.3.6.1.2.1.10.32.3.1',
273 'frErrIfIndex' => '1.3.6.1.2.1.10.32.3.1.1',
274 'frErrType' => '1.3.6.1.2.1.10.32.3.1.2',
275 'frErrData' => '1.3.6.1.2.1.10.32.3.1.3',
276 'frErrTime' => '1.3.6.1.2.1.10.32.3.1.4',
277 'frame-relay-globals' => '1.3.6.1.2.1.10.32.4',
278 'frTrapState' => '1.3.6.1.2.1.10.32.4.1',
279 'snmp' => '1.3.6.1.2.1.11',
280 'snmpInPkts' => '1.3.6.1.2.1.11.1',
281 'snmpInBadValues' => '1.3.6.1.2.1.11.10',
282 'snmpInReadOnlys' => '1.3.6.1.2.1.11.11',
283 'snmpInGenErrs' => '1.3.6.1.2.1.11.12',
284 'snmpInTotalReqVars' => '1.3.6.1.2.1.11.13',
285 'snmpInTotalSetVars' => '1.3.6.1.2.1.11.14',
286 'snmpInGetRequests' => '1.3.6.1.2.1.11.15',
287 'snmpInGetNexts' => '1.3.6.1.2.1.11.16',
288 'snmpInSetRequests' => '1.3.6.1.2.1.11.17',
289 'snmpInGetResponses' => '1.3.6.1.2.1.11.18',
290 'snmpInTraps' => '1.3.6.1.2.1.11.19',
291 'snmpOutPkts' => '1.3.6.1.2.1.11.2',
292 'snmpOutTooBigs' => '1.3.6.1.2.1.11.20',
293 'snmpOutNoSuchNames' => '1.3.6.1.2.1.11.21',
294 'snmpOutBadValues' => '1.3.6.1.2.1.11.22',
295 'snmpOutGenErrs' => '1.3.6.1.2.1.11.24',
296 'snmpOutGetRequests' => '1.3.6.1.2.1.11.25',
297 'snmpOutGetNexts' => '1.3.6.1.2.1.11.26',
298 'snmpOutSetRequests' => '1.3.6.1.2.1.11.27',
299 'snmpOutGetResponses' => '1.3.6.1.2.1.11.28',
300 'snmpOutTraps' => '1.3.6.1.2.1.11.29',
301 'snmpInBadVersions' => '1.3.6.1.2.1.11.3',
302 'snmpEnableAuthenTraps' => '1.3.6.1.2.1.11.30',
303 'snmpInBadCommunityNames' => '1.3.6.1.2.1.11.4',
304 'snmpInBadCommunityUses' => '1.3.6.1.2.1.11.5',
305 'snmpInASNParseErrs' => '1.3.6.1.2.1.11.6',
306 'snmpInTooBigs' => '1.3.6.1.2.1.11.8',
307 'snmpInNoSuchNames' => '1.3.6.1.2.1.11.9',
308 'ifName' => '1.3.6.1.2.1.31.1.1.1.1',
309 'ifInMulticastPkts' => '1.3.6.1.2.1.31.1.1.1.2',
310 'ifInBroadcastPkts' => '1.3.6.1.2.1.31.1.1.1.3',
311 'ifOutMulticastPkts' => '1.3.6.1.2.1.31.1.1.1.4',
312 'ifOutBroadcastPkts' => '1.3.6.1.2.1.31.1.1.1.5',
313 'ifHCInOctets' => '1.3.6.1.2.1.31.1.1.1.6',
314 'ifHCInUcastPkts' => '1.3.6.1.2.1.31.1.1.1.7',
315 'ifHCInMulticastPkts' => '1.3.6.1.2.1.31.1.1.1.8',
316 'ifHCInBroadcastPkts' => '1.3.6.1.2.1.31.1.1.1.9',
317 'ifHCOutOctets' => '1.3.6.1.2.1.31.1.1.1.10',
318 'ifHCOutUcastPkts' => '1.3.6.1.2.1.31.1.1.1.11',
319 'ifHCOutMulticastPkts' => '1.3.6.1.2.1.31.1.1.1.12',
320 'ifHCOutBroadcastPkts' => '1.3.6.1.2.1.31.1.1.1.13',
321 'ifLinkUpDownTrapEnable' => '1.3.6.1.2.1.31.1.1.1.14',
322 'ifHighSpeed' => '1.3.6.1.2.1.31.1.1.1.15',
323 'ifPromiscuousMode' => '1.3.6.1.2.1.31.1.1.1.16',
324 'ifConnectorPresent' => '1.3.6.1.2.1.31.1.1.1.17',
325 'ifAlias' => '1.3.6.1.2.1.31.1.1.1.18',
326 'ifCounterDiscontinuityTime' => '1.3.6.1.2.1.31.1.1.1.19',
327 'experimental' => '1.3.6.1.3',
328 'private' => '1.3.6.1.4',
329 'enterprises' => '1.3.6.1.4.1',
330 );
331  
332 # GIL
333 my %revOIDS = (); # Reversed %SNMP_util::OIDS hash
334 my $RevNeeded = 1;
335  
336 my $agent_start_time = time;
337  
338 undef $SNMP_util::Host;
339 undef $SNMP_util::Session;
340 undef $SNMP_util::Version;
341 undef $SNMP_util::LHost;
342 undef $SNMP_util::IPv4only;
343 $SNMP_util::Debug = 0;
344 $SNMP_util::CacheFile = "OID_cache.txt";
345 $SNMP_util::CacheLoaded = 0;
346 $SNMP_util::Return_array_refs = 0;
347 $SNMP_util::Return_hash_refs = 0;
348  
349 srand(time + $$);
350  
351 ### Prototypes
352 sub snmpget ($@);
353 sub snmpgetnext ($@);
354 sub snmpopen ($$$);
355 sub snmpwalk ($@);
356 sub snmpwalk_flg ($$@);
357 sub snmpset ($@);
358 sub snmptrap ($$$$$@);
359 sub snmpgetbulk ($$$@);
360 sub snmpmaptable ($$@);
361 sub snmpmaptable4 ($$$@);
362 sub snmpwalkhash ($$@);
363 sub toOID (@);
364 sub snmpmapOID (@);
365 sub snmpMIB_to_OID ($);
366 sub encode_oid_with_errmsg ($);
367 sub Check_OID ($);
368 sub snmpLoad_OID_Cache ($);
369 sub snmpQueue_MIB_File (@);
370 sub MIB_fill_OID ($);
371  
372 sub version () { $VERSION; }
373  
374 #
375 # Start an snmp session
376 #
377 sub snmpopen ($$$) {
378 my($host, $type, $vars) = @_;
379 my($nhost, $port, $community, $lhost, $lport, $nlhost);
380 my($timeout, $retries, $backoff, $version);
381 my $v4onlystr;
382  
383 $type = 0 if (!defined($type));
384 $community = "public";
385 $nlhost = "";
386  
387 ($community, $host) = ($1, $2) if ($host =~ /^(.*)@([^@]+)$/);
388  
389 # We can't split on the : character because a numeric IPv6
390 # address contains a variable number of :'s
391 my $opts;
392 if( ($host =~ /^(\[.*\]):(.*)$/) or ($host =~ /^(\[.*\])$/) ) {
393 # Numeric IPv6 address between []
394 ($host, $opts) = ($1, $2);
395 } else {
396 # Hostname or numeric IPv4 address
397 ($host, $opts) = split(':', $host, 2);
398 }
399 ($port, $timeout, $retries, $backoff, $version, $v4onlystr) = split(':', $opts, 6)
400 if(defined($opts) and (length $opts > 0) );
401  
402 undef($version) if (defined($version) and length($version) <= 0);
403 $v4onlystr = "" unless defined $v4onlystr;
404 $version = '1' unless defined $version;
405 if (defined($port) and ($port =~ /^([^!]*)!(.*)$/)) {
406 ($port, $lhost) = ($1, $2);
407 $nlhost = $lhost;
408 ($lhost, $lport) = ($1, $2) if ($lhost =~ /^(.*)!(.*)$/);
409 undef($lhost) if (defined($lhost) and (length($lhost) <= 0));
410 undef($lport) if (defined($lport) and (length($lport) <= 0));
411 }
412 undef($port) if (defined($port) and length($port) <= 0);
413 $port = 162 if ($type == 1 and !defined($port));
414 $nhost = "$community\@$host";
415 $nhost .= ":" . $port if (defined($port));
416  
417 if ((!defined($SNMP_util::Session))
418 or ($SNMP_util::Host ne $nhost)
419 or ($SNMP_util::Version ne $version)
420 or ($SNMP_util::LHost ne $nlhost)
421 or ($SNMP_util::IPv4only ne $v4onlystr)) {
422 if (defined($SNMP_util::Session)) {
423 $SNMP_util::Session->close();
424 undef $SNMP_util::Session;
425 undef $SNMP_util::Host;
426 undef $SNMP_util::Version;
427 undef $SNMP_util::LHost;
428 undef $SNMP_util::IPv4only;
429 }
430 $SNMP_util::Session = ($version =~ /^2c?$/i)
431 ? SNMPv2c_Session->open($host, $community, $port, undef,
432 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 )
433 : SNMP_Session->open($host, $community, $port, undef,
434 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 );
435 ($SNMP_util::Host = $nhost, $SNMP_util::Version = $version,
436 $SNMP_util::LHost = $nlhost, $SNMP_util::IPv4only = $v4onlystr) if defined($SNMP_util::Session);
437 }
438  
439 if (defined($SNMP_util::Session)) {
440 if (ref $vars->[0] eq 'HASH') {
441 my $opts = shift @$vars;
442 foreach $type (keys %$opts) {
443 if ($type eq 'return_array_refs') {
444 $SNMP_util::Return_array_refs = $opts->{$type};
445 } elsif ($type eq 'return_hash_refs') {
446 $SNMP_util::Return_hash_refs = $opts->{$type};
447 } else {
448 if (exists $SNMP_util::Session->{$type}) {
449 if ($type eq 'timeout') {
450 $SNMP_util::Session->set_timeout($opts->{$type});
451 } elsif ($type eq 'retries') {
452 $SNMP_util::Session->set_retries($opts->{$type});
453 } elsif ($type eq 'backoff') {
454 $SNMP_util::Session->set_backoff($opts->{$type});
455 } else {
456 $SNMP_util::Session->{$type} = $opts->{$type};
457 }
458 } else {
459 carp "SNMPopen Unknown SNMP Option Key '$type'\n"
460 unless ($SNMP_Session::suppress_warnings > 1);
461 }
462 }
463 }
464 }
465 $SNMP_util::Session->set_timeout($timeout)
466 if (defined($timeout) and (length($timeout) > 0));
467 $SNMP_util::Session->set_retries($retries)
468 if (defined($retries) and (length($retries) > 0));
469 $SNMP_util::Session->set_backoff($backoff)
470 if (defined($backoff) and (length($backoff) > 0));
471 }
472 return $SNMP_util::Session;
473 }
474  
475  
476 #
477 # A restricted snmpget.
478 #
479 sub snmpget ($@) {
480 my($host, @vars) = @_;
481 my(@enoid, $var, $response, $bindings, $binding, $value, $oid, @retvals);
482 my $session;
483  
484 @retvals = ();
485 $session = &snmpopen($host, 0, \@vars);
486 if (!defined($session)) {
487 carp "SNMPGET Problem for $host\n"
488 unless ($SNMP_Session::suppress_warnings > 1);
489 return wantarray ? @retvals : undef;
490 }
491  
492 @enoid = &toOID(@vars);
493 if ($#enoid < 0) {
494 return wantarray ? @retvals : undef;
495 }
496  
497 if ($session->get_request_response(@enoid)) {
498 $response = $session->pdu_buffer;
499 ($bindings) = $session->decode_get_response($response);
500 while ($bindings) {
501 ($binding, $bindings) = decode_sequence($bindings);
502 ($oid, $value) = decode_by_template($binding, "%O%@");
503 my $tempo = pretty_print($value);
504 push @retvals, $tempo;
505 }
506 return wantarray ? @retvals : $retvals[0];
507 }
508 $var = join(' ', @vars);
509 carp "SNMPGET Problem for $var on $host\n"
510 unless ($SNMP_Session::suppress_warnings > 1);
511 return wantarray ? @retvals : undef;
512 }
513  
514 #
515 # A restricted snmpgetnext.
516 #
517 sub snmpgetnext ($@) {
518 my($host, @vars) = @_;
519 my(@enoid, $var, $response, $bindings, $binding);
520 my($value, $upoid, $oid, @retvals);
521 my($noid);
522 my $session;
523  
524 @retvals = ();
525 $session = &snmpopen($host, 0, \@vars);
526 if (!defined($session)) {
527 carp "SNMPGETNEXT Problem for $host\n"
528 unless ($SNMP_Session::suppress_warnings > 1);
529 return wantarray ? @retvals : undef;
530 }
531  
532 @enoid = &toOID(@vars);
533 if ($#enoid < 0) {
534 return wantarray ? @retvals : undef;
535 }
536  
537 undef @vars;
538 undef @retvals;
539 foreach $noid (@enoid) {
540 $upoid = pretty_print($noid);
541 push(@vars, $upoid);
542 }
543 if ($session->getnext_request_response(@enoid)) {
544 $response = $session->pdu_buffer;
545 ($bindings) = $session->decode_get_response($response);
546 while ($bindings) {
547 ($binding, $bindings) = decode_sequence($bindings);
548 ($oid, $value) = decode_by_template($binding, "%O%@");
549 my $tempo = pretty_print($oid);
550 my $tempv = pretty_print($value);
551 push @retvals, "$tempo:$tempv";
552 }
553 return wantarray ? @retvals : $retvals[0];
554 } else {
555 $var = join(' ', @vars);
556 carp "SNMPGETNEXT Problem for $var on $host\n"
557 unless ($SNMP_Session::suppress_warnings > 1);
558 return wantarray ? @retvals : undef;
559 }
560 }
561  
562 #
563 # A restricted snmpwalk.
564 #
565 sub snmpwalk ($@) {
566 my($host, @vars) = @_;
567 return(&snmpwalk_flg($host, undef, @vars));
568 }
569  
570 #
571 # Walk the MIB, putting everything you find into hashes.
572 #
573 sub snmpwalkhash($$@) {
574 # my($host, $hash_sub, @vars) = @_;
575 return(&snmpwalk_flg( @_ ));
576 }
577  
578 sub snmpwalk_flg ($$@) {
579 my($host, $hash_sub, @vars) = @_;
580 my(@enoid, $var, $response, $bindings, $binding);
581 my($value, $upoid, $oid, @retvals, @retvaltmprefs);
582 my($got, @nnoid, $noid, $ok, $ix, @avars);
583 my $session;
584 my(%soid);
585 my(%done, %rethash, $h_ref);
586  
587 $h_ref = (ref $vars[$#vars] eq "HASH") ? pop(@vars) : \%rethash;
588  
589 $session = &snmpopen($host, 0, \@vars);
590 if (!defined($session)) {
591 carp "SNMPWALK Problem for $host\n"
592 unless ($SNMP_Session::suppress_warnings > 1);
593 if (defined($hash_sub)) {
594 return ($h_ref) if ($SNMP_util::Return_hash_refs);
595 return (%$h_ref);
596 } else {
597 @retvals = ();
598 return (@retvals);
599 }
600 }
601  
602 @enoid = toOID(@vars);
603 if ($#enoid < 0) {
604 if (defined($hash_sub)) {
605 return ($h_ref) if ($SNMP_util::Return_hash_refs);
606 return (%$h_ref);
607 } else {
608 @retvals = ();
609 return (@retvals);
610 }
611 }
612  
613 # GIL
614 #
615 # Create/Refresh a reversed hash with oid -> name
616 #
617 if (defined($hash_sub) and ($RevNeeded)) {
618 %revOIDS = reverse %SNMP_util::OIDS;
619 $RevNeeded = 0;
620 }
621  
622 $got = 0;
623 @nnoid = @enoid;
624 undef @vars;
625 foreach $noid (@enoid) {
626 $upoid = pretty_print($noid);
627 push(@vars, $upoid);
628 }
629  
630 # @vars is the original set of walked variables.
631 # @avars is the current set of walked variables as the
632 # walk goes on.
633 # @vars stays static while @avars may shrink as we reach end
634 # of walk for individual variables during PDU exchange.
635
636 @avars = @vars;
637  
638 # IlvJa
639 #
640 # Create temporary array of refs to return vals.
641  
642 if ($SNMP_util::Return_array_refs) {
643 for($ix = 0;$ix < scalar @vars; $ix++) {
644 my $tmparray = [];
645 $retvaltmprefs[$ix] = $tmparray;
646 $retvals[$ix] = $tmparray;
647 }
648 }
649  
650  
651 while(($SNMP_util::Version ne '1' and $session->{'use_getbulk'})
652 ? $session->getbulk_request_response(0,
653 $session->default_max_repetitions(),
654 @nnoid)
655 : $session->getnext_request_response(@nnoid))
656 {
657 $got = 1;
658 $response = $session->pdu_buffer;
659 ($bindings) = $session->decode_get_response($response);
660 $ix = 0;
661 while ($bindings) {
662 ($binding, $bindings) = decode_sequence($bindings);
663 unless ($nnoid[$ix]) { # IlvJa
664 $ix = ++$ix % (scalar @avars);
665 next;
666 }
667 ($oid, $value) = decode_by_template($binding, "%O%@");
668 $ok = 0;
669 my $tempo = pretty_print($oid);
670 $noid = $avars[$ix]; # IlvJa
671 if ($tempo =~ /^$noid\./ or $tempo eq $noid ) {
672 $ok = 1;
673 $upoid = $noid;
674 } else {
675 # IlvJa
676 #
677 # The walk for variable $vars[$ix] has been finished as
678 # $nnoid[$ix] no longer is in the $avar[$ix] OID tree.
679 # So we exclude this variable from further requests.
680  
681 $avars[$ix] = "";
682 $nnoid[$ix] = "";
683 $retvaltmprefs[$ix] = undef if $SNMP_util::Return_array_refs;
684 }
685 if ($ok) {
686 my $tmp = encode_oid_with_errmsg ($tempo);
687 if (!defined $tmp) {
688 if (defined($hash_sub)) {
689 return ($h_ref) if ($SNMP_util::Return_hash_refs);
690 return (%$h_ref);
691 } else {
692 @retvals = ();
693 return (@retvals);
694 }
695 }
696 if (exists($done{$tmp})) { # GIL, Ilvja
697 #
698 # We've detected a loop for $nnoid[$ix], so mark it as finished.
699 # Exclude this variable from further requests.
700 #
701 $avars[$ix] = "";
702 $nnoid[$ix] = "";
703 $retvaltmprefs[$ix] = undef if $SNMP_util::Return_array_refs;
704 next;
705 }
706 $nnoid[$ix] = $tmp; # Keep on walking. (IlvJa)
707 my $tempv = pretty_print($value);
708 if (defined($hash_sub)) {
709 #
710 # extract name of the oid, if possible, the rest becomes the instance
711 #
712 my $inst = "";
713 my $upo = $upoid;
714 while (!exists($revOIDS{$upo}) and length($upo)) {
715 $upo =~ s/(\.\d+?)$//;
716 if (defined($1) and length($1)) {
717 $inst = $1 . $inst;
718 } else {
719 $upo = "";
720 last;
721 }
722 }
723 if (length($upo) and exists($revOIDS{$upo})) {
724 $upo = $revOIDS{$upo} . $inst;
725 } else {
726 $upo = $upoid;
727 }
728  
729 $inst = "";
730 while (!exists($revOIDS{$tempo}) and length($tempo)) {
731 $tempo =~ s/(\.\d+?)$//;
732 if (defined($1) and length($1)) {
733 $inst = $1 . $inst;
734 } else {
735 $tempo = "";
736 last;
737 }
738 }
739 if (length($tempo) and exists($revOIDS{$tempo})) {
740 $var = $revOIDS{$tempo};
741 } else {
742 $var = pretty_print($oid);
743 }
744 #
745 # call hash_sub
746 #
747 &$hash_sub($h_ref, $host, $var, $tempo, $inst,
748 $tempv, $upo);
749 } else {
750 if ($SNMP_util::Return_array_refs) {
751 $tempo=~s/^$upoid\.//;
752 push @{$retvaltmprefs[$ix]}, "$tempo:$tempv";
753 } else {
754 $tempo=~s/^$upoid\.// if ($#enoid <= 0);
755 push @retvals, "$tempo:$tempv";
756 }
757 }
758 $done{$tmp} = 1; # GIL
759 }
760 $ix = ++$ix % (scalar @avars);
761 }
762  
763 # Ok, @nnoid should contain the remaining variables for the
764 # next request. Some or all entries in @nnoid might be the empty
765 # string. If the nth element in @nnoid is "" that means that
766 # the walk related to the nth variable in the last request has been
767 # completed and we should not include that var in subsequent reqs.
768  
769 # Clean up both @nnoid and @avars so "" elements are removed.
770 @nnoid = grep (($_), @nnoid);
771 @avars = grep (($_), @avars);
772 @retvaltmprefs = grep (($_), @retvaltmprefs);
773  
774 last if ($#nnoid < 0); # @nnoid empty means we are done walking.
775 }
776 if (!$got) {
777 $var = join(' ', @vars);
778 carp "SNMPWALK Problem for $var on $host\n"
779 unless ($SNMP_Session::suppress_warnings > 1);
780 @retvals = ();
781 }
782 if (defined($hash_sub)) {
783 return ($h_ref) if ($SNMP_util::Return_hash_refs);
784 return (%$h_ref);
785 } else {
786 return (@retvals);
787 }
788 }
789  
790 #
791 # A restricted snmpset.
792 #
793 sub snmpset($@) {
794 my($host, @vars) = @_;
795 my(@enoid, $response, $bindings, $binding);
796 my($oid, @retvals, $type, $value, $val);
797 my $session;
798  
799 @retvals = ();
800 $session = &snmpopen($host, 0, \@vars);
801 if (!defined($session)) {
802 carp "SNMPSET Problem for $host\n"
803 unless ($SNMP_Session::suppress_warnings > 1);
804 return wantarray ? @retvals : undef;
805 }
806  
807 while(@vars) {
808 ($oid) = toOID((shift @vars));
809 $type = shift @vars;
810 $value = shift @vars;
811 $type =~ tr/A-Z/a-z/;
812 if ($type eq "int") {
813 $val = encode_int($value);
814 } elsif ($type eq "integer") {
815 $val = encode_int($value);
816 } elsif ($type eq "string") {
817 $val = encode_string($value);
818 } elsif ($type eq "octetstring") {
819 $val = encode_string($value);
820 } elsif ($type eq "octet string") {
821 $val = encode_string($value);
822 } elsif ($type eq "oid") {
823 $val = encode_oid_with_errmsg($value);
824 } elsif ($type eq "object id") {
825 $val = encode_oid_with_errmsg($value);
826 } elsif ($type eq "object identifier") {
827 $val = encode_oid_with_errmsg($value);
828 } elsif ($type eq "ipaddr") {
829 $val = encode_ip_address($value);
830 } elsif ($type eq "ip address") {
831 $val = encode_ip_address($value);
832 } elsif ($type eq "timeticks") {
833 $val = encode_timeticks($value);
834 } elsif ($type eq "uint") {
835 $val = encode_uinteger32($value);
836 } elsif ($type eq "uinteger") {
837 $val = encode_uinteger32($value);
838 } elsif ($type eq "uinteger32") {
839 $val = encode_uinteger32($value);
840 } elsif ($type eq "unsigned int") {
841 $val = encode_uinteger32($value);
842 } elsif ($type eq "unsigned integer") {
843 $val = encode_uinteger32($value);
844 } elsif ($type eq "unsigned integer32") {
845 $val = encode_uinteger32($value);
846 } elsif ($type eq "counter") {
847 $val = encode_counter32($value);
848 } elsif ($type eq "counter32") {
849 $val = encode_counter32($value);
850 } elsif ($type eq "counter64") {
851 $val = encode_counter64($value);
852 } elsif ($type eq "gauge") {
853 $val = encode_gauge32($value);
854 } elsif ($type eq "gauge32") {
855 $val = encode_gauge32($value);
856 } else {
857 carp "unknown SNMP type: $type\n"
858 unless ($SNMP_Session::suppress_warnings > 1);
859 return wantarray ? @retvals : undef;
860 }
861 if (!defined($val)) {
862 carp "SNMP type $type value $value didn't encode properly\n"
863 unless ($SNMP_Session::suppress_warnings > 1);
864 return wantarray ? @retvals : undef;
865 }
866 push @enoid, [$oid,$val];
867 }
868 if ($#enoid < 0) {
869 return wantarray ? @retvals : undef;
870 }
871 if ($session->set_request_response(@enoid)) {
872 $response = $session->pdu_buffer;
873 ($bindings) = $session->decode_get_response($response);
874 while ($bindings) {
875 ($binding, $bindings) = decode_sequence($bindings);
876 ($oid, $value) = decode_by_template($binding, "%O%@");
877 my $tempo = pretty_print($value);
878 push @retvals, $tempo;
879 }
880 return wantarray ? @retvals : $retvals[0];
881 }
882 return wantarray ? @retvals : undef;
883 }
884  
885 #
886 # Send an SNMP trap
887 #
888 sub snmptrap($$$$$@) {
889 my($host, $ent, $agent, $gen, $spec, @vars) = @_;
890 my($oid, @retvals, $type, $value);
891 my(@enoid);
892 my $session;
893  
894 $session = &snmpopen($host, 1, \@vars);
895 if (!defined($session)) {
896 carp "SNMPTRAP Problem for $host\n"
897 unless ($SNMP_Session::suppress_warnings > 1);
898 return undef;
899 }
900  
901 if ($agent =~ /^\d+\.\d+\.\d+\.\d+(.*)/ ) {
902 $agent = pack("C*", split /\./, $agent);
903 } else {
904 $agent = inet_aton($agent);
905 }
906 push @enoid, toOID(($ent));
907 push @enoid, encode_ip_address($agent);
908 push @enoid, encode_int($gen);
909 push @enoid, encode_int($spec);
910 push @enoid, encode_timeticks((time-$agent_start_time) * 100);
911 while(@vars) {
912 ($oid) = toOID((shift @vars));
913 $type = shift @vars;
914 $value = shift @vars;
915 if ($type =~ /string/i) {
916 $value = encode_string($value);
917 push @enoid, [$oid,$value];
918 } elsif ($type =~ /ipaddr/i) {
919 $value = encode_ip_address($value);
920 push @enoid, [$oid,$value];
921 } elsif ($type =~ /int/i) {
922 $value = encode_int($value);
923 push @enoid, [$oid,$value];
924 } elsif ($type =~ /oid/i) {
925 my $tmp = encode_oid_with_errmsg($value);
926 return undef unless defined $tmp;
927 push @enoid, [$oid,$tmp];
928 } else {
929 carp "unknown SNMP type: $type\n"
930 unless ($SNMP_Session::suppress_warnings > 1);
931 return undef;
932 }
933 }
934 return($session->trap_request_send(@enoid));
935 }
936  
937 #
938 # A restricted snmpgetbulk.
939 #
940 sub snmpgetbulk ($$$@) {
941 my($host, $nr, $mr, @vars) = @_;
942 my(@enoid, $var, $response, $bindings, $binding);
943 my($value, $upoid, $oid, @retvals);
944 my($noid);
945 my $session;
946  
947 @retvals = ();
948 $session = &snmpopen($host, 0, \@vars);
949 if (!defined($session)) {
950 carp "SNMPGETBULK Problem for $host\n"
951 unless ($SNMP_Session::suppress_warnings > 1);
952 return @retvals;
953 }
954  
955 @enoid = &toOID(@vars);
956 return @retvals if ($#enoid < 0);
957  
958 undef @vars;
959 foreach $noid (@enoid) {
960 $upoid = pretty_print($noid);
961 push(@vars, $upoid);
962 }
963 if ($session->getbulk_request_response($nr, $mr, @enoid)) {
964 $response = $session->pdu_buffer;
965 ($bindings) = $session->decode_get_response($response);
966 while ($bindings) {
967 ($binding, $bindings) = decode_sequence($bindings);
968 ($oid, $value) = decode_by_template($binding, "%O%@");
969 my $tempo = pretty_print($oid);
970 my $tempv = pretty_print($value);
971 push @retvals, "$tempo:$tempv";
972 }
973 return @retvals;
974 } else {
975 $var = join(' ', @vars);
976 carp "SNMPGETBULK Problem for $var on $host\n"
977 unless ($SNMP_Session::suppress_warnings > 1);
978 return @retvals;
979 }
980 }
981  
982 #
983 # walk a table, calling a user-supplied function for each
984 # column of a table.
985 #
986 sub snmpmaptable($$@) {
987 my($host, $fun, @vars) = @_;
988 return snmpmaptable4($host, $fun, 0, @vars);
989 }
990  
991 sub snmpmaptable4($$$@) {
992 my($host, $fun, $max_reps, @vars) = @_;
993 my(@enoid, $var, $session);
994  
995 $session = &snmpopen($host, 0, \@vars);
996 if (!defined($session)) {
997 carp "SNMPMAPTABLE Problem for $host\n"
998 unless ($SNMP_Session::suppress_warnings > 1);
999 return undef;
1000 }
1001  
1002 foreach $var (toOID(@vars)) {
1003 push(@enoid, [split('\.', pretty_print($var))]);
1004 }
1005  
1006 $max_reps = $session->default_max_repetitions() if ($max_reps <= 0);
1007  
1008 return $session->map_table_start_end( [@enoid],
1009 sub() {
1010 my ($ind, @vals) = @_;
1011 my (@pvals, $val);
1012  
1013 foreach $val (@vals) {
1014 push(@pvals, pretty_print($val));
1015 }
1016 &$fun($ind, @pvals);
1017 },
1018 "", undef, $max_reps);
1019 }
1020  
1021  
1022 #
1023 # Given an OID in either ASN.1 or mixed text/ASN.1 notation, return an
1024 # encoded OID.
1025 #
1026 sub toOID(@) {
1027 my(@vars) = @_;
1028 my($oid, $var, $tmp, $tmpv, @retvar);
1029  
1030 undef @retvar;
1031 foreach $var (@vars) {
1032 ($oid, $tmp) = &Check_OID($var);
1033 if (!$oid and $SNMP_util::CacheLoaded == 0) {
1034 $tmp = $SNMP_Session::suppress_warnings;
1035 $SNMP_Session::suppress_warnings = 1000;
1036  
1037 &snmpLoad_OID_Cache($SNMP_util::CacheFile);
1038  
1039 $SNMP_util::CacheLoaded = 1;
1040 $SNMP_Session::suppress_warnings = $tmp;
1041  
1042 ($oid, $tmp) = &Check_OID($var);
1043 }
1044 while (!$oid and $#SNMP_util::MIB_Files >= 0) {
1045 $tmp = $SNMP_Session::suppress_warnings;
1046 $SNMP_Session::suppress_warnings = 1000;
1047  
1048 snmpMIB_to_OID(shift(@SNMP_util::MIB_Files));
1049  
1050 $SNMP_Session::suppress_warnings = $tmp;
1051  
1052 ($oid, $tmp) = &Check_OID($var);
1053 if ($oid) {
1054 open(CACHE, ">>$SNMP_util::CacheFile");
1055 print CACHE "$tmp\t$oid\n";
1056 close(CACHE);
1057 }
1058 }
1059 if ($oid) {
1060 $var =~ s/^$tmp/$oid/;
1061 } else {
1062 carp "Unknown SNMP var $var\n"
1063 unless ($SNMP_Session::suppress_warnings > 1);
1064 next;
1065 }
1066 while ($var =~ /\"([^\"]*)\"/) {
1067 $tmp = sprintf("%d.%s", length($1), join(".", map(ord, split(//, $1))));
1068 $var =~ s/\"$1\"/$tmp/;
1069 }
1070 print "toOID: $var\n" if $SNMP_util::Debug;
1071 $tmp = encode_oid_with_errmsg($var);
1072 if (!defined($tmp)) {
1073 my @empty = ();
1074 return @empty;
1075 }
1076 push(@retvar, $tmp);
1077 }
1078 return @retvar;
1079 }
1080  
1081 #
1082 # Add passed-in text, OID pairs to the OID mapping table.
1083 #
1084 sub snmpmapOID(@)
1085 {
1086 my(@vars) = @_;
1087 my($oid, $txt);
1088  
1089 while($#vars >= 0) {
1090 $txt = shift @vars;
1091 $oid = shift @vars;
1092  
1093 next unless($txt =~ /^[a-zA-Z][\w\-]*(\.[a-zA-Z][\w\-])*$/);
1094 next unless($oid =~ /^\d+(\.\d+)*$/);
1095  
1096 $SNMP_util::OIDS{$txt} = $oid;
1097 $RevNeeded = 1;
1098 print "snmpmapOID: $txt => $oid\n" if $SNMP_util::Debug;
1099 }
1100
1101 return undef;
1102 }
1103  
1104 #
1105 # Open the passed-in file name and read it in to populate
1106 # the cache of text-to-OID map table. It expects lines
1107 # with two fields, the first the textual string like "ifInOctets",
1108 # and the second the OID value, like "1.3.6.1.2.1.2.2.1.10".
1109 #
1110 # blank lines and anything after a '#' or between '--' is ignored.
1111 #
1112 sub snmpLoad_OID_Cache ($) {
1113 my($arg) = @_;
1114 my($txt, $oid);
1115  
1116 if (!open(CACHE, $arg)) {
1117 carp "snmpLoad_OID_Cache: Can't open $arg: $!"
1118 unless ($SNMP_Session::suppress_warnings > 1);
1119 return -1;
1120 }
1121  
1122 while(<CACHE>) {
1123 s/#.*//; # '#' starts a comment
1124 s/--.*?--/ /g; # comment delimited by '--', like MIBs
1125 s/--.*//; # comment started by '--'
1126 next if (/^$/);
1127 next unless (/\s/); # must have whitespace as separator
1128 chomp;
1129 ($txt, $oid) = split(' ', $_, 2);
1130 $txt = $1 if ($txt =~ /^[\'\"](.*)[\'\"]/);
1131 $oid = $1 if ($oid =~ /^[\'\"](.*)[\'\"]/);
1132 if (($txt =~ /^\.?\d+(\.\d+)*\.?$/)
1133 and ($oid !~ /^\.?\d+(\.\d+)*\.?$/)) {
1134 my($a) = $oid;
1135 $oid = $txt;
1136 $txt = $a;
1137 }
1138 $oid =~ s/^\.//;
1139 $oid =~ s/\.$//;
1140 &snmpmapOID($txt, $oid);
1141 }
1142 close(CACHE);
1143 return 0;
1144 }
1145  
1146 #
1147 # Check to see if an OID is in the text-to-OID cache.
1148 # Returns the OID and the corresponding text as two separate
1149 # elements.
1150 #
1151 sub Check_OID ($) {
1152 my($var) = @_;
1153 my($tmp, $tmpv, $oid);
1154  
1155 if ($var =~ /^[a-zA-Z][\w\-]*(\.[a-zA-Z][\w\-]*)*/)
1156 {
1157 $tmp = $&;
1158 $tmpv = $tmp;
1159 for (;;) {
1160 last if exists($SNMP_util::OIDS{$tmpv});
1161 last if !($tmpv =~ s/^[^\.]*\.//);
1162 }
1163 $oid = $SNMP_util::OIDS{$tmpv};
1164 if ($oid) {
1165 return ($oid, $tmp);
1166 } else {
1167 my @empty = ();
1168 return @empty;
1169 }
1170 }
1171 return ($var, $var);
1172 }
1173  
1174 #
1175 # Save the passed-in list of MIB files until an OID can't be
1176 # found in the existing table. At that time the MIB file will
1177 # be loaded, and the lookup attempted again.
1178 #
1179 sub snmpQueue_MIB_File (@) {
1180 my(@files) = @_;
1181 my($file);
1182  
1183 foreach $file (@files) {
1184 push(@SNMP_util::MIB_Files, $file);
1185 }
1186 }
1187  
1188 #
1189 # Read in the passed MIB file, parsing it
1190 # for their text-to-OID mappings
1191 #
1192 sub snmpMIB_to_OID ($) {
1193 my($arg) = @_;
1194 my($cnt, $quote, $buf, %tOIDs, $tgot);
1195 my($var, @parts, $strt, $indx, $ind, $val);
1196  
1197 if (!open(MIB, $arg)) {
1198 carp "snmpMIB_to_OID: Can't open $arg: $!"
1199 unless ($SNMP_Session::suppress_warnings > 1);
1200 return -1;
1201 }
1202 print "snmpMIB_to_OID: loading $arg\n" if $SNMP_util::Debug;
1203 $cnt = 0;
1204 $quote = 0;
1205 $tgot = 0;
1206 $buf = '';
1207 while(<MIB>) {
1208 if ($quote) {
1209 next unless /"/;
1210 $quote = 0;
1211 }
1212 chomp;
1213 $buf .= ' ' . $_;
1214  
1215 $buf =~ s/"[^"]*"//g; # throw away quoted strings
1216 $buf =~ s/--.*?--/ /g; # throw away comments (-- anything --)
1217 $buf =~ s/--.*//; # throw away comments (-- anything to EOL)
1218 $buf =~ s/\s+/ /g; # clean up multiple spaces
1219  
1220 if ($buf =~ /"/) { # look for quoted string
1221 $quote = 1;
1222 next;
1223 }
1224  
1225 if ($buf =~ /DEFINITIONS *::= *BEGIN/) {
1226 $cnt += MIB_fill_OID(\%tOIDs) if ($tgot);
1227 $buf = '';
1228 %tOIDs = ();
1229 $tgot = 0;
1230 next;
1231 }
1232 $buf =~ s/OBJECT-TYPE/OBJECT IDENTIFIER/;
1233 $buf =~ s/OBJECT-IDENTITY/OBJECT IDENTIFIER/;
1234 $buf =~ s/OBJECT-GROUP/OBJECT IDENTIFIER/;
1235 $buf =~ s/MODULE-IDENTITY/OBJECT IDENTIFIER/;
1236 $buf =~ s/NOTIFICATION-TYPE/OBJECT IDENTIFIER/;
1237 $buf =~ s/ IMPORTS .*\;//;
1238 $buf =~ s/ SEQUENCE *{.*}//;
1239 $buf =~ s/ SYNTAX .*//;
1240 $buf =~ s/ [\w\-]+ *::= *OBJECT IDENTIFIER//;
1241 $buf =~ s/ OBJECT IDENTIFIER.*::= *{/ OBJECT IDENTIFIER ::= {/;
1242  
1243 if ($buf =~ / ([\w\-]+) OBJECT IDENTIFIER *::= *{([^}]+)}/) {
1244 $var = $1;
1245 $buf = $2;
1246 $buf =~ s/ +$//;
1247 $buf =~ s/\s+\(/\(/g; # remove spacing around '('
1248 $buf =~ s/\(\s+/\(/g;
1249 $buf =~ s/\s+\)/\)/g; # remove spacing before ')'
1250 @parts = split(' ', $buf);
1251 $strt = '';
1252 foreach $indx (@parts) {
1253 if ($indx =~ /([\w\-]+)\((\d+)\)/) {
1254 $ind = $1;
1255 $val = $2;
1256 if (exists($tOIDs{$strt})) {
1257 $tOIDs{$ind} = $tOIDs{$strt} . '.' . $val;
1258 } elsif ($strt ne '') {
1259 $tOIDs{$ind} = "${strt}.${val}";
1260 } else {
1261 $tOIDs{$ind} = $val;
1262 }
1263 $strt = $ind;
1264 $tgot = 1;
1265 } elsif ($indx =~ /^\d+$/) {
1266 if (exists($tOIDs{$strt})) {
1267 $tOIDs{$var} = $tOIDs{$strt} . '.' . $indx;
1268 } else {
1269 $tOIDs{$var} = "${strt}.${indx}";
1270 }
1271 $tgot = 1;
1272 } else {
1273 $strt = $indx;
1274 }
1275 }
1276 $buf = '';
1277 }
1278 }
1279 $cnt += MIB_fill_OID(\%tOIDs) if ($tgot);
1280 $RevNeeded = 1 if ($cnt > 0);
1281 return $cnt;
1282 }
1283  
1284 #
1285 # Fill the OIDS hash with results from the MIB parsing
1286 #
1287 sub MIB_fill_OID ($)
1288 {
1289 my($href) = @_;
1290 my($cnt, $changed, @del, $var, $val, @parts, $indx);
1291 my(%seen);
1292  
1293 $cnt = 0;
1294 do {
1295 $changed = 0;
1296 @del = ();
1297 foreach $var (keys %$href) {
1298 $val = $href->{$var};
1299 @parts = split('\.', $val);
1300 $val = '';
1301 foreach $indx (@parts) {
1302 if ($indx =~ /^\d+$/) {
1303 $val .= '.' . $indx;
1304 } else {
1305 if (exists($SNMP_util::OIDS{$indx})) {
1306 $val = $SNMP_util::OIDS{$indx};
1307 } else {
1308 $val .= '.' . $indx;
1309 }
1310 }
1311 }
1312 if ($val =~ /^[\d\.]+$/) {
1313 $val =~ s/^\.+//;
1314 if (!exists($SNMP_util::OIDS{$var})
1315 || (length($val) > length($SNMP_util::OIDS{$var}))) {
1316 $SNMP_util::OIDS{$var} = $val;
1317 print "'$var' => '$val'\n" if $SNMP_util::Debug;
1318 $changed = 1;
1319 $cnt++;
1320 }
1321 push @del, $var;
1322 }
1323 }
1324 foreach $var (@del) {
1325 delete $href->{$var};
1326 }
1327 } while($changed);
1328  
1329 $Carp::CarpLevel++;
1330 foreach $var (sort keys %$href) {
1331 $val = $href->{$var};
1332 $val =~ s/\..*//;
1333 next if (exists($seen{$val}));
1334 $seen{$val} = 1;
1335 $seen{$var} = 1;
1336 carp "snmpMIB_to_OID: prefix \"$val\" unknown, load the parent MIB first.\n"
1337 unless ($SNMP_Session::suppress_warnings > 1);
1338 }
1339 $Carp::CarpLevel--;
1340 return $cnt;
1341 }
1342  
1343  
1344 sub encode_oid_with_errmsg ($) {
1345 my ($oid) = @_;
1346 my $tmp = encode_oid(split(/\./, $oid));
1347 if (! defined $tmp) {
1348 carp "cannot encode Object ID $oid: $BER::errmsg"
1349 unless ($SNMP_Session::suppress_warnings > 1);
1350 return undef;
1351 }
1352 return $tmp;
1353 }
1354  
1355 1;