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 snmp.*;




public class SNMPInquisitor extends JFrame
                            implements ActionListener, Runnable
{

    JButton getDataButton, getTreewalkDataButton, getTableButton, getNextButton, 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 aboutItem, quitItem, infoOid;

    Thread treewalkThread;

    SNMPv1CommunicationInterface comInterface;
    String community;
    InetAddress hostAddress;
    int version;


    // WindowCloseAdapter to catch window close-box closings
    private class WindowCloseAdapter extends WindowAdapter
    { 
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    };


    public SNMPInquisitor() 
    {
        treewalkThread = new Thread(this);

        setUpDisplay();

    }



    private void setUpDisplay()
    {


        this.setTitle("SNMP Inquisitor");

        this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));

        // set fonts to smaller-than-normal size, for compaction!
        /*
        FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
        UIDefaults defaults = UIManager.getLookAndFeelDefaults();
        Enumeration keys = defaults.keys();

        while (keys.hasMoreElements())
        {
            String nextKey = (String)(keys.nextElement());
            if ((nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1))
            {
                UIManager.put(nextKey, appFont);
            }
        }
        */

        // add WindowCloseAdapter to catch window close-box closings
        addWindowListener(new WindowCloseAdapter());


        theMenubar = new MenuBar();
        this.setMenuBar(theMenubar);
        fileMenu = new Menu("File");

        aboutItem = new MenuItem("About...");
        aboutItem.setActionCommand("about");
        aboutItem.addActionListener(this);
        fileMenu.add(aboutItem);

        infoOid = new MenuItem("OID...");
        infoOid.setActionCommand("oid");
        infoOid.addActionListener(this);
        fileMenu.add(infoOid);

        fileMenu.addSeparator();

        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("192.168.1.2");
        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("ANNETTE ARPANA ");
        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);

        getTableButton = new JButton("Get table");
        getTableButton.setActionCommand("get table");
        getTableButton.addActionListener(this);

        getNextButton = new JButton("Get next OID value");
        getNextButton.setActionCommand("get next");
        getNextButton.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(getNextButton, c);
        buttonPanel.add(getNextButton);

        c.gridx = 3;
        c.gridy = 1;
        theLayout.setConstraints(getTableButton, c);
        buttonPanel.add(getTableButton);

        c.gridx = 4;
        c.gridy = 1;
        theLayout.setConstraints(getTreewalkDataButton, c);
        buttonPanel.add(getTreewalkDataButton);

        c.gridx = 5;
        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);





    }





    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(), " ,;");
                //System.out.println("st="+st);
                //st.
                //if(st.equals("System Name"))st="1.3.6.1.2.1.5.0;" ;

                while (st.hasMoreTokens())
                {
                    //if(st.nextToken().equals("system name") itemID="1.3.6.1.2.1.5.0");
                    //)
                    //String itemID = "";
                    //if(st.nextToken().equals("system name")) itemID="1.3.6.1.2.1.5.0";
                    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(700,500);
            theApp.setVisible(true);
        }
        catch (Exception e)
        {
            System.out.println("Exception starting app: " + e.toString());
        }
    }


}
// this is the main class for the project



import snmp.*;
import java.math.*;
import java.net.*;



public class SNMPSample
{

    public static void main(String args[]) 
    {

        try
        {

            // create a communications interface to a remote SNMP-capable device;
            // need to provide the remote host's InetAddress and the community
            // name for the device; in addition, need to  supply the version number
            // for the SNMP messages to be sent (the value 0 corresponding to SNMP
            // version 1)
            InetAddress hostAddress = InetAddress.getByName("192.168.1.2");
            String community = "public";
            int version = 0;    // SNMPv1

            SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);



            // now send an SNMP GET request to retrieve the value of the SNMP variable
            // corresponding to OID 1.3.6.1.2.1.2.1.0; this is the OID corresponding to
            // the device identifying string, and the type is thus SNMPOctetString
            String itemID = "1.3.6.1.2.1.1.1.0";

            System.out.println("Retrieving value corresponding to OID " + itemID);

            // the getMIBEntry method of the communications interface returns an SNMPVarBindList
            // object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
            // returned Vector has just one pair inside it.
            SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);

            // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
            // SNMPSequence
            SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));

            // extract the object identifier from the pair; it's the first element in the sequence
            SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

            // extract the corresponding value from the pair; it's the second element in the sequence
            SNMPObject snmpValue = pair.getSNMPObjectAt(1);

            // print out the String representation of the retrieved value
            System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());

            // the retrieved value can be obtained from the SNMPObject using the getValue method;
            // the return type of the method is the generic base class Object, and must be cast to 
            // the appropriate actual Java type; in this case, for an SNMPOctetString, the underlying
            // Java type is a byte array[]
            byte[] javaByteArrayValue = (byte[])snmpValue.getValue();



            // now send an SNMP GET request to retrieve the value of the SNMP variable
            // corresponding to OID 1.3.6.1.2.1.1.3.0; this is the OID corresponding to
            // the uptime of the device, and the return type is thus SNMPTimeTicks
            itemID = "1.3.6.1.2.1.1.3.0";

            System.out.println("Retrieving value corresponding to OID " + itemID);

            // the getMIBEntry method of the communications interface returns an SNMPVarBindList
            // object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
            // returned Vector has just one pair inside it.
            newVars = comInterface.getMIBEntry(itemID);

            // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
            // SNMPSequence
            pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));

            // extract the object identifier from the pair; it's the first element in the sequence
            snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

            // extract the corresponding value from the pair; it's the second element in the sequence
            snmpValue = pair.getSNMPObjectAt(1);

            // print out the String representation of the retrieved value
            System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());

            // the retrieved value can be obtained from the SNMPObject using the getValue method;
            // the return type of the method is the generic base class Object, and must be cast to 
            // the appropriate actual Java type; in this case, for SNMPTimeTicks, which is a subclass
            // of SNMPInteger, the actual type is BigInteger (which permits arbitrarily large values to 
            // be represented).
            BigInteger javaIntegerValue = (BigInteger)snmpValue.getValue();



            // now send an SNMP GET request to simultaneously retrieve the value of the SNMP variables
            // corresponding to OIDs 1.3.6.1.2.1.1.1.0 to 1.3.6.1.2.1.1.5.0
            String[] itemIDs = {"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0", "1.3.6.1.2.1.1.3.0", "1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.5.0"};

            System.out.println("Retrieving value corresponding to OIDs: ");
            for (int i = 0; i < itemIDs.length; i++)
            {
                System.out.println("  " + itemIDs[i]);
            }

            // the getMIBEntry method of the communications interface returns an SNMPVarBindList
            // object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
            // returned Vector has several pairs inside it.
            newVars = comInterface.getMIBEntry(itemIDs);

            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < newVars.size(); i++)
            {
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));

                // extract the object identifier from the pair; it's the first element in the sequence
                snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

                // extract the corresponding value from the pair; it's the second element in the sequence
                snmpValue = pair.getSNMPObjectAt(1);

                // print out the String representation of the retrieved value
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }

            // now do get-next for the OIDS above; this will return the values for the OIDs following
            // each of the supplied OIDs
            System.out.println("Retrieving values _following_ OIDs: ");
            for (int i = 0; i < itemIDs.length; i++)
            {
                System.out.println("  " + itemIDs[i]);
            }

            newVars = comInterface.getNextMIBEntry(itemIDs);

            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < newVars.size(); i++)
            {
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(i));

                // extract the object identifier from the pair; it's the first element in the sequence
                snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

                // extract the corresponding value from the pair; it's the second element in the sequence
                snmpValue = pair.getSNMPObjectAt(1);

                // print out the String representation of the retrieved value
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }



            // next, retrieve an entire table, and print out the results
            // This uses the simple getMIBTable method, in which a single base OID is supplied,
            // and all of the values of OIDs which start with that base are retrieved, one element
            // at a time - this in effect retrieves the elements column-wise: all the elements in
            // the first column are retrieved first, then all the elements in the second column, etc.
            // This is an artifact of the way the OIDs of the elements in a table are formed in SNMP.
            String baseID = "1.3.6.1.2.1.2.2.1";

            System.out.println("Retrieving table corresponding to base OID " + baseID);

            SNMPVarBindList tableVars = comInterface.retrieveMIBTable(baseID);

            System.out.println("Number of table entries: " + tableVars.size());

            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < tableVars.size(); i++)
            {
                pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));

                // extract the object identifier from the pair; it's the first element in the sequence
                snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

                // extract the corresponding value from the pair; it's the second element in the sequence
                snmpValue = pair.getSNMPObjectAt(1);

                // print out the String representation of the retrieved value
                System.out.println("Retrieved OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }



            // Next, retrieve an entire table, but row-by-row rather than entry-by-entry
            // This uses the multiple getMIBTable method, in which an array of base OIDs is supplied,
            // and sequences of the values of OIDs which start with that base are retrieved. This returns
            // the elements row-wise: all the elements corresponding to the first row, followed by the 
            // elements in the second row, etc. Note also that only elements from the columns supplied 
            // are retrieved; this differs from the previous retrieval, in which all of the table's
            // elements, from all columns, are retrieved. This method is thus more selective in the
            // information retrieved. It is also more efficient, in that all of the elements in a single 
            // row are retrieved with a single get-request - multiple OIDs are requested in each message.
            String[] baseIDs = {"1.3.6.1.2.1.2.2.1.1", "1.3.6.1.2.1.2.2.1.2", "1.3.6.1.2.1.2.2.1.3", "1.3.6.1.2.1.2.2.1.4"};

            System.out.println("Retrieving table columns corresponding to base OIDs ");
            for (int i = 0; i < baseIDs.length; i++)
            {
                System.out.println("  " + baseIDs[i]);
            }

            tableVars = comInterface.retrieveMIBTable(baseIDs);

            System.out.println("Number of table entries: " + tableVars.size());

            // extract the (OID,value) pairs from the SNMPVarBindList; each pair is just a two-element
            // SNMPSequence
            for (int i = 0; i < tableVars.size(); i++)
            {
                pair = (SNMPSequence)(tableVars.getSNMPObjectAt(i));

                // extract the object identifier from the pair; it's the first element in the sequence
                snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

                // extract the corresponding value from the pair; it's the second element in the sequence
                snmpValue = pair.getSNMPObjectAt(1);

                // print out the String representation of the retrieved value
                System.out.println("Retrieved OID: " + snmpOID + ", type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            }



            // now send an SNMP SET request to set the value of the SNMP variable
            // corresponding to OID 1.3.6.1.2.1.1.4.0; this is the OID corresponding to
            // the device contact person, and the type is thus SNMPOctetString;
            // to set a new value, a string is supplied
            itemID = "1.3.6.1.2.1.1.4.0";    

            SNMPOctetString newValue = new SNMPOctetString("Jon S");

            System.out.println("Setting value corresponding to OID " + itemID);
            System.out.println("New value: " + newValue.toString());

            // the setMIBEntry method of the communications interface returns the SNMPVarBindList
            // corresponding to the supplied OID and value
            // This call will probably cause an SNMPSetException to be thrown, since the
            // community name "public" is probably not the read/write password of the device 
            newVars = comInterface.setMIBEntry(itemID, newValue);



            // now send an SNMP SET request to set the values of the SNMP variables
            // corresponding to OID 1.3.6.1.2.1.1.4.0 and 1.3.6.1.2.1.1.5.0; the latter
            // is the OID corresponding to the device name, and the type is thus SNMPOctetString;
            // to set a new value, a string is supplied
            String[] setItemIDs = {"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.5.0"};    

            SNMPOctetString[] newValues = {new SNMPOctetString("Jon"), new SNMPOctetString("Jon's device")};

            System.out.println("Setting value corresponding to OIDs " + itemID);
            for (int i = 0; i < setItemIDs.length; i++)
            {
                System.out.println("  " + setItemIDs[i] + ", new values " + newValues[i]);
            }

            // the setMIBEntry method of the communications interface returns the SNMPVarBindList
            // corresponding to the supplied OID and value
            // This call will probably cause an SNMPSetException to be thrown, since the
            // community name "public" is probably not the read/write password of the device 
            newVars = comInterface.setMIBEntry(setItemIDs, newValues);



        }
        catch(Exception e)
        {
            System.out.println("Exception during SNMP operation:  " + e + "\n");
        }

    }

}

u can create a class for the string characters only once and then call the class for the particular control in ur full project whenever required...

like below

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyTextField extends JTextField implements KeyListener,FocusListener
{
int size;
    public MyTextField(int size)
    {
        super(20);
        this.size=size;
        setFont(new Font("Palatino Linotype",0,15));
        addKeyListener(this);
        setDisabledTextColor(new Color(0,0,0));
        addFocusListener(this);
    }
    public void keyTyped(KeyEvent e)
    {   
        char ch=e.getKeyChar();     
            if(!(((ch<=90)&&(ch>=65))||((ch<=122)&&(ch>=97))||(ch==8)||(ch==127)))
            {
                if(ch==' ')
                    JOptionPane.showMessageDialog(this,"No space allowed.");
                else
                    JOptionPane.showMessageDialog(this,"Please enter alphabets only.");
                e.setKeyChar('\0');

            }
        if(getText().length()>size-1)
        e.setKeyChar('\0');
    }   
    public void keyPressed(KeyEvent e){}
    public void keyReleased(KeyEvent e){}

    public void focusLost(FocusEvent fe)
    {
        setBackground(new Color(255,255,255));
    }
    public void focusGained(FocusEvent fe)
    {

        setBackground(new Color(200,255,200));

    }
}

to make a reference in ur SNMPInquisitor or the other class call as below wherever u need...

MyTextField jt;

how u write

JTextField jt; 

same way....

it is convienent coz with this one class we can write in complete project and if changes are required change in once class only....

if(!(((ch<=90)&&(ch>=65))||((ch<=122)&&(ch>=97))||(ch==8)||(ch==127)))

Please don't do this in Java, it's not C.
Firstly, you don't need to use opaque numeric values when what you mean is a character. Much better to code ch>='a' && ch<='z' etc
More seriously, the designers of Java went to great lengths to make Java international, and you just crippled your code to work in English only. Even in the USA Spanish is widely used, and Spanish (like French, German etc etc) has letters that are not between ASCII A and Z. And this doesn't even touch on all the scripts that don't have A or Z in them at all. Much better to use the Character class's isLetter(char c) method, which knows about Unicode international characters.

This article has been dead for over six months. Start a new discussion instead.