How can I access ink levels of printers programmatically?

后端 未结 4 669
南笙
南笙 2021-02-02 04:44

Okay, this is a Windows specific question.

I need to be able to access the ink levels of a printer connected to a computer. Possibly direct connection, or a network conn

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 04:56

    I have an SNMP-capable HP 8600 pro N911a around to do some digging, so the following commands may help you a bit. Beware that this particular model has some firmware problems, you can't query "magenta" with snmpget, but you see a value with snmpwalk (which does some kind of recursive drill-down).

    OLD: You can query the names and sequence of values, but I couldn't find the "max value" to calculate a clean percentage so far ;(. I'm guessing so far the values are relative to 255, so dividing by 2.55 yields a percentage.

    Update: Marcelo's hint was great! From Registers .8.* you can read the max level per cartridge, and I was totally wrong assuming the max value can only be an 8-bit value. I have updated the sample script to read the max values and calculate c

    There is also some discussion over there at Cacti forums. One answer confirms that the ink levels are measured as percent (value 15 is "percent" in an enumeration):

    # snmpwalk -v1 -c public 192.168.100.173 1.3.6.1.2.1.43.11.1.1.7                 
    SNMPv2-SMI::mib-2.43.11.1.1.7.0.1 = INTEGER: 15
    SNMPv2-SMI::mib-2.43.11.1.1.7.0.2 = INTEGER: 15
    SNMPv2-SMI::mib-2.43.11.1.1.7.0.3 = INTEGER: 15
    SNMPv2-SMI::mib-2.43.11.1.1.7.0.4 = INTEGER: 15
    

    You need to install the net-snmp package. If you're not on Linux you might need some digging for SNMP command line tools for your preferred OS.

    # snmpwalk -v1 -c public 192.168.100.173 1.3.6.1.2.1.43.11.1.1.6.0
    SNMPv2-SMI::mib-2.43.11.1.1.6.0.1 = STRING: "black ink"
    SNMPv2-SMI::mib-2.43.11.1.1.6.0.2 = STRING: "yellow ink"
    SNMPv2-SMI::mib-2.43.11.1.1.6.0.3 = STRING: "cyan ink"
    SNMPv2-SMI::mib-2.43.11.1.1.6.0.4 = STRING: "magenta ink"
    
    # snmpwalk -v1 -c public 192.168.100.173 1.3.6.1.2.1.43.11.1.1.9.0
    SNMPv2-SMI::mib-2.43.11.1.1.9.0.1 = INTEGER: 231
    SNMPv2-SMI::mib-2.43.11.1.1.9.0.2 = INTEGER: 94
    SNMPv2-SMI::mib-2.43.11.1.1.9.0.3 = INTEGER: 210
    SNMPv2-SMI::mib-2.43.11.1.1.9.0.4 = INTEGER: 174
    
    # snmpwalk -v1 -c praxis 192.168.100.173 1.3.6.1.2.1.43.11.1.1.8.0
    SNMPv2-SMI::mib-2.43.11.1.1.8.0.1 = INTEGER: 674
    SNMPv2-SMI::mib-2.43.11.1.1.8.0.2 = INTEGER: 240
    SNMPv2-SMI::mib-2.43.11.1.1.8.0.3 = INTEGER: 226
    SNMPv2-SMI::mib-2.43.11.1.1.8.0.4 = INTEGER: 241
    

    On my Linux box I use the following script to do some pretty-printing:

    #!/bin/sh
    
    PATH=/opt/bin${PATH:+:$PATH}
    
    # get current ink levels
    eval $(snmpwalk -v1 -c praxis 192.168.100.173 1.3.6.1.2.1.43.11.1.1.6.0 |
    perl -ne 'print "c[$1]=$2\n" if(m!SNMPv2-SMI::mib-2.43.11.1.1.6.0.(\d) = STRING:\s+"(\w+) ink"!i);')
    
    # get max ink level per cartridge
    eval $(snmpwalk -v1 -c praxis 192.168.100.173 1.3.6.1.2.1.43.11.1.1.8.0 |
    perl -ne 'print "max[$1]=$2\n" if(m!SNMPv2-SMI::mib-2.43.11.1.1.8.0.(\d) = INTEGER:\s+(\d+)!i);')
    
    snmpwalk -v1 -c praxis 192.168.100.173 1.3.6.1.2.1.43.11.1.1.9.0 |
    perl -ne '
        my @c=("","'${c[1]}'","'${c[2]}'","'${c[3]}'","'${c[4]}'");
        my @max=("","'${max[1]}'","'${max[2]}'","'${max[3]}'","'${max[4]}'");
        printf"# $c[$1]=$2 (%.0f)\n",$2/$max[$1]*100
            if(m!SNMPv2-SMI::mib-2.43.11.1.1.9.0.(\d) = INTEGER:\s+(\d+)!i);'
    

提交回复
热议问题