2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * Licensed under the Academic Free License version 2.1
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
15 #undef PACKAGE_VERSION
17 #include <net-snmp/net-snmp-config.h>
18 #include <net-snmp/net-snmp-includes.h>
24 hrDeviceDesc_to_info(char *string
, char **manufacturer
, char **model
,
33 /* if it has : and ; in it, it's probably a 1284 device id */
34 if ((strchr(string
, ':') != NULL
) && (strchr(string
, ';') != NULL
)) {
35 rc
= ieee1284_devid_to_printer_info(string
, manufacturer
, model
,
36 description
, NULL
, NULL
, NULL
);
39 *description
= strdup(string
);
40 *manufacturer
= strdup(string
);
41 if ((s
= strchr(*manufacturer
, ' ')) != NULL
) {
50 static struct snmp_pdu
*
51 snmp_get_item(char *host
, char *community
, char *mib_item
)
53 struct snmp_session session
, *ss
;
54 struct snmp_pdu
*request
= NULL
, *result
= NULL
;
56 unsigned int oid_len
= MAX_OID_LEN
;
58 /* initialize the SNMP session */
59 snmp_sess_init(&session
);
60 session
.peername
= host
;
61 session
.community
= (uchar_t
*)community
;
62 session
.community_len
= strlen((const char *)session
.community
);
63 session
.version
= SNMP_VERSION_1
;
66 if ((ss
= snmp_open(&session
)) == NULL
)
69 /* add the requested data */
70 if (!read_objid(mib_item
, Oid
, &oid_len
))
71 snmp_perror(mib_item
);
73 /* initialize the request PDU */
74 request
= snmp_pdu_create(SNMP_MSG_GET
);
75 snmp_add_null_var(request
, Oid
, oid_len
);
77 (void) snmp_synch_response(ss
, request
, &result
);
85 snmp_get_string(char *host
, char *community
, char *mib_item
)
88 struct snmp_pdu
*response
= NULL
;
90 response
= snmp_get_item(host
, community
, mib_item
);
92 if ((response
!= NULL
) && (response
->errstat
== SNMP_ERR_NOERROR
)) {
93 struct variable_list
*v
= response
->variables
;
95 if (v
->type
== ASN_OCTET_STR
) {
96 result
= calloc(1, v
->val_len
+ 1);
97 memcpy(result
, v
->val
.string
, v
->val_len
);
101 HAL_DEBUG(("snmp_get_string(%s, %s, %s): %s", host
, community
, mib_item
,
102 (result
?result
:"NULL")));
104 if (response
!= NULL
)
105 snmp_free_pdu(response
);
111 snmp_brother_printer_info(char *hostname
, char *community
, char **manufacturer
,
112 char **model
, char **description
, char **serial_no
,
119 * Brother printers appear to store
120 * 1284 DevID SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0
121 * Serial Number SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0
123 tmp
= snmp_get_string(hostname
, community
,
124 "SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0");
126 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
127 description
, NULL
, serial_no
, command_set
);
130 if (*serial_no
== NULL
)
131 *serial_no
= snmp_get_string(hostname
, community
,
132 "SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0");
139 snmp_ricoh_printer_info(char *hostname
, char *community
, char **manufacturer
,
140 char **model
, char **description
, char **serial_no
,
147 * OKI printers appear to store
148 * 1284 DevID SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0
149 * Serial Number SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0
151 tmp
= snmp_get_string(hostname
, community
,
152 "SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0");
154 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
155 description
, NULL
, serial_no
, command_set
);
158 if (*serial_no
== NULL
)
159 *serial_no
= snmp_get_string(hostname
, community
,
160 "SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0");
167 snmp_lexmark_printer_info(char *hostname
, char *community
, char **manufacturer
,
168 char **model
, char **description
, char **serial_no
,
175 * Lexmark printers appear to store
176 * 1284 DevID SNMPv2-SMI::enterprises.641.2.1.2.1.3.1
177 * Serial Number SNMPv2-SMI::enterprises.641.2.1.2.1.6.1
179 tmp
= snmp_get_string(hostname
, community
,
180 "SNMPv2-SMI::enterprises.641.2.1.2.1.3.1");
182 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
183 description
, NULL
, serial_no
, command_set
);
186 if (*serial_no
== NULL
)
187 *serial_no
= snmp_get_string(hostname
, community
,
188 "SNMPv2-SMI::enterprises.641.2.1.2.1.6.1");
195 snmp_xerox_phaser_printer_info(char *hostname
, char *community
,
196 char **manufacturer
, char **model
, char **description
,
197 char **serial_no
, char ***command_set
, char **uri
)
203 * Xerox Phaser XXXX printers store their
204 * 1284 DevID SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1
206 * SNMPv2-SMI::enterprises.128.2.1.3.1.1.0
207 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16
208 * SNMPv2-SMI::enterprises.23.2.32.4.1.0
210 * SNMPv2-SMI::enterprises.128.2.1.3.1.2.0
211 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17
212 * SNMPv2-SMI::enterprises.23.2.32.4.2.0
213 * Description SNMPv2-SMI::enterprises.253.8.53.3.2.1.2.1
214 * Serial Number SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1
215 * Uri SNMPv2-SMI::enterprises.128.2.1.3.6.23.1.5.1
218 tmp
= snmp_get_string(hostname
, community
,
219 "SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1");
221 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
222 description
, NULL
, serial_no
, command_set
);
226 if (*manufacturer
== NULL
)
227 *manufacturer
= snmp_get_string(hostname
, community
,
228 "SNMPv2-SMI::enterprises.128.2.1.3.1.1.0");
229 if (*manufacturer
== NULL
)
230 *manufacturer
= snmp_get_string(hostname
, community
,
231 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16");
232 if (*manufacturer
== NULL
)
233 *manufacturer
= snmp_get_string(hostname
, community
,
234 "SNMPv2-SMI::enterprises.23.2.32.4.1.0");
237 *model
= snmp_get_string(hostname
, community
,
238 "SNMPv2-SMI::enterprises.128.2.1.3.1.2.0");
240 *model
= snmp_get_string(hostname
, community
,
241 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17");
243 *model
= snmp_get_string(hostname
, community
,
244 "SNMPv2-SMI::enterprises.23.2.32.4.2.0");
246 if (*serial_no
== NULL
)
247 *serial_no
= snmp_get_string(hostname
, community
,
248 "SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1");
250 if ((*manufacturer
!= NULL
) && (*model
!= NULL
))
257 snmp_qms_printer_info(char *hostname
, char *community
, char **manufacturer
,
258 char **model
, char **description
, char **serial_no
,
259 char ***command_set
, char **uri
)
265 * MINOLTA-QMS printers appear to store
266 * Prouct Name SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1
267 * Serial Number SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2
268 * URI SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1
269 * SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2
271 tmp
= snmp_get_string(hostname
, community
,
272 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1");
274 rc
= hrDeviceDesc_to_info(tmp
, manufacturer
, model
,
278 if (*serial_no
== NULL
)
279 *serial_no
= snmp_get_string(hostname
, community
,
280 "SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2");
281 tmp
= snmp_get_string(hostname
, community
,
282 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2");
284 tmp
= snmp_get_string(hostname
, community
,
285 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1");
294 snmp_oki_printer_info(char *hostname
, char *community
, char **manufacturer
,
295 char **model
, char **description
, char **serial_no
,
302 * OKI printers appear to store
303 * Prouct Name SNMPv2-SMI::enterprises.2001.1.2.683.1.3
304 * Serial Number SNMPv2-SMI::enterprises.2001.1.2.683.1.5
306 tmp
= snmp_get_string(hostname
, community
,
307 "SNMPv2-SMI::enterprises.2001.1.2.683.1.3");
309 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
310 description
, NULL
, serial_no
, command_set
);
313 if (*serial_no
== NULL
)
314 *serial_no
= snmp_get_string(hostname
, community
,
315 "SNMPv2-SMI::enterprises.2001.1.2.683.1.5");
322 snmp_hp_printer_info(char *hostname
, char *community
, char **manufacturer
,
323 char **model
, char **description
, char **serial_no
,
330 * HP printers appear to store
331 * 1284 DevID SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0
332 * Serial Number SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17
334 tmp
= snmp_get_string(hostname
, community
,
335 "SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0");
337 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
338 description
, NULL
, serial_no
, command_set
);
341 if (*serial_no
== NULL
)
342 *serial_no
= snmp_get_string(hostname
, community
,
343 "SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17");
350 snmp_ppm_printer_info(char *hostname
, char *community
, char **manufacturer
,
351 char **model
, char **description
, char **serial_no
,
358 * The PWG portMon MIB stores
359 * 1284 DevID SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3`
361 tmp
= snmp_get_string(hostname
, community
,
362 "SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3");
364 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
365 description
, NULL
, serial_no
, command_set
);
373 snmp_prt_printer_info(char *hostname
, char *community
, char **manufacturer
,
374 char **model
, char **description
, char **serial_no
,
381 * The Printer Printer MIB stores
382 * Vendor SNMPv2-SMI::mib-2.43.8.2.1.14.1.1
383 * Model SNMPv2-SMI::mib-2.43.8.2.1.15.1.1
384 * Serial SNMPv2-SMI::mib-2.43.8.2.1.17.1.1
387 if (*manufacturer
== NULL
)
388 *manufacturer
= snmp_get_string(hostname
, community
,
389 "SNMPv2-SMI::mib-2.43.8.2.1.14.1.1");
391 *model
= snmp_get_string(hostname
, community
,
392 "SNMPv2-SMI::mib-2.43.8.2.1.15.1.1");
393 if (*serial_no
== NULL
)
394 *serial_no
= snmp_get_string(hostname
, community
,
395 "SNMPv2-SMI::mib-2.43.8.2.1.17.1.1");
397 if (*manufacturer
!= NULL
)
404 snmp_host_resource_printer_info(char *hostname
, char *community
,
405 char **manufacturer
, char **model
, char **description
,
406 char **serial_no
, char ***command_set
)
411 tmp
= snmp_get_string(hostname
, community
,
412 "HOST-RESOURCES-MIB::hrDeviceDescr.1");
414 rc
= hrDeviceDesc_to_info(tmp
, manufacturer
, model
,
423 snmp_printer_info(char *hostname
, char *community
, char **manufacturer
,
424 char **model
, char **description
, char **serial_no
,
425 char ***command_set
, char **uri
)
429 init_snmp("network-printer-probe");
432 if (snmp_brother_printer_info(hostname
, community
, manufacturer
, model
,
433 description
, serial_no
, command_set
) == 0) {
435 } else if (snmp_ricoh_printer_info(hostname
, community
, manufacturer
,
436 model
, description
, serial_no
, command_set
) == 0) {
438 } else if (snmp_lexmark_printer_info(hostname
, community
, manufacturer
,
439 model
, description
, serial_no
, command_set
) == 0) {
441 } else if (snmp_xerox_phaser_printer_info(hostname
, community
,
442 manufacturer
, model
, description
, serial_no
,
443 command_set
, uri
) == 0) {
445 } else if (snmp_qms_printer_info(hostname
, community
, manufacturer
,
446 model
, description
, serial_no
, command_set
, uri
) == 0) {
448 } else if (snmp_oki_printer_info(hostname
, community
, manufacturer
,
449 model
, description
, serial_no
, command_set
) == 0) {
451 } else if (snmp_hp_printer_info(hostname
, community
, manufacturer
,
452 model
, description
, serial_no
, command_set
) == 0) {
454 } else if (snmp_ppm_printer_info(hostname
, community
, manufacturer
,
455 model
, description
, serial_no
, command_set
) == 0) {
457 } else if (snmp_prt_printer_info(hostname
, community
, manufacturer
,
458 model
, description
, serial_no
, command_set
) == 0) {
460 } else if (snmp_host_resource_printer_info(hostname
, community
,
461 manufacturer
, model
, description
, serial_no
,
471 #define NP(x) (x?x:"")
474 main(int ac
, char *av
[])
478 for (i
= 1; av
[i
] != NULL
; i
++) {
479 char *hostname
= av
[i
], *manufacturer
= NULL
, *model
= NULL
,
480 *description
= NULL
, *serial_no
= NULL
,
481 **command_set
= NULL
, *uri
= NULL
;
484 rc
= snmp_printer_info(hostname
, &manufacturer
, &model
,
485 &description
, &serial_no
, &command_set
, &uri
);
486 printf("SNMP data for %s...(%d)\n", hostname
, rc
);
487 printf("\tvendor = %s\n", NP(manufacturer
));
488 printf("\tproduct = %s\n", NP(model
));
489 printf("\tdescription = %s\n", NP(description
));
490 printf("\tserial = %s\n", NP(serial_no
));
491 printf("\tdevice = %s\n", NP(uri
));
493 if (command_set
!= NULL
) {
496 printf("\tcommand set = \n");
497 for (j
= 0; command_set
[j
] != NULL
; j
++)
498 printf("\t\t%s\n", command_set
[j
]);