打印机剩余墨盒监测

 1 public class Constants {
 2     public static final String sysDescr = ".1.3.6.1.2.1.1.1.0";
 3     public static final String sysName = ".1.3.6.1.2.1.1.5.0";
 4     public static final String sysObjectID = ".1.3.6.1.2.1.1.2.0";
 5     public static final String ifNumber = ".1.3.6.1.2.1.2.1.0";
 6 
 7     public static final String m1 = ".1.3.6.1.2.1.43.11.1.1.8.1.1";
 8     public static final String m2 = ".1.3.6.1.2.1.43.11.1.1.9.1.1";
 9     public static final String m3 = ".1.3.6.1.2.1.43.11.1.1.9.1.2";
10     public static final String m4 = ".1.3.6.1.2.1.43.11.1.1.9.1.3";
11     public static final String m5 = ".1.3.6.1.2.1.43.11.1.1.9.1.4";
12     public static final String m6 = ".1.3.6.1.2.1.25.3.5.1.1.1";
13     public static final String[] ifOids=new String[]{
14         ".1.3.6.1.2.1.2.2.1.1",
15         ".1.3.6.1.2.1.2.2.1.2",
16         ".1.3.6.1.2.1.2.2.1.3",
17         ".1.3.6.1.2.1.2.2.1.4",
18         ".1.3.6.1.2.1.2.2.1.5",
19         ".1.3.6.1.2.1.2.2.1.6",
20         ".1.3.6.1.2.1.2.2.1.7",
21         ".1.3.6.1.2.1.2.2.1.8",
22         ".1.3.6.1.2.1.2.2.1.9",
23         ".1.3.6.1.2.1.2.2.1.10",
24         ".1.3.6.1.2.1.2.2.1.11",
25         ".1.3.6.1.2.1.2.2.1.12",
26         ".1.3.6.1.2.1.2.2.1.13",
27         ".1.3.6.1.2.1.2.2.1.14",
28         ".1.3.6.1.2.1.2.2.1.15",
29         ".1.3.6.1.2.1.2.2.1.16",
30         ".1.3.6.1.2.1.2.2.1.17",
31         ".1.3.6.1.2.1.2.2.1.18",
32         ".1.3.6.1.2.1.2.2.1.19",
33         ".1.3.6.1.2.1.2.2.1.20",
34         ".1.3.6.1.2.1.2.2.1.21",
35         ".1.3.6.1.2.1.2.2.1.22"
36     };
37     
38 }
Constants
  1 public class SNMPSessionUtil {
  2     private  Snmp snmp;
  3     private Address targetAddress;
  4     private String hostComputer;
  5     private String port;
  6     private String community;
  7     private int version;
  8     private  CommunityTarget communityTarget;
  9 
 10     public SNMPSessionUtil(String hostComputer,String port ,String community, String version) {
 11         this.hostComputer = hostComputer;
 12         this.community = community;
 13         this.port=port;
 14         if (version.equals("2")){
 15             this.version=SnmpConstants.version2c;
 16         }else {
 17             System.out.println("版本不对");
 18         }
 19         init();
 20     }
 21 // 初始化 
 22     public void init(){
 23         String target="udp:"+hostComputer+"/"+port;
 24         targetAddress= GenericAddress.parse(target);
 25         try {
 26             TransportMapping transportMapping=new DefaultUdpTransportMapping();
 27             snmp=new Snmp(transportMapping);
 28             snmp.listen();
 29             // 设置权限
 30             communityTarget=new CommunityTarget();
 31             communityTarget.setCommunity(new OctetString(community));
 32             communityTarget.setAddress(targetAddress);
 33             // 通信不成功重复次数
 34             communityTarget.setRetries(2);
 35             // 超时时间
 36             communityTarget.setTimeout(2*1000);
 37             // 设置版本
 38             communityTarget.setVersion(version);
 39         } catch (IOException e) {
 40             e.printStackTrace();
 41         }
 42     }
 43 // 单个获取方式
 44     public void getSnmpGet(String... oid) throws IOException {
 45         ResponseEvent responseEvent=null;
 46         for (int i=0;i<oid.length;i++){
 47             PDU pdu=new PDU();
 48             pdu.add(new VariableBinding(new OID(oid[i])));
 49             // 设置请求方式
 50             pdu.setType(PDU.GET);
 51             ResponseEvent event=snmp.send(pdu,communityTarget);
 52             if (null!=event){
 53                 readResponse(event);
 54             }
 55         }
 56 
 57     }
 58 // 对结果进行解析
 59     public void readResponse(ResponseEvent event){
 60         if (null!=event&&event.getResponse()!=null){
 61             System.out.println("收到回复,正在解析");
 62             Vector<VariableBinding>vector=(Vector<VariableBinding>) event.getResponse().getVariableBindings();
 63             for (int i=0;i<vector.size();i++){
 64                 VariableBinding vec= vector.elementAt(i);
 65                 System.out.println(vec);
 66             }
 67         }else
 68             System.out.println("没有收到回复");
 69     }
 70 // 遍历请求
 71     public void snmpWalk2(String oids[]){
 72         ArrayList mapList=new ArrayList<>();
 73         // 设置TableUtil的工具
 74         TableUtils utils=new TableUtils(snmp,new DefaultPDUFactory(PDU.GETBULK));
 75         utils.setMaxNumRowsPerPDU(2);
 76         OID[] clounmOid =new OID[oids.length];
 77         for (int i=0;i<oids.length;i++){
 78             clounmOid[i]=new OID(oids[i]);
 79         }
 80         // 获取查询结果list,new OID("0"),new OID("40")设置输出的端口数量
 81         List<TableEvent> list=utils.getTable(communityTarget,clounmOid,new OID("0"),new OID("40"));
 82         for (int i = 0; i < list.size(); i++) {
 83             // 取list中的一行
 84             TableEvent te = (TableEvent) list.get(i);
 85             // 对每一行结果进行再次拆分
 86             VariableBinding[] vb = te.getColumns();
 87             Map map  = new HashMap<String,String>();
 88             if (vb != null) {
 89                 for (int j = 0; j < vb.length; j++) {
 90                     System.out.println(vb[j].toString());
 91                 }
 92             } else {
 93                 throw new NullPointerException("被监控系统的网络不通或IP或其它相关配置错识!");
 94             }
 95         }
 96     }
 97     public static void main(String[] args) {
 98         SNMPSessionUtil snmpSessionUtil=new SNMPSessionUtil("192.168.1.165","161","public", "2");
 99 //        SNMPSessionUtil snmpSessionUtil=new SNMPSessionUtil("192.168.0.128","161","public", "2");
100 //        SNMPSessionUtil snmpSessionUtil=new SNMPSessionUtil("192.168.0.175","161","public", "2");
101 //        SNMPSessionUtil snmpSessionUtil=new SNMPSessionUtil("192.168.1.19","161","public", "2");
102        try {
103            snmpSessionUtil.getSnmpGet(Constants.sysDescr,Constants.sysName,Constants.sysObjectID,Constants.ifNumber,Constants.m1,Constants.m2,Constants.m3,Constants.m4,Constants.m5,Constants.m6);
104            snmpSessionUtil.snmpWalk2(Constants.ifOids);
105        } catch (IOException e) {
106             e.printStackTrace();
107        }
108     }
SNMPSessionUtil

经测试,京瓷打印机5201CDN,HP打印机MFP M277dw,MFP M476dw   适用。

注意:打印机需连接网线,设置为网络打印机,有IP地址,并开启SNMP。

猜你喜欢

转载自www.cnblogs.com/codinghappy/p/10535023.html