📄 snmpinquisitor.java
字号:
c.gridwidth = 1; c.weightx = 0; c.weighty = 0; this.getContentPane().setLayout(theLayout); c.gridx = 1; c.gridy = 1; theLayout.setConstraints(hostPanel, c); this.getContentPane().add(hostPanel); c.gridx = 1; c.gridy = 2; theLayout.setConstraints(oidPanel, c); this.getContentPane().add(oidPanel); c.gridx = 1; c.gridy = 3; theLayout.setConstraints(buttonPanel, c); this.getContentPane().add(buttonPanel); c.fill = GridBagConstraints.BOTH; c.gridx = 1; c.gridy = 4; c.weightx = .5; c.weighty = .5; theLayout.setConstraints(messagesPanel, c); this.getContentPane().add(messagesPanel); c.fill = GridBagConstraints.NONE; c.gridx = 1; c.gridy = 5; c.weightx = 0; c.weighty = 0; theLayout.setConstraints(authorLabel, c); this.getContentPane().add(authorLabel); } 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 == "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 == "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"); } 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 + -