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
)
54 struct snmp_session session
, *ss
;
55 struct snmp_pdu
*request
= NULL
, *result
= NULL
;
57 unsigned int oid_len
= MAX_OID_LEN
;
59 /* initialize the SNMP session */
60 snmp_sess_init(&session
);
61 session
.peername
= host
;
62 session
.community
= (uchar_t
*)community
;
63 session
.community_len
= strlen((const char *)session
.community
);
64 session
.version
= SNMP_VERSION_1
;
67 if ((ss
= snmp_open(&session
)) == NULL
)
70 /* add the requested data */
71 if (!read_objid(mib_item
, Oid
, &oid_len
))
72 snmp_perror(mib_item
);
74 /* initialize the request PDU */
75 request
= snmp_pdu_create(SNMP_MSG_GET
);
76 snmp_add_null_var(request
, Oid
, oid_len
);
78 status
= snmp_synch_response(ss
, request
, &result
);
86 snmp_get_string(char *host
, char *community
, char *mib_item
)
89 struct snmp_pdu
*response
= NULL
;
91 response
= snmp_get_item(host
, community
, mib_item
);
93 if ((response
!= NULL
) && (response
->errstat
== SNMP_ERR_NOERROR
)) {
94 struct variable_list
*v
= response
->variables
;
96 if (v
->type
== ASN_OCTET_STR
) {
97 result
= calloc(1, v
->val_len
+ 1);
98 memcpy(result
, v
->val
.string
, v
->val_len
);
102 HAL_DEBUG(("snmp_get_string(%s, %s, %s): %s", host
, community
, mib_item
,
103 (result
?result
:"NULL")));
105 if (response
!= NULL
)
106 snmp_free_pdu(response
);
112 snmp_brother_printer_info(char *hostname
, char *community
, char **manufacturer
,
113 char **model
, char **description
, char **serial_no
,
120 * Brother printers appear to store
121 * 1284 DevID SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0
122 * Serial Number SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0
124 tmp
= snmp_get_string(hostname
, community
,
125 "SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0");
127 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
128 description
, NULL
, serial_no
, command_set
);
131 if (*serial_no
== NULL
)
132 *serial_no
= snmp_get_string(hostname
, community
,
133 "SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0");
140 snmp_ricoh_printer_info(char *hostname
, char *community
, char **manufacturer
,
141 char **model
, char **description
, char **serial_no
,
148 * OKI printers appear to store
149 * 1284 DevID SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0
150 * Serial Number SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0
152 tmp
= snmp_get_string(hostname
, community
,
153 "SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0");
155 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
156 description
, NULL
, serial_no
, command_set
);
159 if (*serial_no
== NULL
)
160 *serial_no
= snmp_get_string(hostname
, community
,
161 "SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0");
168 snmp_lexmark_printer_info(char *hostname
, char *community
, char **manufacturer
,
169 char **model
, char **description
, char **serial_no
,
176 * Lexmark printers appear to store
177 * 1284 DevID SNMPv2-SMI::enterprises.641.2.1.2.1.3.1
178 * Serial Number SNMPv2-SMI::enterprises.641.2.1.2.1.6.1
180 tmp
= snmp_get_string(hostname
, community
,
181 "SNMPv2-SMI::enterprises.641.2.1.2.1.3.1");
183 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
184 description
, NULL
, serial_no
, command_set
);
187 if (*serial_no
== NULL
)
188 *serial_no
= snmp_get_string(hostname
, community
,
189 "SNMPv2-SMI::enterprises.641.2.1.2.1.6.1");
196 snmp_xerox_phaser_printer_info(char *hostname
, char *community
,
197 char **manufacturer
, char **model
, char **description
,
198 char **serial_no
, char ***command_set
, char **uri
)
204 * Xerox Phaser XXXX printers store their
205 * 1284 DevID SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1
207 * SNMPv2-SMI::enterprises.128.2.1.3.1.1.0
208 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16
209 * SNMPv2-SMI::enterprises.23.2.32.4.1.0
211 * SNMPv2-SMI::enterprises.128.2.1.3.1.2.0
212 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17
213 * SNMPv2-SMI::enterprises.23.2.32.4.2.0
214 * Description SNMPv2-SMI::enterprises.253.8.53.3.2.1.2.1
215 * Serial Number SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1
216 * Uri SNMPv2-SMI::enterprises.128.2.1.3.6.23.1.5.1
219 tmp
= snmp_get_string(hostname
, community
,
220 "SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1");
222 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
223 description
, NULL
, serial_no
, command_set
);
227 if (*manufacturer
== NULL
)
228 *manufacturer
= snmp_get_string(hostname
, community
,
229 "SNMPv2-SMI::enterprises.128.2.1.3.1.1.0");
230 if (*manufacturer
== NULL
)
231 *manufacturer
= snmp_get_string(hostname
, community
,
232 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16");
233 if (*manufacturer
== NULL
)
234 *manufacturer
= snmp_get_string(hostname
, community
,
235 "SNMPv2-SMI::enterprises.23.2.32.4.1.0");
238 *model
= snmp_get_string(hostname
, community
,
239 "SNMPv2-SMI::enterprises.128.2.1.3.1.2.0");
241 *model
= snmp_get_string(hostname
, community
,
242 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17");
244 *model
= snmp_get_string(hostname
, community
,
245 "SNMPv2-SMI::enterprises.23.2.32.4.2.0");
247 if (*serial_no
== NULL
)
248 *serial_no
= snmp_get_string(hostname
, community
,
249 "SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1");
251 if ((*manufacturer
!= NULL
) && (*model
!= NULL
))
258 snmp_qms_printer_info(char *hostname
, char *community
, char **manufacturer
,
259 char **model
, char **description
, char **serial_no
,
260 char ***command_set
, char **uri
)
266 * MINOLTA-QMS printers appear to store
267 * Prouct Name SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1
268 * Serial Number SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2
269 * URI SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1
270 * SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2
272 tmp
= snmp_get_string(hostname
, community
,
273 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1");
275 rc
= hrDeviceDesc_to_info(tmp
, manufacturer
, model
,
279 if (*serial_no
== NULL
)
280 *serial_no
= snmp_get_string(hostname
, community
,
281 "SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2");
282 tmp
= snmp_get_string(hostname
, community
,
283 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2");
285 tmp
= snmp_get_string(hostname
, community
,
286 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1");
295 snmp_oki_printer_info(char *hostname
, char *community
, char **manufacturer
,
296 char **model
, char **description
, char **serial_no
,
303 * OKI printers appear to store
304 * Prouct Name SNMPv2-SMI::enterprises.2001.1.2.683.1.3
305 * Serial Number SNMPv2-SMI::enterprises.2001.1.2.683.1.5
307 tmp
= snmp_get_string(hostname
, community
,
308 "SNMPv2-SMI::enterprises.2001.1.2.683.1.3");
310 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
311 description
, NULL
, serial_no
, command_set
);
314 if (*serial_no
== NULL
)
315 *serial_no
= snmp_get_string(hostname
, community
,
316 "SNMPv2-SMI::enterprises.2001.1.2.683.1.5");
323 snmp_hp_printer_info(char *hostname
, char *community
, char **manufacturer
,
324 char **model
, char **description
, char **serial_no
,
331 * HP printers appear to store
332 * 1284 DevID SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0
333 * Serial Number SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17
335 tmp
= snmp_get_string(hostname
, community
,
336 "SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0");
338 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
339 description
, NULL
, serial_no
, command_set
);
342 if (*serial_no
== NULL
)
343 *serial_no
= snmp_get_string(hostname
, community
,
344 "SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17");
351 snmp_ppm_printer_info(char *hostname
, char *community
, char **manufacturer
,
352 char **model
, char **description
, char **serial_no
,
359 * The PWG portMon MIB stores
360 * 1284 DevID SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3`
362 tmp
= snmp_get_string(hostname
, community
,
363 "SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3");
365 rc
= ieee1284_devid_to_printer_info(tmp
, manufacturer
, model
,
366 description
, NULL
, serial_no
, command_set
);
374 snmp_prt_printer_info(char *hostname
, char *community
, char **manufacturer
,
375 char **model
, char **description
, char **serial_no
,
382 * The Printer Printer MIB stores
383 * Vendor SNMPv2-SMI::mib-2.43.8.2.1.14.1.1
384 * Model SNMPv2-SMI::mib-2.43.8.2.1.15.1.1
385 * Serial SNMPv2-SMI::mib-2.43.8.2.1.17.1.1
388 if (*manufacturer
== NULL
)
389 *manufacturer
= snmp_get_string(hostname
, community
,
390 "SNMPv2-SMI::mib-2.43.8.2.1.14.1.1");
392 *model
= snmp_get_string(hostname
, community
,
393 "SNMPv2-SMI::mib-2.43.8.2.1.15.1.1");
394 if (*serial_no
== NULL
)
395 *serial_no
= snmp_get_string(hostname
, community
,
396 "SNMPv2-SMI::mib-2.43.8.2.1.17.1.1");
398 if (*manufacturer
!= NULL
)
405 snmp_host_resource_printer_info(char *hostname
, char *community
,
406 char **manufacturer
, char **model
, char **description
,
407 char **serial_no
, char ***command_set
)
412 tmp
= snmp_get_string(hostname
, community
,
413 "HOST-RESOURCES-MIB::hrDeviceDescr.1");
415 rc
= hrDeviceDesc_to_info(tmp
, manufacturer
, model
,
424 snmp_printer_info(char *hostname
, char *community
, char **manufacturer
,
425 char **model
, char **description
, char **serial_no
,
426 char ***command_set
, char **uri
)
430 init_snmp("network-printer-probe");
433 if (snmp_brother_printer_info(hostname
, community
, manufacturer
, model
,
434 description
, serial_no
, command_set
) == 0) {
436 } else if (snmp_ricoh_printer_info(hostname
, community
, manufacturer
,
437 model
, description
, serial_no
, command_set
) == 0) {
439 } else if (snmp_lexmark_printer_info(hostname
, community
, manufacturer
,
440 model
, description
, serial_no
, command_set
) == 0) {
442 } else if (snmp_xerox_phaser_printer_info(hostname
, community
,
443 manufacturer
, model
, description
, serial_no
,
444 command_set
, uri
) == 0) {
446 } else if (snmp_qms_printer_info(hostname
, community
, manufacturer
,
447 model
, description
, serial_no
, command_set
, uri
) == 0) {
449 } else if (snmp_oki_printer_info(hostname
, community
, manufacturer
,
450 model
, description
, serial_no
, command_set
) == 0) {
452 } else if (snmp_hp_printer_info(hostname
, community
, manufacturer
,
453 model
, description
, serial_no
, command_set
) == 0) {
455 } else if (snmp_ppm_printer_info(hostname
, community
, manufacturer
,
456 model
, description
, serial_no
, command_set
) == 0) {
458 } else if (snmp_prt_printer_info(hostname
, community
, manufacturer
,
459 model
, description
, serial_no
, command_set
) == 0) {
461 } else if (snmp_host_resource_printer_info(hostname
, community
,
462 manufacturer
, model
, description
, serial_no
,
472 #define NP(x) (x?x:"")
475 main(int ac
, char *av
[])
479 for (i
= 1; av
[i
] != NULL
; i
++) {
480 char *hostname
= av
[i
], *manufacturer
= NULL
, *model
= NULL
,
481 *description
= NULL
, *serial_no
= NULL
,
482 **command_set
= NULL
, *uri
= NULL
;
485 rc
= snmp_printer_info(hostname
, &manufacturer
, &model
,
486 &description
, &serial_no
, &command_set
, &uri
);
487 printf("SNMP data for %s...(%d)\n", hostname
, rc
);
488 printf("\tvendor = %s\n", NP(manufacturer
));
489 printf("\tproduct = %s\n", NP(model
));
490 printf("\tdescription = %s\n", NP(description
));
491 printf("\tserial = %s\n", NP(serial_no
));
492 printf("\tdevice = %s\n", NP(uri
));
494 if (command_set
!= NULL
) {
497 printf("\tcommand set = \n");
498 for (j
= 0; command_set
[j
] != NULL
; j
++)
499 printf("\t\t%s\n", command_set
[j
]);