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

📄 snmpinquisitor.java

📁 基于snmp/mib的网络数据获取系统设计与实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void actionPerformed(ActionEvent theEvent)
    // respond to button pushes, menu selections
    {
        String command = theEvent.getActionCommand();
        
    
        if (command == "quit")
        {
            System.exit(0);
        }
        
        
        
        if (command == "clear messages")
        {
            messagesArea.setText("");
        }
        
        
        
        if (command == "about")
        {
            AboutDialog aboutDialog = new AboutDialog(this);
        }
        
        
        
        if (command == "get data")
        {
            try
            {
            
                String community = communityField.getText();
                int version = 0;    // SNMPv1
                InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
                
                StringTokenizer st = new StringTokenizer(OIDField.getText(), " ,;");
                
                while (st.hasMoreTokens())
                {
                    String itemID = st.nextToken();    
                    SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
                    SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
                    SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
                    SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                    String typeString = snmpValue.getClass().getName();
                    
                    if (typeString.equals("snmp.SNMPOctetString"))
                    {
                        String snmpString = snmpValue.toString();
                        
                        // truncate at first null character
                        int nullLocation = snmpString.indexOf('\0');
                        if (nullLocation >= 0)
                            snmpString = snmpString.substring(0,nullLocation);
                        
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpString);
                        messagesArea.append("  (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n");
                    }
                    else
                    {
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpValue);
                        messagesArea.append("\n");
                    }
                }    
            }
            catch(InterruptedIOException e)
            {
                messagesArea.append("Interrupted during retrieval:  " + e + "\n");
            }
            catch(Exception e)
            {
                messagesArea.append("Exception during retrieval:  " + e + "\n");
            }
                    
        }
        
        
        
        if (command == "get next")
        {
            try
            {
            
                String community = communityField.getText();
                int version = 0;    // SNMPv1
                InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
                
                StringTokenizer st = new StringTokenizer(OIDField.getText(), " ,;");
                
                while (st.hasMoreTokens())
                {
                    String itemID = st.nextToken();    
                    SNMPVarBindList newVars = comInterface.getNextMIBEntry(itemID);
                    SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
                    SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
                    SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                    String typeString = snmpValue.getClass().getName();
                    
                    if (typeString.equals("snmp.SNMPOctetString"))
                    {
                        String snmpString = snmpValue.toString();
                        
                        // truncate at first null character
                        int nullLocation = snmpString.indexOf('\0');
                        if (nullLocation >= 0)
                            snmpString = snmpString.substring(0,nullLocation);
                        
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpString);
                        messagesArea.append("  (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n");
                    }
                    else
                    {
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpValue);
                        messagesArea.append("\n");
                    }
                }    
            }
            catch(InterruptedIOException e)
            {
                messagesArea.append("Interrupted during retrieval:  " + e + "\n");
            }
            catch(Exception e)
            {
                messagesArea.append("Exception during retrieval:  " + e + "\n");
            }
                    
        }
        
        
        
        if (command == "get table")
        {
            try
            {
            
                String community = communityField.getText();
                int version = 0;    // SNMPv1
                InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
                
                String itemID = OIDField.getText();    
                
                SNMPVarBindList newVars = comInterface.retrieveMIBTable(itemID);
                
                // print the retrieved stuff
                for (int i = 0; i < newVars.size(); i++)
                {
                    SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));
                    
                    SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
                    SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                    String typeString = snmpValue.getClass().getName();
                    
                    if (typeString.equals("snmp.SNMPOctetString"))
                    {
                        String snmpString = snmpValue.toString();
                        
                        // truncate at first null character
                        int nullLocation = snmpString.indexOf('\0');
                        if (nullLocation >= 0)
                            snmpString = snmpString.substring(0,nullLocation);
                        
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpString);
                        messagesArea.append("  (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n");
                    }
                    else
                    {
                        messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpValue);
                        messagesArea.append("\n");
                    }
                
                }
            }
            catch(InterruptedIOException e)
            {
                messagesArea.append("Interrupted during retrieval:  " + e + "\n");
            }
            catch(Exception e)
            {
                messagesArea.append("Exception during retrieval:  " + e + "\n");
            }
                    
        }
        
        
        
        
        if (command == "set value")
        {
            try
            {
            
                String community = communityField.getText();
                int version = 0;    // SNMPv1
                InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
            
                
                String itemID = OIDField.getText();
                String valueString = valueField.getText();
                String valueTypeString = (String)valueTypeBox.getSelectedItem();
                valueTypeString = "snmp." + valueTypeString;
                
                SNMPObject itemValue;
                Class valueClass = Class.forName(valueTypeString);
                itemValue = (SNMPObject)valueClass.newInstance();
                itemValue.setValue(valueString);
                    
                SNMPVarBindList newVars = comInterface.setMIBEntry(itemID, itemValue);
                
                SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
            
                SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
                
                SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                
                String typeString = snmpValue.getClass().getName();
                
                messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpValue);
                
                if (typeString.equals("snmp.SNMPOctetString"))
                    messagesArea.append("  (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n");
                else
                    messagesArea.append("\n");
            
            }
            catch(InterruptedIOException e)
            {
                messagesArea.append("Interrupted during retrieval:  " + e + "\n");
            }
            catch(Exception e)
            {
                messagesArea.append("Exception during retrieval:  " + e + "\n");
            }
                    
        }
        
        
        
        
        if (command == "get treewalk data")
        {
            if (!treewalkThread.isAlive())
            {
                treewalkThread = new Thread(this);
                treewalkThread.start();
                getTreewalkDataButton.setText("Stop OID retrieval");
            }
            else
            {
                treewalkThread.interrupt();
            }
        }
        
        
    
        
    }
    
    
    
    
    
    
    public void run() 
    {
        
        try
        {
        
            String community = communityField.getText();
            int version = 0;    // SNMPv1
            InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
            SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
        
            
            //String itemID = "1.3.6.1.2.1.1.1.0";    // start with device name
            String itemID = "";            
            String retrievedID = "1.3.6.1.2.1";        // start point
                
                
            while (!Thread.interrupted() && !retrievedID.equals(itemID))
            {
                itemID = retrievedID;
                
                SNMPVarBindList newVars = comInterface.getNextMIBEntry(itemID);
                
                SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
                SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
                SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                retrievedID = snmpOID.toString();
                String typeString = snmpValue.getClass().getName();
                
                if (typeString.equals("snmp.SNMPOctetString"))
                {
                    String snmpString = snmpValue.toString();
                    
                    // truncate at first null character
                    int nullLocation = snmpString.indexOf('\0');
                    if (nullLocation >= 0)
                        snmpString = snmpString.substring(0,nullLocation);
                    
                    messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpString);
                    messagesArea.append("  (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n");
                }
                else
                {
                    messagesArea.append("OID: " + snmpOID + "  type: " + typeString + "  value: " + snmpValue);
                    messagesArea.append("\n");
                }
            }
            
            
        }
        catch(InterruptedIOException e)
        {
            messagesArea.append("Interrupted during retrieval:  " + e + "\n");
        }
        catch(Exception e)
        {
            messagesArea.append("Exception during retrieval:  " + e + "\n");
        }
        catch(Error err)
        {
            messagesArea.append("Error during retrieval:  " + err + "\n");
        }
        
        getTreewalkDataButton.setText("Get all OID values");
        
    }
    
    
    
    
    
    
    
    
    private String hexByte(byte b)
    {
        int pos = b;
        if (pos < 0)
            pos += 256;
        String returnString = new String();
        returnString += Integer.toHexString(pos/16);
        returnString += Integer.toHexString(pos%16);
        return returnString;
    }
    
    
    
    
    
    
    
    
    
    public static void main(String args[]) 
    {
        try
        {
            SNMPInquisitor theApp = new SNMPInquisitor();
            theApp.pack();
            theApp.setSize(600,500);
            theApp.show();
        }
        catch (Exception e)
        {}
    }
    

}

⌨️ 快捷键说明

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