SNMP_util.pm untested

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