1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
import java.util.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import snmp.*;
/*
* snmpgetnext.java
*
* Created on 11 mars 2011, 21:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Administrateur
*/
public class snmpgetnext extends JFrame
implements ActionListener, Runnable {
JButton getDataButton, getTreewalkDataButton, setValueButton;
JButton clearButton;
JTextArea messagesArea;
JScrollPane messagesScroll;
JTextField hostIDField, communityField, OIDField, valueField;
JLabel authorLabel, hostIDLabel, communityLabel, OIDLabel, valueLabel;
JComboBox valueTypeBox;
MenuBar theMenubar;
Menu fileMenu;
MenuItem quitItem;
Thread treewalkThread;
SNMPv1CommunicationInterface comInterface;
String community;
InetAddress hostAddress;
int version;
private class WindowCloseAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
private void setUpDisplay()
{
this.setTitle("SNMP Inquisitor");
this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));
// set fonts to smaller-than-normal size, for compaction!
UIManager manager = new UIManager();
FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
UIDefaults defaults = manager.getLookAndFeelDefaults();
Enumeration keys = defaults.keys();
while (keys.hasMoreElements())
{
String nextKey = (String)(keys.nextElement());
if ((nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1))
{
manager.put(nextKey, appFont);
}
}
// add WindowCloseAdapter to catch window close-box closings
addWindowListener(new WindowCloseAdapter());
theMenubar = new MenuBar();
this.setMenuBar(theMenubar);
fileMenu = new Menu("File");
quitItem = new MenuItem("Quit");
quitItem.setActionCommand("quit");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
theMenubar.add(fileMenu);
hostIDLabel = new JLabel("Device address:");
hostIDField = new JTextField(20);
hostIDField.setText("10.0.1.1");
hostIDField.setEditable(true);
OIDLabel = new JLabel("OID:");
OIDField = new JTextField(20);
OIDField.setEditable(true);
valueLabel = new JLabel("Value (for Set):");
valueField = new JTextField(20);
valueField.setEditable(true);
communityLabel = new JLabel("Community:");
communityField = new JTextField(20);
communityField.setText("public");
communityField.setEditable(true);
authorLabel = new JLabel(" Version 1.1 J. Sevy, January 2001 ");
authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8));
getDataButton = new JButton("Get OID value");
getDataButton.setActionCommand("get data");
getDataButton.addActionListener(this);
setValueButton = new JButton("Set OID value");
setValueButton.setActionCommand("set value");
setValueButton.addActionListener(this);
getTreewalkDataButton = new JButton("Get all OID values");
getTreewalkDataButton.setActionCommand("get treewalk data");
getTreewalkDataButton.addActionListener(this);
clearButton = new JButton("Clear responses");
clearButton.setActionCommand("clear messages");
clearButton.addActionListener(this);
messagesArea = new JTextArea(10,60);
messagesScroll = new JScrollPane(messagesArea);
valueTypeBox = new JComboBox();
valueTypeBox.addItem("SNMPInteger");
valueTypeBox.addItem("SNMPCounter32");
valueTypeBox.addItem("SNMPCounter64");
valueTypeBox.addItem("SNMPGauge32");
valueTypeBox.addItem("SNMPOctetString");
valueTypeBox.addItem("SNMPIPAddress");
valueTypeBox.addItem("SNMPNSAPAddress");
valueTypeBox.addItem("SNMPObjectIdentifier");
valueTypeBox.addItem("SNMPTimeTicks");
valueTypeBox.addItem("SNMPUInteger32");
// now set up display
// set params for layout manager
GridBagLayout theLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
c.ipadx = 0;
c.ipady = 0;
c.insets = new Insets(2,2,2,2);
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0;
c.weighty = 0;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(getDataButton, c);
buttonPanel.add(getDataButton);
c.gridx = 2;
c.gridy = 1;
theLayout.setConstraints(getTreewalkDataButton, c);
buttonPanel.add(getTreewalkDataButton);
c.gridx = 3;
c.gridy = 1;
theLayout.setConstraints(setValueButton, c);
buttonPanel.add(setValueButton);
JPanel hostPanel = new JPanel();
hostPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(hostIDLabel, c);
hostPanel.add(hostIDLabel);
c.gridx = 2;
c.gridy = 1;
theLayout.setConstraints(hostIDField, c);
hostPanel.add(hostIDField);
c.gridx = 1;
c.gridy = 2;
theLayout.setConstraints(communityLabel, c);
hostPanel.add(communityLabel);
c.gridx = 2;
c.gridy = 2;
theLayout.setConstraints(communityField, c);
hostPanel.add(communityField);
JPanel oidPanel = new JPanel();
oidPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(OIDLabel, c);
oidPanel.add(OIDLabel);
c.gridx = 2;
c.gridy = 1;
theLayout.setConstraints(OIDField, c);
oidPanel.add(OIDField);
c.gridx = 1;
c.gridy = 2;
theLayout.setConstraints(valueLabel, c);
oidPanel.add(valueLabel);
c.gridx = 2;
c.gridy = 2;
theLayout.setConstraints(valueField, c);
oidPanel.add(valueField);
c.gridx = 3;
c.gridy = 2;
theLayout.setConstraints(valueTypeBox, c);
oidPanel.add(valueTypeBox);
c.gridwidth = 1;
c.anchor = GridBagConstraints.CENTER;
JPanel messagesPanel = new JPanel();
messagesPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
JLabel messagesLabel = new JLabel("Responses:");
theLayout.setConstraints(messagesLabel, c);
messagesPanel.add(messagesLabel);
c.gridx = 2;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(clearButton, c);
messagesPanel.add(clearButton);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.weightx = .5;
c.weighty = .5;
c.anchor = GridBagConstraints.CENTER;
theLayout.setConstraints(messagesScroll, c);
messagesPanel.add(messagesScroll);
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);
}
/** Creates a new instance of snmpgetnext */
public snmpgetnext() {
treewalkThread = new Thread(this);
setUpDisplay();
}
public void actionPerformed(ActionEvent theEvent) {
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
{
snmpgetnext theApp = new snmpgetnext();
theApp.pack();
theApp.setSize(600,500);
theApp.show();
}
catch (Exception e)
{}
}
} |
Partager