wbml 2011-02-13 15:14
浏览 244
已采纳

使用snmp4j实现Snmp功能

求个例子~~

  • 写回答

1条回答 默认 最新

  • mirinda95925 2011-02-13 15:16
    关注

    这个例子是可以用的
    [code="java"]
    import java.io.IOException;

    import java.util.Vector;

    import org.snmp4j.CommunityTarget;

    import org.snmp4j.PDU;

    import org.snmp4j.Snmp;

    import org.snmp4j.TransportMapping;

    import org.snmp4j.event.ResponseEvent;

    import org.snmp4j.mp.SnmpConstants;

    import org.snmp4j.smi.Address;

    import org.snmp4j.smi.GenericAddress;

    import org.snmp4j.smi.OID;

    import org.snmp4j.smi.OctetString;

    import org.snmp4j.smi.VariableBinding;

    import org.snmp4j.transport.DefaultUdpTransportMapping;

    public class SnmpUtil {

       private Snmp snmp = null;   
    
    
    
       private Address targetAddress = null;   
    
    
    
       public void initComm() throws IOException {   
    
    
    
              // 设置Agent方的IP和端口   
    
              targetAddress = GenericAddress.parse("udp:127.0.0.1/161");   
    
              TransportMapping transport = new DefaultUdpTransportMapping();   
    
              snmp = new Snmp(transport);   
    
              transport.listen();   
    
       }   
    
    
    
       public ResponseEvent sendPDU(PDU pdu) throws IOException {   
    
              // 设置 target   
    
              CommunityTarget target = new CommunityTarget();   
    
              target.setCommunity(new OctetString("public"));   
    
              target.setAddress(targetAddress);   
    
              // 通信不成功时的重试次数   
    
              target.setRetries(2);   
    
              // 超时时间   
    
              target.setTimeout(1500);   
    
              target.setVersion(SnmpConstants.version1);   
    
              // 向Agent发送PDU,并返回Response   
    
              return snmp.send(pdu, target);   
    
       }   
    
    
    
       public void setPDU() throws IOException {   
    
              // set PDU   
    
              PDU pdu = new PDU();   
    
              pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST")));   
    
              pdu.setType(PDU.SET);   
    
              sendPDU(pdu);   
    
       }   
    
    
    
       public void getPDU() throws IOException {   
    
              // get PDU   
    
              PDU pdu = new PDU();   
    
              pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));   
    
              pdu.setType(PDU.GET);   
    
              readResponse(sendPDU(pdu));   
    
       }   
    
    
    
       public void readResponse(ResponseEvent respEvnt) {   
    
              // 解析Response   
    
              if (respEvnt != null && respEvnt.getResponse() != null) {   
    
                     Vector<VariableBinding> recVBs = respEvnt.getResponse()   
    
                                   .getVariableBindings();   
    
                     for (int i = 0; i < recVBs.size(); i++) {   
    
                            VariableBinding recVB = recVBs.elementAt(i);   
    
                            System.out.println(recVB.getOid() + " : " + recVB.getVariable());   
    
                     }   
    
              }   
    
       }   
    
    
    
       public static void main(String[] args) {   
    
              try {   
    
                     SnmpUtil util = new SnmpUtil();   
    
                     util.initComm();   
    
                     util.setPDU();   
    
                     util.getPDU();   
    
              } catch (IOException e) {   
    
                     e.printStackTrace();   
    
              }   
    
       }   
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图