如何使用Java格式化SNMP中的输出

时间:2016-04-27 09:50:43

标签: java snmp

我正在尝试使用SNMP发送陷阱消息。使用来自以下链接的TrapSender和Receiver代码, http://techdive.in/snmp/snmp4j-trap-sender

Output obtained is:
Received PDU...
Trap Type = -89
1.3.6.1.2.1.1.3.0 = Wed Apr 27 15:17:59 IST 2016
1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.2.1.1.6
1.3.6.1.6.3.18.1.3.0 = 127.0.0.1
1.3.6.1.2.1.1.6 = No Exception Occured

现在我希望我的代码以下面的格式生成输出,这种格式是在我们使用snmpwalk时获得的。但我不知道如何使用JAva

获取此格式
"1.3.6.1.2.1.1.6.1.1.1.1" 
"1.3.6.1.2.1.1.6.1.1.1.1" 
"1.3.6.1.2.1.1.6.1.1.2.2" s "sdsda" 
"1.3.6.1.2.1.1.6.1.1.2.1" s "2016-04-22 09:36:52"

这可能吗?我应该用什么方法来实现这一目标?任何帮助,将不胜感激。

以下是我的发件人代码:

TransportMapping transport = new DefaultUdpTransportMapping();
            transport.listen();

            //Create Target 
            CommunityTarget comtarget = new CommunityTarget();
            comtarget.setCommunity(new OctetString(community));
            comtarget.setVersion(SnmpConstants.version2c);
            comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
            comtarget.setRetries(2);
            comtarget.setTimeout(5000);

            //Create PDU for V2
            PDU pdu = new PDU();

            // need to specify the system up time
            pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
            pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid)));
            pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(ipAddress)));

            // variable binding for Enterprise Specific objects, Severity (should be defined in MIB file)
            pdu.add(new VariableBinding(new OID(trapOid), new OctetString(message))); 
            pdu.setType(PDU.NOTIFICATION);

            //Send the PDU
            Snmp snmp = new Snmp(transport);
            System.out.println("Sending V2 Trap to " + ipAddress + " on Port " + port);
            /*System.out.println("PDU:" + pdu.toString());*/
            snmp.send(pdu, comtarget);
            snmp.close();

主要课程:

    public class Main{
        public static final String  community  = "public";
        // Sending Trap for sysLocation of RFC1213
        public static final String  trapOid          = ".1.3.6.1.2.1.1.6";                         
        public static final String  ipAddress      = "127.0.0.1";
        public static final int     port      = 162;

        public static void main(String args[]) throws SNMPNullFieldException{  
            TrapSender sender= new TrapSender(community, trapOid, ipAddress, port);
            String message = "No Exception Occured";
sender.sendSnmpV2Trap(message); 
    }  
}

0 个答案:

没有答案