1 29 30 31 32 import java.util.*; 33 import java.net.*; 34 import java.awt.*; 35 import javax.swing.*; 36 import javax.swing.border.*; 37 import javax.swing.plaf.*; 38 import java.awt.event.*; 39 import java.io.*; 40 import snmp.*; 41 42 43 44 45 public class SNMPInquisitor extends JFrame 46 implements ActionListener, Runnable  47 { 48 49 JButton getDataButton, getTreewalkDataButton, getTableButton, getNextButton, setValueButton; 50 JButton clearButton; 51 JTextArea messagesArea; 52 JScrollPane messagesScroll; 53 JTextField hostIDField, communityField, OIDField, valueField; 54 JLabel authorLabel, hostIDLabel, communityLabel, OIDLabel, valueLabel; 55 JComboBox valueTypeBox; 56 57 MenuBar theMenubar; 58 Menu fileMenu; 59 MenuItem aboutItem, quitItem; 60 61 Thread treewalkThread; 62 63 SNMPv1CommunicationInterface comInterface; 64 String community; 65 InetAddress hostAddress; 66 int version; 67 68 69 private class WindowCloseAdapter extends WindowAdapter 71 { 72 public void windowClosing(WindowEvent e) 73 { 74 System.exit(0); 75 } 76 }; 77 78 79 public SNMPInquisitor() 80 { 81 treewalkThread = new Thread (this); 82 83 setUpDisplay(); 84 85 } 86 87 88 89 private void setUpDisplay() 90 { 91 92 93 this.setTitle("SNMP Inquisitor"); 94 95 this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED)); 96 97 112 113 addWindowListener(new WindowCloseAdapter()); 115 116 117 theMenubar = new MenuBar(); 118 this.setMenuBar(theMenubar); 119 fileMenu = new Menu("File"); 120 121 aboutItem = new MenuItem("About..."); 122 aboutItem.setActionCommand("about"); 123 aboutItem.addActionListener(this); 124 fileMenu.add(aboutItem); 125 126 fileMenu.addSeparator(); 127 128 quitItem = new MenuItem("Quit"); 129 quitItem.setActionCommand("quit"); 130 quitItem.addActionListener(this); 131 fileMenu.add(quitItem); 132 133 theMenubar.add(fileMenu); 134 135 136 hostIDLabel = new JLabel("Device address:"); 137 hostIDField = new JTextField(20); 138 hostIDField.setText("10.0.1.1"); 139 hostIDField.setEditable(true); 140 141 OIDLabel = new JLabel("OID:"); 142 OIDField = new JTextField(20); 143 OIDField.setEditable(true); 144 145 valueLabel = new JLabel("Value (for Set):"); 146 valueField = new JTextField(20); 147 valueField.setEditable(true); 148 149 communityLabel = new JLabel("Community:"); 150 communityField = new JTextField(20); 151 communityField.setText("public"); 152 communityField.setEditable(true); 153 154 authorLabel = new JLabel(" Version 1.1 J. Sevy, January 2001 "); 155 authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8)); 156 157 158 getDataButton = new JButton("Get OID value"); 159 getDataButton.setActionCommand("get data"); 160 getDataButton.addActionListener(this); 161 162 setValueButton = new JButton("Set OID value"); 163 setValueButton.setActionCommand("set value"); 164 setValueButton.addActionListener(this); 165 166 getTableButton = new JButton("Get table"); 167 getTableButton.setActionCommand("get table"); 168 getTableButton.addActionListener(this); 169 170 getNextButton = new JButton("Get next OID value"); 171 getNextButton.setActionCommand("get next"); 172 getNextButton.addActionListener(this); 173 174 getTreewalkDataButton = new JButton("Get all OID values"); 175 getTreewalkDataButton.setActionCommand("get treewalk data"); 176 getTreewalkDataButton.addActionListener(this); 177 178 clearButton = new JButton("Clear responses"); 179 clearButton.setActionCommand("clear messages"); 180 clearButton.addActionListener(this); 181 182 messagesArea = new JTextArea(10,60); 183 messagesScroll = new JScrollPane(messagesArea); 184 185 valueTypeBox = new JComboBox(); 186 valueTypeBox.addItem("SNMPInteger"); 187 valueTypeBox.addItem("SNMPCounter32"); 188 valueTypeBox.addItem("SNMPCounter64"); 189 valueTypeBox.addItem("SNMPGauge32"); 190 valueTypeBox.addItem("SNMPOctetString"); 191 valueTypeBox.addItem("SNMPIPAddress"); 192 valueTypeBox.addItem("SNMPNSAPAddress"); 193 valueTypeBox.addItem("SNMPObjectIdentifier"); 194 valueTypeBox.addItem("SNMPTimeTicks"); 195 valueTypeBox.addItem("SNMPUInteger32"); 196 197 198 199 201 GridBagLayout theLayout = new GridBagLayout(); 203 GridBagConstraints c = new GridBagConstraints(); 204 205 c.gridwidth = 1; 206 c.gridheight = 1; 207 c.fill = GridBagConstraints.NONE; 208 c.ipadx = 0; 209 c.ipady = 0; 210 c.insets = new Insets(2,2,2,2); 211 c.anchor = GridBagConstraints.CENTER; 212 c.weightx = 0; 213 c.weighty = 0; 214 215 216 JPanel buttonPanel = new JPanel(); 217 buttonPanel.setLayout(theLayout); 218 219 c.gridx = 1; 220 c.gridy = 1; 221 theLayout.setConstraints(getDataButton, c); 222 buttonPanel.add(getDataButton); 223 224 c.gridx = 2; 225 c.gridy = 1; 226 theLayout.setConstraints(getNextButton, c); 227 buttonPanel.add(getNextButton); 228 229 c.gridx = 3; 230 c.gridy = 1; 231 theLayout.setConstraints(getTableButton, c); 232 buttonPanel.add(getTableButton); 233 234 c.gridx = 4; 235 c.gridy = 1; 236 theLayout.setConstraints(getTreewalkDataButton, c); 237 buttonPanel.add(getTreewalkDataButton); 238 239 c.gridx = 5; 240 c.gridy = 1; 241 theLayout.setConstraints(setValueButton, c); 242 buttonPanel.add(setValueButton); 243 244 245 JPanel hostPanel = new JPanel(); 246 hostPanel.setLayout(theLayout); 247 248 c.gridx = 1; 249 c.gridy = 1; 250 theLayout.setConstraints(hostIDLabel, c); 251 hostPanel.add(hostIDLabel); 252 253 c.gridx = 2; 254 c.gridy = 1; 255 theLayout.setConstraints(hostIDField, c); 256 hostPanel.add(hostIDField); 257 258 c.gridx = 1; 259 c.gridy = 2; 260 theLayout.setConstraints(communityLabel, c); 261 hostPanel.add(communityLabel); 262 263 c.gridx = 2; 264 c.gridy = 2; 265 theLayout.setConstraints(communityField, c); 266 hostPanel.add(communityField); 267 268 269 270 JPanel oidPanel = new JPanel(); 271 oidPanel.setLayout(theLayout); 272 273 c.gridx = 1; 274 c.gridy = 1; 275 theLayout.setConstraints(OIDLabel, c); 276 oidPanel.add(OIDLabel); 277 278 c.gridx = 2; 279 c.gridy = 1; 280 theLayout.setConstraints(OIDField, c); 281 oidPanel.add(OIDField); 282 283 c.gridx = 1; 284 c.gridy = 2; 285 theLayout.setConstraints(valueLabel, c); 286 oidPanel.add(valueLabel); 287 288 c.gridx = 2; 289 c.gridy = 2; 290 theLayout.setConstraints(valueField, c); 291 oidPanel.add(valueField); 292 293 c.gridx = 3; 294 c.gridy = 2; 295 theLayout.setConstraints(valueTypeBox, c); 296 oidPanel.add(valueTypeBox); 297 298 299 c.gridwidth = 1; 300 c.anchor = GridBagConstraints.CENTER; 301 302 303 304 JPanel messagesPanel = new JPanel(); 305 messagesPanel.setLayout(theLayout); 306 307 c.gridx = 1; 308 c.gridy = 1; 309 c.anchor = GridBagConstraints.WEST; 310 JLabel messagesLabel = new JLabel("Responses:"); 311 theLayout.setConstraints(messagesLabel, c); 312 messagesPanel.add(messagesLabel); 313 314 c.gridx = 2; 315 c.gridy = 1; 316 c.anchor = GridBagConstraints.EAST; 317 theLayout.setConstraints(clearButton, c); 318 messagesPanel.add(clearButton); 319 320 c.fill = GridBagConstraints.BOTH; 321 c.gridx = 1; 322 c.gridy = 2; 323 c.gridwidth = 2; 324 c.weightx = .5; 325 c.weighty = .5; 326 c.anchor = GridBagConstraints.CENTER; 327 theLayout.setConstraints(messagesScroll, c); 328 messagesPanel.add(messagesScroll); 329 330 331 c.gridwidth = 1; 332 c.weightx = 0; 333 c.weighty = 0; 334 335 336 337 this.getContentPane().setLayout(theLayout); 338 339 340 c.gridx = 1; 341 c.gridy = 1; 342 theLayout.setConstraints(hostPanel, c); 343 this.getContentPane().add(hostPanel); 344 345 c.gridx = 1; 346 c.gridy = 2; 347 theLayout.setConstraints(oidPanel, c); 348 this.getContentPane().add(oidPanel); 349 350 c.gridx = 1; 351 c.gridy = 3; 352 theLayout.setConstraints(buttonPanel, c); 353 this.getContentPane().add(buttonPanel); 354 355 c.fill = GridBagConstraints.BOTH; 356 c.gridx = 1; 357 c.gridy = 4; 358 c.weightx = .5; 359 c.weighty = .5; 360 theLayout.setConstraints(messagesPanel, c); 361 this.getContentPane().add(messagesPanel); 362 363 c.fill = GridBagConstraints.NONE; 364 c.gridx = 1; 365 c.gridy = 5; 366 c.weightx = 0; 367 c.weighty = 0; 368 theLayout.setConstraints(authorLabel, c); 369 this.getContentPane().add(authorLabel); 370 371 372 } 373 374 375 376 377 378 public void actionPerformed(ActionEvent theEvent) 379 { 381 String command = theEvent.getActionCommand(); 382 383 384 if (command == "quit") 385 { 386 System.exit(0); 387 } 388 389 390 391 if (command == "clear messages") 392 { 393 messagesArea.setText(""); 394 } 395 396 397 398 if (command == "about") 399 { 400 AboutDialog aboutDialog = new AboutDialog(this); 401 } 402 403 404 405 if (command == "get data") 406 { 407 try 408 { 409 410 String community = communityField.getText(); 411 int version = 0; InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 413 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 414 415 StringTokenizer st = new StringTokenizer(OIDField.getText(), " ,;"); 416 417 while (st.hasMoreTokens()) 418 { 419 String itemID = st.nextToken(); 420 SNMPVarBindList newVars = comInterface.getMIBEntry(itemID); 421 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 422 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 423 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 424 String typeString = snmpValue.getClass().getName(); 425 426 if (typeString.equals("snmp.SNMPOctetString")) 427 { 428 String snmpString = snmpValue.toString(); 429 430 int nullLocation = snmpString.indexOf('\0'); 432 if (nullLocation >= 0) 433 snmpString = snmpString.substring(0,nullLocation); 434 435 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpString); 436 messagesArea.append(" (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n"); 437 } 438 else 439 { 440 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpValue); 441 messagesArea.append("\n"); 442 } 443 } 444 } 445 catch(InterruptedIOException e) 446 { 447 messagesArea.append("Interrupted during retrieval: " + e + "\n"); 448 } 449 catch(Exception e) 450 { 451 messagesArea.append("Exception during retrieval: " + e + "\n"); 452 } 453 454 } 455 456 457 458 if (command == "get next") 459 { 460 try 461 { 462 463 String community = communityField.getText(); 464 int version = 0; InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 466 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 467 468 StringTokenizer st = new StringTokenizer(OIDField.getText(), " ,;"); 469 470 while (st.hasMoreTokens()) 471 { 472 String itemID = st.nextToken(); 473 SNMPVarBindList newVars = comInterface.getNextMIBEntry(itemID); 474 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 475 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 476 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 477 String typeString = snmpValue.getClass().getName(); 478 479 if (typeString.equals("snmp.SNMPOctetString")) 480 { 481 String snmpString = snmpValue.toString(); 482 483 int nullLocation = snmpString.indexOf('\0'); 485 if (nullLocation >= 0) 486 snmpString = snmpString.substring(0,nullLocation); 487 488 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpString); 489 messagesArea.append(" (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n"); 490 } 491 else 492 { 493 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpValue); 494 messagesArea.append("\n"); 495 } 496 } 497 } 498 catch(InterruptedIOException e) 499 { 500 messagesArea.append("Interrupted during retrieval: " + e + "\n"); 501 } 502 catch(Exception e) 503 { 504 messagesArea.append("Exception during retrieval: " + e + "\n"); 505 } 506 507 } 508 509 510 511 if (command == "get table") 512 { 513 try 514 { 515 516 String community = communityField.getText(); 517 int version = 0; InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 519 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 520 521 String itemID = OIDField.getText(); 522 523 SNMPVarBindList newVars = comInterface.retrieveMIBTable(itemID); 524 525 for (int i = 0; i < newVars.size(); i++) 527 { 528 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(i)); 529 530 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 531 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 532 String typeString = snmpValue.getClass().getName(); 533 534 if (typeString.equals("snmp.SNMPOctetString")) 535 { 536 String snmpString = snmpValue.toString(); 537 538 int nullLocation = snmpString.indexOf('\0'); 540 if (nullLocation >= 0) 541 snmpString = snmpString.substring(0,nullLocation); 542 543 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpString); 544 messagesArea.append(" (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n"); 545 } 546 else 547 { 548 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpValue); 549 messagesArea.append("\n"); 550 } 551 552 } 553 } 554 catch(InterruptedIOException e) 555 { 556 messagesArea.append("Interrupted during retrieval: " + e + "\n"); 557 } 558 catch(Exception e) 559 { 560 messagesArea.append("Exception during retrieval: " + e + "\n"); 561 } 562 563 } 564 565 566 567 568 if (command == "set value") 569 { 570 try 571 { 572 573 String community = communityField.getText(); 574 int version = 0; InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 576 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 577 578 579 String itemID = OIDField.getText(); 580 String valueString = valueField.getText(); 581 String valueTypeString = (String )valueTypeBox.getSelectedItem(); 582 valueTypeString = "snmp." + valueTypeString; 583 584 SNMPObject itemValue; 585 Class valueClass = Class.forName(valueTypeString); 586 itemValue = (SNMPObject)valueClass.newInstance(); 587 itemValue.setValue(valueString); 588 589 SNMPVarBindList newVars = comInterface.setMIBEntry(itemID, itemValue); 590 591 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 592 593 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 594 595 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 596 597 String typeString = snmpValue.getClass().getName(); 598 599 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpValue); 600 601 if (typeString.equals("snmp.SNMPOctetString")) 602 messagesArea.append(" (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n"); 603 else 604 messagesArea.append("\n"); 605 606 } 607 catch(InterruptedIOException e) 608 { 609 messagesArea.append("Interrupted during retrieval: " + e + "\n"); 610 } 611 catch(Exception e) 612 { 613 messagesArea.append("Exception during retrieval: " + e + "\n"); 614 } 615 616 } 617 618 619 620 621 if (command == "get treewalk data") 622 { 623 if (!treewalkThread.isAlive()) 624 { 625 treewalkThread = new Thread (this); 626 treewalkThread.start(); 627 getTreewalkDataButton.setText("Stop OID retrieval"); 628 } 629 else 630 { 631 treewalkThread.interrupt(); 632 } 633 } 634 635 636 637 638 } 639 640 641 642 643 644 645 public void run() 646 { 647 648 try 649 { 650 651 String community = communityField.getText(); 652 int version = 0; InetAddress hostAddress = InetAddress.getByName(hostIDField.getText()); 654 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 655 656 657 String itemID = ""; 659 String retrievedID = "1.3.6.1.2.1"; 661 662 while (!Thread.interrupted() && !retrievedID.equals(itemID)) 663 { 664 itemID = retrievedID; 665 666 SNMPVarBindList newVars = comInterface.getNextMIBEntry(itemID); 667 668 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 669 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 670 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 671 retrievedID = snmpOID.toString(); 672 String typeString = snmpValue.getClass().getName(); 673 674 if (typeString.equals("snmp.SNMPOctetString")) 675 { 676 String snmpString = snmpValue.toString(); 677 678 int nullLocation = snmpString.indexOf('\0'); 680 if (nullLocation >= 0) 681 snmpString = snmpString.substring(0,nullLocation); 682 683 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpString); 684 messagesArea.append(" (hex: " + ((SNMPOctetString)snmpValue).toHexString() + ")\n"); 685 } 686 else 687 { 688 messagesArea.append("OID: " + snmpOID + " type: " + typeString + " value: " + snmpValue); 689 messagesArea.append("\n"); 690 } 691 } 692 693 694 } 695 catch(InterruptedIOException e) 696 { 697 messagesArea.append("Interrupted during retrieval: " + e + "\n"); 698 } 699 catch(Exception e) 700 { 701 messagesArea.append("Exception during retrieval: " + e + "\n"); 702 } 703 catch(Error err) 704 { 705 messagesArea.append("Error during retrieval: " + err + "\n"); 706 } 707 708 getTreewalkDataButton.setText("Get all OID values"); 709 710 } 711 712 713 714 715 716 717 718 719 private String hexByte(byte b) 720 { 721 int pos = b; 722 if (pos < 0) 723 pos += 256; 724 String returnString = new String (); 725 returnString += Integer.toHexString(pos/16); 726 returnString += Integer.toHexString(pos%16); 727 return returnString; 728 } 729 730 731 732 733 734 735 736 737 738 public static void main(String args[]) 739 { 740 try 741 { 742 SNMPInquisitor theApp = new SNMPInquisitor(); 743 theApp.pack(); 744 theApp.setSize(700,500); 745 theApp.setVisible(true); 746 } 747 catch (Exception e) 748 { 749 System.out.println("Exception starting app: " + e.toString()); 750 } 751 } 752 753 754 } | Popular Tags |