How can I access ink levels of printers programmatically?

后端 未结 4 662
南笙
南笙 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 05:11

    I really liked tseeling's approach!

    Complementarily, I found out that the max value for the OID ... .9 is not 255 as guessed by him, but it actually varies per individual cartridge. The values can be obtained from OID .1.3.6.1.2.1.43.11.1.1.8 (the results obtained by dividing by these values match the ones obtained by running hp-inklevels command from hplip.

    I wrote my own script that output CSVs like below (suppose printer IP addr is 192.168.1.20):

    # ./hpink 192.168.1.20
    black,73,366,19.9454
    yellow,107,115,93.0435
    cyan,100,108,92.5926
    magenta,106,114,92.9825
    

    values are in this order: ,,,

    The script source (one will notice I usually prefer awk over perl when the puzzle is simple enough):

    #!/bin/sh
    
    snmpwalk -v1 -c public $1 1.3.6.1.2.1.43.11.1.1 | awk '
    
    /.*\.6\.0\./ {
      sub(/.*\./,"");
      split($0,TT,/[ "]*/);
      color[TT[1]]=TT[4];
    }
    
    /.*\.8\.0\./ {
      sub(/.*\./,"");
      split($0,TT,/[ "]*/);
      maxlevel[TT[1]]=TT[4];
    }
    
    /.*\.9\.0\./ {
      sub(/.*\./,"");
      split($0,TT,/[ "]*/);
      print color[TT[1]] "," TT[4] "," maxlevel[TT[1]] "," TT[4] / maxlevel[TT[1]] * 100;
    }'
    

提交回复
热议问题