使用snmp协议获取MIB的数据

公司最近的需求是获取打印机的配件信息,常规的TCP/IP协议获取不到数据,机器信息存放在MIB数据库中,需要用snmp协议读取机器中的信息,网络上查询了几天终于有点头绪了


废话不多说,直接贴代码


public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private final static String REMOTE_ADDRESS = "udp:172.25.73.122/161";
    public static final int UPDATE_TEXT = 1;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_TEXT:
                    PDU response= (PDU) msg.obj;
                    textView.setText(response+"");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.token);
        try {
            getData();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void getData() throws Exception {
      new Thread(){
          @Override
          public void run() {
              Address remoteAddress = GenericAddress.parse(REMOTE_ADDRESS);
              System.out.println("SNMP地址:" + REMOTE_ADDRESS + ";有效:" + remoteAddress.isValid());
              TransportMapping transportMapping = null;
              try {
                  transportMapping = new DefaultUdpTransportMapping();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              Snmp snmp = new Snmp(transportMapping);
              try {
                  snmp.listen();
              } catch (IOException e) {
                  e.printStackTrace();
              }

              //构造发送目标
              CommunityTarget target = new CommunityTarget();
              target.setCommunity(new OctetString("public"));
              target.setAddress(remoteAddress);
              target.setVersion(SnmpConstants.version2c);
              target.setRetries(10);
              target.setTimeout(1500);

              //构造发送内容
              PDU pdu = new PDU();
              OID oid = new OID("1.3.6.1.4.1.367.3.2.1.2.2.18");
              pdu.add(new VariableBinding(oid));
              pdu.setType(PDU.GETNEXT);

              //异步监听响应
              ResponseListener responseListener = new ResponseListener() {
                  @Override
                  public void onResponse(ResponseEvent event) {
                      ((Snmp)event.getSource()).cancel(event.getRequest(),this);
                      PDU response = event.getResponse();
                      PDU request = event.getRequest();
                      if(response == null){
                          System.out.println("请求超时:" + response + ",请求的内容:" + request);
                      }else {
                          System.out.println("获取到信息:" + response);
                          Message msg=handler.obtainMessage();
                          msg.obj=response;
                          msg.what=UPDATE_TEXT;
                          handler.sendMessage(msg);
                      }
                  }
              };

              //发送
              try {
                  snmp.send(pdu,target,null,responseListener);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              //由于是异步获取信息,在这里需要程序不能结束运行,否则接收不到异步获取的消息。
              try {
                  System.in.read();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }.start();
    }
}
记得加上网络权限!

你可能感兴趣的:(SNMP,snmp协议)