⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpagenttest.java

📁 基于snmp/mib的网络数据获取系统设计与实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            {
                messagesArea.append("Unable to write message to report file\n\n");
            }
        }
    }
            
    
    
    public SNMPSequence processRequest(SNMPPDU pdu, String communityName) 
        throws SNMPGetException, SNMPSetException
    {
        writeMessage("Got pdu:\n");
        
        writeMessage("  community name:     " + communityName + "\n");
        writeMessage("  request ID:         " + pdu.getRequestID() + "\n");
        writeMessage("  pdu type:           ");
        byte pduType = pdu.getPDUType();
        switch (pduType)
        {
            case SNMPBERCodec.SNMPGETREQUEST:
            {
                writeMessage("SNMPGETREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPGETNEXTREQUEST:
            {
                writeMessage("SNMPGETNEXTREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPSETREQUEST:
            {
                writeMessage("SNMPSETREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPGETRESPONSE:
            {
                writeMessage("SNMPGETRESPONSE\n");
                break;
            }
            
            case SNMPBERCodec.SNMPTRAP:
            {
                writeMessage("SNMPTRAP\n");
                break;
            }
            
            default:
            {
                writeMessage("unknown\n");
                break;
            }
            
            
        }
        
        
        
        SNMPSequence varBindList = pdu.getVarBindList();
        SNMPSequence responseList = new SNMPSequence();
        
        for (int i = 0; i < varBindList.size(); i++)
        {
            SNMPSequence variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(i);
            SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
            SNMPObject snmpValue = (SNMPObject)variablePair.getSNMPObjectAt(1);
            
            writeMessage("       OID:           " + snmpOID + "\n");
            writeMessage("       value:         " + snmpValue + "\n");
            
            
            // check to see if supplied community name is ours; if not, we'll just silently
            // ignore the request by not returning anything
            if (!communityName.equals(this.communityName))
            {
                continue;
            }
            
            // we'll only respond to requests for OIDs 1.3.6.1.2.1.99.0 and 1.3.6.1.2.1.100.0 
            
            // OID 1.3.6.1.2.1.99.0: it's read-only
            if (snmpOID.toString().equals("1.3.6.1.2.1.99.0"))
            {
                if (pduType == SNMPBERCodec.SNMPSETREQUEST)
                {
                    // got a set-request for our variable; throw an exception to indicate the 
                    // value is read-only - the SNMPv1AgentInterface will create the appropriate
                    // error message using our supplied error index and status
                    // note that error index starts at 1, not 0, so it's i+1
                    int errorIndex = i+1;
                    int errorStatus = SNMPRequestException.VALUE_READ_ONLY;
                    throw new SNMPSetException("Trying to set a read-only variable!", errorIndex, errorStatus);
                }
                else if (pduType == SNMPBERCodec.SNMPGETREQUEST)
                {
                    // got a get-request for our variable; send back a value - just a string
                    try
                    {
                        SNMPVariablePair newPair = new SNMPVariablePair(new SNMPObjectIdentifier(snmpOID.toString()), new SNMPOctetString("Boo"));
                        //SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, new SNMPOctetString("Boo"));
                        responseList.addSNMPObject(newPair);
                    }
                    catch (SNMPBadValueException e)
                    {
                        // won't happen...
                    }
                } 
                
            }
            
            if (snmpOID.toString().equals("1.3.6.1.2.1.100.0"))
            {
                if (pduType == SNMPBERCodec.SNMPSETREQUEST)
                {
                    // got a set-request for our variable; supplied value must be a string
                    if (snmpValue instanceof SNMPOctetString)
                    {
                        // assign new value
                        storedSNMPValue = (SNMPOctetString)snmpValue;
                        
                        // return SNMPVariablePair to indicate we've handled this OID
                        try
                        {
                            SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, storedSNMPValue);
                            responseList.addSNMPObject(newPair);
                        }
                        catch (SNMPBadValueException e)
                        {
                            // won't happen...
                        }
                    
                    }
                    else
                    {
                        int errorIndex = i+1;
                        int errorStatus = SNMPRequestException.BAD_VALUE;
                        throw new SNMPSetException("Supplied value must be SNMPOctetString", errorIndex, errorStatus);
                    }
                    
                }
                else if (pduType == SNMPBERCodec.SNMPGETREQUEST)
                {
                    // got a get-request for our variable; send back a value - just a string
                    try
                    {
                        SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, storedSNMPValue);
                        responseList.addSNMPObject(newPair);
                    }
                    catch (SNMPBadValueException e)
                    {
                        // won't happen...
                    }
                } 
                
            }
            
        }
        
        writeMessage("\n");
        
        
        // return the created list of variable pairs
        return responseList;
        
    }
    
    
    
    
    public SNMPSequence processGetNextRequest(SNMPPDU pdu, String communityName)
        throws SNMPGetException
    {
        writeMessage("Got pdu:\n");
        
        writeMessage("  community name:     " + communityName + "\n");
        writeMessage("  request ID:         " + pdu.getRequestID() + "\n");
        writeMessage("  pdu type:           ");
        byte pduType = pdu.getPDUType();
        
        switch (pduType)
        {
            case SNMPBERCodec.SNMPGETREQUEST:
            {
                writeMessage("SNMPGETREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPGETNEXTREQUEST:
            {
                writeMessage("SNMPGETNEXTREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPSETREQUEST:
            {
                writeMessage("SNMPSETREQUEST\n");
                break;
            }
            
            case SNMPBERCodec.SNMPGETRESPONSE:
            {
                writeMessage("SNMPGETRESPONSE\n");
                break;
            }
            
            case SNMPBERCodec.SNMPTRAP:
            {
                writeMessage("SNMPTRAP\n");
                break;
            }
            
            default:
            {
                writeMessage("unknown\n");
                break;
            }
            
            
        }
        
        
        
        SNMPSequence varBindList = pdu.getVarBindList();
        SNMPSequence responseList = new SNMPSequence();
        
        for (int i = 0; i < varBindList.size(); i++)
        {
            SNMPSequence variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(i);
            SNMPObjectIdentifier suppliedOID = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
            SNMPObject suppliedObject = (SNMPObject)variablePair.getSNMPObjectAt(1);
            
            writeMessage("       OID:           " + suppliedOID + "\n");
            writeMessage("       value:         " + suppliedObject + "\n");
            
            
            // check to see if supplied community name is ours; if not, we'll just silently
            // ignore the request by not returning anything
            if (!communityName.equals(this.communityName))
            {
                continue;
            }
            
            // we'll only respond to requests for OID 1.3.6.1.2.1.99.0, and it's read-only;
            // for get-next request, we'll return the value for 1.3.6.1.2.1.100.0
            if (suppliedOID.toString().equals("1.3.6.1.2.1.99.0"))
            {
                if (pduType == SNMPBERCodec.SNMPGETNEXTREQUEST)
                {
                    // got a get-next-request for our variable; send back a value for OID 1.3.6.1.2.1.100.0
                    try
                    {
                        // create SNMPVariablePair for the next OID and its value 
                        SNMPObjectIdentifier nextOID = new SNMPObjectIdentifier("1.3.6.1.2.1.100.0");
                        SNMPVariablePair innerPair = new SNMPVariablePair(nextOID, storedSNMPValue);
                        
                        // now create a pair containing the supplied OID and the variable pair containing the following
                        // OID and its value; this allows the ANMPv1AgentInterface to know which of the supplied OIDs
                        // the new OID corresponds to (follows).
                        SNMPVariablePair outerPair = new SNMPVariablePair(suppliedOID, innerPair);
                        
                        // add the "compound" SNMPVariablePair to the response list
                        responseList.addSNMPObject(outerPair);
                    }
                    catch (SNMPBadValueException e)
                    {
                        // won't happen...
                    }
                }
                
            }
            
        }
        
        writeMessage("\n");
        
        
        // return the created list of variable pairs
        return responseList;
        
    }
    
    
    
    
    public void run()
    {
        int numChars;
        char[] charArray = new char[256];
        
        try
        {
            while (!readerThread.isInterrupted() && ((numChars = errorReader.read(charArray, 0, charArray.length)) != -1))
            {
                StringBuffer errorMessage = new StringBuffer();
                errorMessage.append("Problem receiving request:\n");
                errorMessage.append(new String(charArray, 0, numChars));
                errorMessage.append("\n");
                writeMessage(errorMessage.toString());
            }
        }
        catch(IOException e)
        {
            messagesArea.append("Problem receiving errors; error reporter exiting!");
        }
    }
    
    
    
    
    public static void main(String args[]) 
    {
        try
        {
            SNMPAgentTest theApp = new SNMPAgentTest();
            theApp.pack();
            theApp.setSize(600,500);
            theApp.show();
        }
        catch (Exception e)
        {}
    }
    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -